Changing while passing

Okay, I'm getting the hang of passing variables. Variables that are in int main could be passed into different functions for one reason or another using arguments. Could someone tell me why the name changes though when it shows up in the definition? Whenever I see the argument in int main, its always different then its matching argument in the definition. Though the type, of coarse, has to stay the same, wouldn't it be easier to have the same name?
The point about functions is they allow the same code to be re-used. That means the function may be called many times, each call passing a differently-named variable. So it's almost inevitable that the name may be different.

Also, the significance of the variable will vary, depending upon where it is used. In function main(), a variable may represent say, an age, or a weight. But in the other function, the variable may simply be considered as a number to be printed, or manipulated in some way, and the function doesn't care what it actually represents.
I don't understand you, ¿could you provide an example?
Variable names are relative to the function that's using them. You most certainly can name them identically; although you get an extreme amount of flexibility from being able to name them what you want.

When it comes down to it, what you name variables isn't important (as far as the compiler is concerned).
For example, say you have some function, compare, that takes two integers and returns true if they are equal, otherwise it returns false.
The prototype of that function can be
1
2
3
4
5
6
7
8
9
bool compare(int first, int second); //conventional function prototype

//However, this is an equally correct prototype:
bool compare(int, int);

//If you used the latter, you create a name for the variables when you define the function, and it doesn't matter what those names are.
bool compare(int left, int right) {
    return left == right;
}


It's also important to note that, in most cases, when you pass a variable to a function, you don't pass that exact variable. A copy of that variable is created in the scope of the function you call. (excluding pointers and references, where a copy of the pointer/reference (address of the data location) is created instead of a copy of the entire variable).

A small example:
1
2
3
4
5
6
7
8
9
bool compare(int left, int right) {
    return left == right;
}

int main()
{
    int x = 5, y = 5;
    compare(x, y);
}

At the moment after the process has executed line 7 of the above example, the contents of memory contain x and y.
At the moment after the process enters the compare function on line 2 the contents of memory contain x, y, left, and right, where left is an exact copy of x, and right is an exact copy of y.
So it makes sense that you should be able to name these variables differently, as they aren't the exact same variable.

There are situations where you would need to name variables differently, such as in a class constructor.
Example:
1
2
3
4
5
6
7
8
9
10
class Rectangle
{
    Rectangle(int w, int h) { //<-- these variable names need to be different to
        width = w;                 //avoid naming conflicts with existing variables.
        height = h;
    }

private:
    int width, height;
}


I hope I helped clear some of that up for you.

EDIT:
Also, functions are an encapsulation of some useful procedure. They're not always meant to be used in the same exact way every time.
For example, the compare function. The two variables you want to compare aren't always going to be named "left," and "right" inside main, but the compare function is useful nonetheless.
Last edited on
Topic archived. No new replies allowed.