473,324 Members | 2,541 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

How to pass values through multiple functions?

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!
Jan 11 '07 #1
5 13575
DeMan
1,806 1GB
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).
Jan 11 '07 #2
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.
Jan 11 '07 #3
horace1
1,510 Expert 1GB
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.
Expand|Select|Wrap|Line Numbers
  1. // using pointer parameters to return values from functions
  2. #include <stdio.h>
  3.  
  4. void D(int *x, int *y)
  5. {
  6.      *x = 5;   // return values
  7.      *y = 10;
  8. }
  9.  
  10. void C(int *x, int *y)
  11. {
  12.      D(x, y);
  13. }
  14.  
  15. void B(int *x, int *y)
  16. {
  17.      C(x, y);
  18. }
  19.  
  20. void A()
  21. {
  22.      int x, y;
  23.      B(&x, &y);   // pass address of parameters
  24.      printf("x %d y %d\n", x, y);
  25. }
  26.  
  27. int main()
  28. {
  29.     A();
  30.     getchar();
  31. }
  32.  
if using C++ you can use reference parameters
Expand|Select|Wrap|Line Numbers
  1. // using reference parameters to return values from functions
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. void D(int &x, int &y)
  6. {
  7.      x = 5;   // return values
  8.      y = 10;
  9. }
  10.  
  11. void C(int &x, int &y)
  12. {
  13.      D(x, y);
  14. }
  15.  
  16. void B(int &x, int &y)
  17. {
  18.      C(x, y);
  19. }
  20.  
  21. void A()
  22. {
  23.      int x, y;
  24.      B(x, y);   // pass reference parameters
  25.      cout << "x " << x << " y " <<  y << endl;
  26. }
  27.  
  28. int main()
  29. {
  30.     A();
  31.     getchar();
  32. }
  33.  
Jan 11 '07 #4
DeMan
1,806 1GB
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:
Expand|Select|Wrap|Line Numbers
  1. A =Tea makeTea(teaLeaves, Water, teaPot) /*Returns a hot Tea, given fresh tea leaves, cold water and a container to store the tea */
  2. B = hotWater boilWater(Water, Kettle) /* returns hot water when given cold water and Kettle */
  3. C = Tea infuseTea(hotWater, teaLeaves, teaPot) /* Makes tea in a teapot, from hot water and tealeaves */
  4. D = void smokoBreak() /*runs a ciggarette break */
  5.  
  6. /* 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 */
  7. Consider 1)  D has hotWater teaLeaves and teaPot, A has Kettle:
  8. smokoBreak()
  9. {
  10.   //do other things
  11.   cuppa = infuseTea(hotWater, teaLeaves, teaPot);
  12.   drink(cuppa);
  13. }
  14.  
  15. Consider 2) D has coldWater, Kettle, teaLeaves, teaPot
  16. smokoBreak()
  17. {
  18.   //do other things
  19.   cuppa = infuseTea(boilWater(Water, Kettle), teaLeaves, teaPot);
  20.   drink(cuppa);
  21. } /* For clarity you might like to get the hotWater in a temp variable */
  22.  
  23. Consider 3)  D has coldWater, teaLeaves, teaPot
  24. smokoBreak
  25. {
  26.   //do other things
  27.   cuppa = makeTea(teaLeaves, Water, teaPot);
  28.   drink(cuppa);
  29. }
  30.  
  31. makeTea(teaLeaves, Water, teaPot)
  32. {
  33.     /* Aha we have Kettle */
  34.     return  infuseTea(boilWater(Water, Kettle), teaLeaves, teaPot); 
  35. }
  36.  
  37.  
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)
Jan 11 '07 #5
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!
Jan 11 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

66
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()
4
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...
8
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,...
2
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...
23
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: ...
1
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...
14
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...
12
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....
1
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: ...
0
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
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...
1
isladogs
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...
0
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...
0
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
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....
0
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.