1 / 10

PASSING PARAMETERS

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

zhen
Download Presentation

PASSING PARAMETERS

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. PASSING PARAMETERS

  2. 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

  3. 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; }

  4. 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”); }

  5. 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”); }

  6. Parameter Passing (by Value) The Numbers are: Number 1 is : 10 Number 2 is : 20 Number 3 is : 30 Screen

  7. 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; }

  8. 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”); }

  9. 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

  10. 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

More Related