nekkron,
Here's the thing about functions - When you "pass-in" a variable, you don't actually pass-in the variable itself. You pass-in a copy of it's value. So, if x=2 and y=3, then the values 2 and 3 get passed-in.
And, you can pass variables A & B into this function: int subtract(int x, int y). Once you've created your subtract() function, you can use it to subtract any two integer variables!
When you pass-in A & B, their values are assigned to new local variables x & y... The variables inside the function are are new local variables. If you have other x & y variables in your program, you can change x & y inside the function and the other x & y variables will not be affected by the function.
Code:
-------------------------------------------------------------------------
int subtract(int x, int y); // Function prototype
int subtract(int, int); // Another way to write the function prototype
-------------------------------------------------------------------------
-------------------------------------------------------------------------
int subtract(int x, int y) // Function definition
{
return x-y;
}
-------------------------------------------------------------------------
-------------------------------------------------------------------------
D = subtract(x, y); // Function Call. Passes-in x & y and assigns subtraction result to D
E = subtract(a, b); // Calls same subtract() function, passes-in a & b
F = subtract(a, c); // Calls same subtract() function, passes-in a & c
Z = subtract(a, 4); // Calls same subtract() function, passes-in a & 4
-------------------------------------------------------------------------
As has been pointed-out, you don't need to create a function to subtract two numbers, since you can simply use the subtraction operator (the minus sign). There is no problem making an integer subtraction function as a learning exercise, but there is no reason to use it in a real world program.
A function is sort-of like a subcontractor. You use a function to do a particular job. You can use a function if you want to do something several times at different points in your program, and you don't want to repeat the same code over-and-over. You can also use functions to do special or complicated tasks. Putting these tasks into a function avoids cluttering-up your main() function with lots of details. And, if you are working a large project, different programmers can write different sets of functions.
NOTE - These are just the basics - This is not everything you need to know about functions! Most C++ books will have a chapter or two (or more) on functions.