130 likes | 378 Views
PASSING PARAMETERS. Parameter Passing (by Value). Parameters Formal Parameters – parameters listed in the header of the function Variables used within the function to perform a task Actual Parameters - parameters listed in the function call
E N D
Parameter Passing (by Value) Parameters • Formal Parameters – parameters listed in the header of the function • Variables used within the function to perform a task • Actual Parameters - parameters listed in the function call • Actual data being passed to the function for use within the function
Formal Parameters Parameter Passing (by Value) void print_nums (int num1, int num2, int num3); { cout << endl; cout << “Number 1 is : “ << num1 << endl; cout << “Number 2 is : “ << num2 << endl; cout << “Number 3 is : “ << num3 << endl; }
Actual Parameters Parameter Passing (by value) int main( ) { number_1 = 10; number_2 = 20; number_3 = 30; cout << “The Numbers are: “ << endl; print_nums ( number_1, number_2, number_3 ); cout << system(“Pause”); }
number_3 num1 num2 num3 num3 number_2 num1 num2 number_1 30 10 20 30 10 20 Parameter Passing (by Value) void print_nums ( int num1, int num2, int num3) { Number_1 = 10; Number_2 = 20; Number_3 = 30; print_nums ( number_1, number_2, number_3 ); cout << system(“Pause”); }
Parameter Passing (by Value) The Numbers are: Number 1 is : 10 Number 2 is : 20 Number 3 is : 30 Screen
Variable Parameter Parameter Passing (by Reference) void print_nums ( int num1, int num2, int& num3); { num3 = num1 * num2; cout << endl; cout << “Number 1 is :” << num1 << endl; cout << “Number 2 is :” << num2 << endl; cout << “Number 3 is :” << num3 << endl; }
Actual Parameters Parameter Passing (by Reference) int main( ) { number_1 = 10; number_2 = 20; number_3 = 30; cout << “The Numbers are: “ << endl; cout << number_1 << number_2 << number_3 << endl; print_nums ( number_1, number_2, number_3 ); cout << “The Numbers are: “ << endl; cout << number_1 << number_2 << number_3 << endl; cout << system(“Pause”); }
number_3 num2 num1 number_1 number_3 number_2 number_1 num3 num3 num2 num1 number_2 30 10 20 20 add 408 10 200 20 10 Passing by Reference void print_nums ( int num1, int num2, int& num3) { number_1 = 10; number_2 = 20; number_3 := 30; print_nums ( number_1, number_2, number_3 ); } address 400 404 408
Passing by Reference The Numbers are: 10 20 30 Number 1 is : 10 Number 2 is : 20 Number 3 is : 200 The Numbers are: 10 20 200 Screen