Functions - Parameter Passing and Return Values - PowerPoint PPT Presentation

About This Presentation
Title:

Functions - Parameter Passing and Return Values

Description:

Title: PowerPoint Presentation Last modified by: Dragan Created Date: 1/1/1601 12:00:00 AM Document presentation format: On-screen Show Other titles – PowerPoint PPT presentation

Number of Views:74
Avg rating:3.0/5.0
Slides: 10
Provided by: csKentEd94
Learn more at: https://www.cs.kent.edu
Category:

less

Transcript and Presenter's Notes

Title: Functions - Parameter Passing and Return Values


1
Functions - Parameter Passing and Return Values
CS 10061 Introduction to Computer Programming
2
Parameter Passing
  • Terminology
  • actual parameters the parameters in the
    function call, also called arguments
  • formal parameters the parameters the function
    definition
  • Positional parameter passing is used by C. The
    value of the first actual parameter is given the
    first formal parameter, the value of the second
    actual parameter is given the second formal
    parameter, ...
  • In C there are two methods for parameter
    passing and an important variation.

3
Methods for Parameter Passing - Main Methods
Example declaration
Method
Explanation
1. Call by value (one of two main methods) void f(int) The formal parameter is separate, distinct variable and receives a copy of the value of the corresponding actual parameter. Changes to the formal parameter do not affect the value of the actual parameter. C uses call by value by default.
2. Call by reference (one of two main methods) void f(int) The formal parameter is alias of the corresponding actual parameter, it refers to the same memory location. Changes to the formal parameter's value changes the actual parameter's value. denotes call by reference. The actual parameter must be a variable.
3. Call by const reference (important variation of 2.) void f(const int) Like call by reference however the formal parameter's value cannot be changed and thus the actual parameter's value cannot be changed. A compiler error will result if the attempt is made.
4
Parameter Passing Example
  • The code for each is placed side by side for
    easier comparison.
  • Execute and draw a picture.
  • Note that the same call would be used for each
    function definition below, the only way to tell
    which is being used is to look at the function
    definition (or consult documentation for the
    function).

5
Parameter Passing Example - Diagram
6
Methods for Parameter Passing Compared
  • The table below contrasts the first three
    parameter passing schemes. Passing values by
    const reference is important because it avoids
    the overhead that would be required to copy a
    large item like a vector (usually) but has the
    safety of call by value, the original is
    protected from change.

Call By Copy Made? Change Actual Parameter?
Value yes no
Reference no yes
const reference no no
7
Function Return
  • The return keyword causes execution of the
    function to cease and execution to resume in the
    calling function.
  • return may be followed by a value, this becomes
    the result of the function call.
  • The same methods are available for returning
    values as are available for parameter passing.
  • We will mainly use the first method for now, the
    others will be used later or in Data Structures.

8
Methods for Returning Values
Example declaration
Method
Explanation
1. Return by value int f() The returned value is a separate, distinct object which has a copy of the value of the expression in the return statement. C uses return by value by default.
2. Return by reference int f() An alias of the object in the return statement is returned. Never return a reference to a local variable!!
3. Nothing is returned void f() return with no value may be used.
9
Functions and Result and Side Effect
  • Any function can have a result simply by
    returning a value.

Examples
  • functions_variables.cpp
  • functions_parameters.cpp
Write a Comment
User Comments (0)
About PowerShow.com