int main() {
first(int a);
int d = 9;
cout << d << endl;
return 0;
}
void first(int a) {
a = 4;
cout << a << endl;
second(int a, int b);
}
void second(int a, int b) {
b = a + 3;
cout << b << endl;
third(int b, int c);
}
void third(int b, int c) {
c = b + 8;
cout << c << endl;
}
So I'm trying to share information between two functions without initializing the data to every function at once. I have tried to research how to do this, but I can't seem to find anything either in my book (either of them surprisingly) about how I would go about doing this. Is there any way I would be able to share information about variables between first() and second(), and second() and third() without sharing info between everything at once?
I thank you in advance to anybody who can help. Sorry if the question has been asked before.
Look up "Pass by Reference" first
Then try setting up a function called swap(int & first, int & second) to prove that you understand pass by ref.
Eventually you will also run across pointers which are kind of wacky but also a powerful thing to understand.
#include <iostream>
usingnamespace std;
void first();
void second(int);
void third(int);
int main() {
first();
int d = 9;
cout << d << endl;
}
void first() {
int a = 4;
cout << a << endl;
second(a);
}
void second(int a) {
int b = a + 3;
cout << b << endl;
third(b);
}
void third(int b) {
int c = b + 8;
cout << c << endl;
}
#include <iostream>
usingnamespace std;
void first(int & a, int & b, int & c);
void second(int a, int & b, int & c);
void third(int b, int & c);
int main() {
int a = 0;
int b = 0;
int c = 0;
first(a, b, c);
int d = 9;
cout << a << endl;
cout << b << endl;
cout << c << endl;
cout << d << endl;
}
void first(int & a, int & b, int & c) {
a = 4;
second(a, b, c);
}
void second(int a, int & b, int & c) {
b = a + 3;
third(b, c);
}
void third(int b, int & c) {
c = b + 8;
}
@OP - There are a number of problems with your code:
Line 4: Your function prototypes don't agree with your implementations. They must match exactly.
Lines 7,17,24: Do not specify types when making a function call.
Line 7: If you're trying to pass a as a parameter, a is not defined.
Line 17: Ditto for b.
Line 24: Ditto for c.
Line 15,22,29: What's the point of these lines? You're overlaying the value that was passed in.
PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post. http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
@AbstractionAnon. Thank you all for the help, and thank you for telling me how to properly send out the code. I will in the future post like that, though I'm just going to close this since I got the answer I needed. Thanks again