Inline commentary:
Code:
int myFunction(int whoAmI_1, int whoAmI_2) //Am I declaring these
//now? So they didnt
//exist till now?
You're saying that myFunction must be passed two integers. It doesn't matter what the variables are called in the original function; they can be constants like "5", or "x" or "someLongName" or even "whoAmI_1". The names are solely for the convenience of this function. Their values must be supplied by the calling function.
Code:
{
int whoAmI_1 = 2;
int whoAmI_2 = 3;
Really bad idea to redeclare these variables again. Now you have two copies: the ones which were passed in as arguments, and the ones declared locally on the stack. The local variables take precedence, "shadowing" the other ones, because they're declared later. Still, a very bad idea. You might be interested in reading Functions and Character Arrays HELP!
Code:
int sendBack;
inMain += wh0AmI_1; //how do I use this "inMain" from another
//function
Okay, so you're setting up a variable to use as a return value. Keep in mind that variable names are mostly only relevant to the function they're inside. You can return the value of this variable inMain with "return inMain;" or you can return a value directly as in "return 2*5;". The calling function doesn't know, or care, how the return value was computed. It certainly doesn't know or care the name of the variable you used, if any.
Code:
sendBack = whoAmI_1 + whoAmI_2;
return sendBack;
//I know I am sending "sendBack" to the function it was called
//from right? If this is so than what are the parameters for at the
//top? Are those vars I am getting from another function?
// Also what if I want to send all of the variables back to main for
//further computation? can I return a few vars?
You can only return one value. If you want to return multiple values you have to "pass by reference", essentially make one or more variables references or pointers. That's a bit more advanced but you can look it up if you're interested. https://www.google.com/search?q=pass...nt=iceweasel-a
Code:
}
int main()
{
int inMain=4;
Okay, now you're confusing yourself... the variable "inMain" that you've declared here will not be the same variable that occurs with the same name in other functions. You're just allocating space for an integer, and you happened to call it "inMain". The name doesn't matter to any other functions. Different functions in different scopes can name variables whatever they like. How else would programmers be able to write for loops and always use "i" or "x" as a name? Can you imagine making up a new, unique name every time you needed a loop?
Code:
cout << myFunction(); //This should print out 5 because
//"myFunction" returned "sendBack" which
//is 4 right?
//What about the parameters I put in when I
//created the function? do i need those here?
return 0;
}
Yes. You need to pass parameters to functions that are expecting parameters.
A few other quick examples to get you going.
Code:
void function(int oneArg) {}
function(5); // pass in a constant
int x = 4;
function(x); // pass in a variable -- note that the name "x" doesn't mean anything to the called function
Code:
int add(int a, int b) {
// this function takes two parameters. We're going to add them up and return them.
// We could use an extra variable here, but it's not necessary.
return a + b;
}
int x = add(1, 2); // pass in constants
int y = add(x, x); // pass in variables, even the same variable. The value is just copied.
int a = add(3, 4); // I can call the variable storing the return value whatever I want. It won't interfere with parameters.
Code:
// (this function is called myswap because there's already a std::swap built-in to the standard library!)
void myswap(int &x, int &y) {
// this function is supposed to swap two variables, in a sense return two values.
// Because the parameters are passed by reference, any changes you do here will be reflected in the calling function.
int temp = x;
x = y;
y = temp;
// now x and y have been changed, we can just let the function exit.
}
int x=1, y=2;
myswap(x, y);
// now x = 2 and y = 1
// myswap(4, 5); // can't pass constants where references are expected
Hope you find some of that useful.