Home » » Passing by Value

Passing by Value

Written By Unknown on Minggu, 22 Januari 2012 | 04.26

Lab RPL .
When dealing with function parameters, there are two ways of passing variables or other values. one ways, and the most common, is called passing by value. This will give a Copy of the value to the function and the original Value will NOT CHANGE IN THE PROGRAM. The below example shows a simple adding function that tries to change a value but does not.

Example 2 :
Value Example

#include <iostream.h>
int add (int a, int b)
{
int sum = a + b;
a = 7
     return sum;
}
int main ( )
{
int n1 = 5 , n2 = 6;
cout<<add(ni,n2) << endl;
cout<< ni <<n2;
return 0
}

This program will pass the variables n1 and n2 to the function add (...) and return the sum of thoser twoo numbers.
For this program, the integer parameter a is a copy of the variable n1 while the integer parameter b is a copy of the variabel n2. when the add (...) function is called , the value of n1 DOES NOT CHANGE because of the passing by Value.

0 komentar:

Posting Komentar