08 Functions and Parameters Passing.pptx - Topic 8 Functions and Parameter-Passing 1 Parameter Passing Pass-by-Value When a function is called the value

08 Functions and Parameters Passing.pptx - Topic 8...

This preview shows page 1 - 14 out of 48 pages.

Topic 8 Functions and Parameter-Passing 1
Image of page 1
Parameter Passing: Pass-by-Value When a function is called, the value of the actual parameter is copied to the formal parameter. This method of parameter-passing is called pass by value . 2
Image of page 2
Parameter Passing: Pass-by-Value Example – void function with parameter : display_rboxed(135.68) ; Function call with actual parameter value 135.68 Function definition with formal parameter rnum void display_rboxed( double rnum ) { cout << "**********\n"; cout << "* *\n"; cout << "* " << fixed << setprecision(2) << setw(6) << rnum << " *\n"; cout << "* *\n"; cout << "**********\n"; } 3
Image of page 3
Parameter Passing: Pass-by-Value Example – void function with parameter : display_rboxed(135.68) ; 135.68 rnum gets copy of the actual parameter value 135.68 rnum void display_rboxed( double rnum ) { cout << "**********\n"; cout << "* *\n"; cout << "* " << fixed << setprecision(2) << setw(6) << rnum << " *\n"; cout << "* *\n"; cout << "**********\n"; } 4
Image of page 4
Parameter Passing: Pass-by-Value Example – non-void function with parameter : double radius = 10.0; double area = compute_area(radius); double compute_area (double r) { return (PI * pow(r,2)); } Function call with actual parameter radius Function definition with formal parameter r 5
Image of page 5
Parameter Passing: Pass-by-Value Example – non-void function with parameter : radius = 10.0; area = compute_area(radius); double compute_area (double r) { return (PI * pow(r,2)); } 10.0 10.0 r r gets copy of actual parameter value 6 double radius = 10.0; double area = compute_area(radius);
Image of page 6
Parameter Passing: Pass-by-Value Example – non-void function with parameter : double compute_area (double r) { return (PI * pow(r,2)); } Result returned to calling function 7 double radius = 10.0; double area = compute_area(radius);
Image of page 7
Functions and Results A function may have a return value but no parameters. For example, a function that accepts a data value from the user and passes back the value to the calling function. 8
Image of page 8
Functions and Results Example – non-void function without parameters : double radius = get_radius(); Result returned to calling function double get_radius () { double radius; cout << "Enter radius: "; cin >> radius; return radius; } 9
Image of page 9
Functions and ResultsSuppose we want a function to accept some data values from the user and pass back the data values to the calling function.Remember the return statement can be used to pass back onlyone value. How we make a function pass back more than one value? 10
Image of page 10
Functions, Results, and Side-Effects We do this by using the concept of side-effect. A side-effect is an action that causes a change in the state of the program. Actions that cause side-effects include: Sending data out of the program e.g. displaying it on the screen. Accepting data from outside the program. Changing the value of a variable in the calling function. 11
Image of page 11
Functions, Results, and Side-Effects Function Return Value Side-Effect display_rboxed None Sends data out of the program i.e. to the screen compute_area Yes, area None get_radius Yes, radius Accepts data from outside the program i.e. from the keyboard A function may have a return value, a side effect, or both. 12
Image of page 12
Functions, Results, and Side-Effects How do we cause the side-effect of changing the values of variables in the calling function? Example: suppose we have two variables in main – a and b. We want a function to change these values. 13
Image of page 13
Image of page 14

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture