Terminology

I'm having trouble. I was looking at a YouTube video tutorial about passing parameters by value (or by reference). To make this short, I was wondering what it means to pass a parameter? Additionally, why, when this program runs, does the values come out to be 5 instead of 6? Is this because the incrementation of 5 to 6 is ignored because the variable is local in the "void value(int)" function? I'm so confused. Thanks for the help, in advance.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;

// Start with function prototypes:
void value(int); // Passes parameter by value. LINE 7

int main(){
	
	int num1 = 5;

	value (num1);

	cout << num1 << endl;

	return 0;

}

void value(int num1){ // Why does the variable num1 need to be put here?
	num1 += 5;
}
So in this example you are passing by value, which means that you're passing the "value" of the variable to a function, then the function creates a new variable with the same value you passed it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int x = 
int main(){
	
	int num1 = 5;

	value (num1);

	cout << num1 << endl;

	return 0;

}

void value(int number){
	number += 5;
}

Is sort of like saying
1
2
3
4
5
6
7
int main() {
    int num1 = 5;
    int number = num1;
    number += 5;
    cout << num1;          // Will display 5
    cout << number;      // Will display 10
}
Last edited on
You don't really pass parameters. You pass arguments, to parameters. :) I know the difference might be a bit confusing, but there is a difference. Arguments are what is inside the parentheses of a function call. Parameters are what are within the parentheses of function declarations and definitions.

What happens when you pass by value is the function actually creates a copy of the value passed to it. That copy is the one that the function acts upon. The only way to get that value out of the function is to return it's value (or pass the object to the function by reference or a poiniter, both of which refer to the original object and not the copy.) In your function num1 is local to the function itself. Even though it has the same name as the one inside main(), the one in main is hidden by the one in value(). You are actually working on value's num1 variable. Once that function is finished its num1 goes out of scope, is destroyed, and the value it holds lost. After that, you output the num1 variable contained inside main(), which has had nothing done to it.

Passing by reference is a means to get around this and cause the function to work on the original object instead of a copy (it also reduces the cpu and memory overhead of having to copy the argument to the parameter.) A reference is also sometimes called an alias and does what that name implies; it is simply another way to refer to an object. Consider this code:

1
2
int var = 0;
int& varref = var;


Now varref is an alias, or reference, to var. Any operation you perform on varref will actually be performed on the value that var holds. The same holds true when you pass arguments to a function by reference. In you case, if you wanted your function to operate as it sounds like you want it to, you would declare your function like this:

void value(int&);

and your function definition would look like:

1
2
3
4
void value (int& value)   // I renamed the parameter to something different. This helps distinguish it from the num1 variable declared in main() and is good practice generally.
{
    value += 5;
}


Now I know what you are thinking, prolly. value is a completely different variable, so how can altering it do anything worthwhile? The thing is, it's NOT a completely different variable, it's a reference to num1. So, anything you do to value is actually performed on num1.

Passing by reference (and also passing pointers) is the only way a function can be used to output or alter more then one object (well, obviously, if the object is a class object, more then 1 piece of data can be manipulated since the class probably holds multiple data members, but you still return the class object itself usually, and that IS a single object.) And, like I said, it cuts down on alot of unneeded copying. Unfortunately, it can also lead to alteration of data that you didn't intend to alter, so if you pass by reference and you don't want to alter the objects passed it's best to pass by constant reference.

Lastly, in your function, num1 does not NEED to be put there. The only place you have to put it to pass it to the function is in the function call. What you are doing in the function declaration is simply telling the compiler that there will be a function definition somewhere that return void, is called value, and takes an argument of type int. The parameters are actually DECLARED in the function definition. You can name these variables, or objects, anything you wish within standard C++ naming rules (IE, no keywords, can't have leading numerals, can only contain letters, numbers and underscores, etc.) Like I said in my comment it is often better to name the parameters to something different so the code doesn't become confusing or intent of the function obfuscated.
Last edited on
I love you guys so much. Very thorough post, Raezzor. Thanks a million.
Topic archived. No new replies allowed.