Pass By Value C++

Here is the code for understanding Pass By Value...
copy the whole code and run in DEV C++ its easy.....
I am a student programmer..please point out mistakes and my weak points in programming..thanks eveyone
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include<iostream>

using namespace std;
void add(int,int);

main()
{
	int n1 = 5, n2 = 6,n3;
	
	 cout<<"n1 and n2 in Main function before= "<<n1<<" "<<n2<<endl<<endl;
	add(n1,n2);
	
	cout<<"Control in main function"<<endl;
   cout<<"n1 and n2 in Main function after= "<<n1<<" "<<n2<<endl<<endl;
	cout<<"NOTE HERE, EVEN THOUGH WE CHANGED VALUE FOR 'N1' IN ADD FUNCTION,";
	cout<<"   BUT IT WON'T REFLECT IN MAIN FUNCTION BECAUSE";
	cout<<" WE ARE PASSING ONLY THE COPIES OF THE VALUES OF N1 AND N2 ";
	cout<<" NOT THE VARIABLES AS N1 AND N2 OR THEIR MEMORY ADDRESSES!";
    n3=n1+n2;
	cout<<endl<<endl<<endl;
	
	cout<<"n1+n2 in main function= "<<n3<<endl<<endl;
	
    system("pause");
}
void add(int n1, int n2)
{
     cout<<"Control in add function"<<endl;
    int sum;
    cout<<"n1 and n2 before ="<<n1<<" "<<n2<<endl;
    sum=n1+n2;
    cout<<"sum before ="<<sum<<endl;
      n1=7;
      cout<<"value changed for n1 as "<<n1<<endl;
    cout<<"n1 and n2 after ="<<n1<<" "<<n2<<endl;
    sum=n1+n2;
    cout<<"sum after ="<<sum<<endl<<endl;
  
    
    
}
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include<iostream>

using namespace std;

void add(int &n1, int &n2)    
{
	cout<<"Control in add function"<<endl;
    int sum;
    cout<<"n1 and n2 before ="<<n1<<" "<<n2<<endl;
    sum=n1+n2;
    cout<<"sum before ="<<sum<<endl;
	n1=7;
	cout<<"value changed for n1 as "<<n1<<endl;
    cout<<"n1 and n2 after ="<<n1<<" "<<n2<<endl;
    sum=n1+n2;
    cout<<"sum after ="<<sum<<endl<<endl;
  
}

int main()
{
	int n1 = 5, n2 = 6,n3;
	
	cout<<"n1 and n2 in Main function before= "<<n1<<" "<<n2<<endl;
	add(n1,n2);
	
	cout<<"Control in main function"<<endl;
	cout<<"n1 and n2 in Main function after= "<<n1<<" "<<n2<<endl;
	cout<<"NOTE HERE, EVEN THOUGH WE CHANGED VALUE FOR 'N1' IN ADD FUNCTION,";
	cout<<"   BUT IT WON'T REFLECT IN MAIN FUNCTION BECAUSE";
	cout<<" WE ARE PASSING ONLY THE COPIES OF THE VALUES OF N1 AND N2 ";
	cout<<" NOT THE VARIABLES AS N1 AND N2 OR THEIR MEMORY ADDRESSES!";
    n3=n1+n2;
	cout<<endl<<endl<<endl;
	
	cout<<"n1+n2 in main function= "<<n3<<endl<<endl;
	
    system("pause");
	return 0;
}

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include<iostream>

using namespace std;

void add(int *n1, int *n2)
{
	cout<<"Control in add function"<<endl;
    int sum;
    cout<<"n1 and n2 before ="<<n1<<" "<<n2<<endl;
    sum=*n1+*n2;
    cout<<"sum before ="<<sum<<endl;
	*n1=7;
	cout<<"value changed for n1 as "<<n1<<endl;
    cout<<"n1 and n2 after ="<<n1<<" "<<n2<<endl;
    sum=*n1+*n2;
    cout<<"sum after ="<<sum<<endl<<endl;
  
}

int main()
{
	int n1 = 5, n2 = 6,n3;
	
	cout<<"n1 and n2 in Main function before= "<<n1<<" "<<n2<<endl;
	add(&n1,&n2);
	
	cout<<"Control in main function"<<endl;
	cout<<"n1 and n2 in Main function after= "<<n1<<" "<<n2<<endl;
	cout<<"NOTE HERE, EVEN THOUGH WE CHANGED VALUE FOR 'N1' IN ADD FUNCTION,";
	cout<<"   BUT IT WON'T REFLECT IN MAIN FUNCTION BECAUSE";
	cout<<" WE ARE PASSING ONLY THE COPIES OF THE VALUES OF N1 AND N2 ";
	cout<<" NOT THE VARIABLES AS N1 AND N2 OR THEIR MEMORY ADDRESSES!";
    n3=n1+n2;
	cout<<endl<<endl<<endl;
	
	cout<<"n1+n2 in main function= "<<n3<<endl<<endl;
	
    system("pause");
	return 0;
}
hey dangerous....when you changed the concept to pass by reference then why didn't you change the mechanism explained in CAPS letters.. It may confuse new users of c++.
Thanks for reply, but keep track on those minor mistakes....
Actually, passing by reference is way easier to understand like this:

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>

using namespace std;

void read_Matrix(int Matrix[100][100], int &num_Lines, int &num_Columns);
void print_Matrix(int Matrix[100][100], int num_Lines, int num_Columns);

int main(int argc, char *argv[])
{
    int Matrix[100][100];
    int nLines = 0;
    int nColumns = 0;

    read_Matrix(Matrix,nLines,nColumns); // Variables lines, columns are sent by reference which means that before calling the function, their values are 0, 0 and after calling them, the function will return their new values in the main function.
    print_Matrix(Matrix,nLines,nColumns); // Variables lines, columns are already initialized and are sent by value to the function, their values will remain unchanged.

    return 0;
}

void read_Matrix(int Matrix[100][100], int &num_Lines, int &num_Columns)
{
    cin >> num_Lines >> num_Columns;

    for (int i = 0; i < num_Lines; i++)
    {
        for (int j = 0; j < num_Columns; j++)
        {
            cin >> Matrix[i][j];
        }
    }
}

void print_Matrix(int Matrix[100][100], int num_Lines, int num_Columns)
{
    for (int i = 0; i < num_Lines; i++)
    {
        for (int j = 0; j < num_Columns; j++)
        {
            cout << Matrix[i][j] << " ";
        }
        cout << "\n";
    }
}


Best of wishes,
~ Raul ~
Last edited on
Topic archived. No new replies allowed.