Hello Helpers,
I have a question about passing values through multiple C++ functions:
Function A calls function B;
Function B calls function C;
Function C calls function D;
D has two values “a” and “b”, need pass them to Function A. Please help me on
How to make this happen? Should I pass values of “a” and “b” to C, then to C,
to B, finally to A?
I know it is a basic question, but I could not figure it out by myself. Thanks!
5 13575
I'm a little confused as to what you mean, but I don't see any reason why D can't call A directly.
If A calls B calls C calls D we don't have to follow back the way we came (unless the functions are not all visible to each other, in which case the fact that they call in the order provided is irrelevant to the problem, and the problem depends on who can call who in the other direction).
Thanks for your reply!
The reason is because D needs inputs from C; but A does not have it. After sevearl function calls (B, C), D gets the inputs, then process and return values.
Thanks for your reply!
The reason is because D needs inputs from C; but A does not have it. After sevearl function calls (B, C), D gets the inputs, then process and return values.
if you are using C you can use pointers to achieve "pass by reference", e.g. -
// using pointer parameters to return values from functions
-
#include <stdio.h>
-
-
void D(int *x, int *y)
-
{
-
*x = 5; // return values
-
*y = 10;
-
}
-
-
void C(int *x, int *y)
-
{
-
D(x, y);
-
}
-
-
void B(int *x, int *y)
-
{
-
C(x, y);
-
}
-
-
void A()
-
{
-
int x, y;
-
B(&x, &y); // pass address of parameters
-
printf("x %d y %d\n", x, y);
-
}
-
-
int main()
-
{
-
A();
-
getchar();
-
}
-
if using C++ you can use reference parameters -
// using reference parameters to return values from functions
-
#include <iostream>
-
using namespace std;
-
-
void D(int &x, int &y)
-
{
-
x = 5; // return values
-
y = 10;
-
}
-
-
void C(int &x, int &y)
-
{
-
D(x, y);
-
}
-
-
void B(int &x, int &y)
-
{
-
C(x, y);
-
}
-
-
void A()
-
{
-
int x, y;
-
B(x, y); // pass reference parameters
-
cout << "x " << x << " y " << y << endl;
-
}
-
-
int main()
-
{
-
A();
-
getchar();
-
}
-
Again this is a visibility thing (and it depends what the programming is doing).
If C can complete all the functionality required by D, (and D can see C) D can call see directly.
If (on the other hand) D needs all of the functionality of A, who uses all of B to do part of what he wants (etc into the third layer), you would need to call A to call B to call C
example: -
A =Tea makeTea(teaLeaves, Water, teaPot) /*Returns a hot Tea, given fresh tea leaves, cold water and a container to store the tea */
-
B = hotWater boilWater(Water, Kettle) /* returns hot water when given cold water and Kettle */
-
C = Tea infuseTea(hotWater, teaLeaves, teaPot) /* Makes tea in a teapot, from hot water and tealeaves */
-
D = void smokoBreak() /*runs a ciggarette break */
-
-
/* Let's ignore for a second exactly where some of the variables will come from, we will assume we have them as we use them */
-
Consider 1) D has hotWater teaLeaves and teaPot, A has Kettle:
-
smokoBreak()
-
{
-
//do other things
-
cuppa = infuseTea(hotWater, teaLeaves, teaPot);
-
drink(cuppa);
-
}
-
-
Consider 2) D has coldWater, Kettle, teaLeaves, teaPot
-
smokoBreak()
-
{
-
//do other things
-
cuppa = infuseTea(boilWater(Water, Kettle), teaLeaves, teaPot);
-
drink(cuppa);
-
} /* For clarity you might like to get the hotWater in a temp variable */
-
-
Consider 3) D has coldWater, teaLeaves, teaPot
-
smokoBreak
-
{
-
//do other things
-
cuppa = makeTea(teaLeaves, Water, teaPot);
-
drink(cuppa);
-
}
-
-
makeTea(teaLeaves, Water, teaPot)
-
{
-
/* Aha we have Kettle */
-
return infuseTea(boilWater(Water, Kettle), teaLeaves, teaPot);
-
}
-
-
This is a contrived example (REALLY?), but hopefully it demonstrates when you might and mightn't want to make methods nested (A calls B calls C...), sequential (A calls B then A calls C....) or optional (A can call D itself, or can call other methods (B perhaps) to do part of the work that D does (eg I'm not a fan of Tea, but I still want hotWater for my noodles)
Thanks very much for your replys! It really help me understand more about passing in functions.
Actually, from A to D, it does not only go through B, C. It goes through 6 functions. Only the final function D has value A wants, other functions in the middle do not care about the return value from D, but they do something else.
Should I have to pass them one by one as a chain or there is another way? What about if I use Global variables, does it help? Thank you!
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Darren Dale |
last post by:
Hello,
def test(data):
i = ? This is the line I have trouble with
if i==1: return data
else: return data
a,b,c,d = test()
|
by: cwwilly |
last post by:
Hello,
Thanks for taking a look at this!
Problem:
I'm trying to pass multiple dynamic values between a slaveform and a
masterform. The problem I'm having is on the slaveform I loop through...
|
by: darrel |
last post by:
I'm still trying to fully understand how best to pass variables between
pages/usercontrols/each other.
On a current site I've done, I've had one userControl do the logic and set
the variable,...
|
by: macyp |
last post by:
I have to pass values from one aspx page to another. The controls I
have in the first page are:
a textbox, 3 drop down lists, and 2 check boxes, and a submit button.
It is a search page, and the...
|
by: Sanjay Kumar |
last post by:
Folks,
I am getting back into C++ after a long time and I have
this simple question: How do pyou ass a STL container
like say a vector or a map (to and from a function) ?
function:
...
|
by: dan_williams |
last post by:
Is it possible to pass multiple discrete values to a report document so
that I can export it to PDF?
I've managed to perform the following code to display a Crystal Report
Viewer ok, but i want...
|
by: Abhi |
last post by:
I wrote a function foo(int arr) and its prototype
is declared as foo(int arr); I modify the values of the array in the
function and the values are getting modified in the main array which
is...
|
by: Bryan Parkoff |
last post by:
I write my large project in C++ source code. My C++ source code
contains approximate four thousand small functions. Most of them are
inline. I define variables and functions in the global scope....
|
by: Murdz |
last post by:
Hi all,
A constructor for System.Text.RegularExpressions.Regex includes a 2nd parameter for the RegexOptions enum. With this you can pass multiple RegexOptions as below:
...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |