4-Parameters and Overloading

Embed Size (px)

Citation preview

  • 8/3/2019 4-Parameters and Overloading

    1/40

    Lesson 1

  • 8/3/2019 4-Parameters and Overloading

    2/40

  • 8/3/2019 4-Parameters and Overloading

    3/40

    Contents Parameters Call-by-value parameters

    Call-by-reference parameters

    Scope of identifiers

    Functions with default parameters

    Function Overloading

  • 8/3/2019 4-Parameters and Overloading

    4/40

    PARAMETER

    a special kind of variable, used ina subroutine to refer to one of

    the pieces of data provided asinput to the subroutine

    These pieces of data are calledarguments

  • 8/3/2019 4-Parameters and Overloading

    5/40

    Parameters and Overloading

    Two methods of passing arguments as

    parameters:

    1. Call-by-value - copy of value is passed

    2. Call-by-reference means the address

    of actual argument is passed

  • 8/3/2019 4-Parameters and Overloading

    6/40

    When an argument (local variable) is passed by value, a copyof the variables value is sent toand is assigned tothe

    receiving functions parameter.

  • 8/3/2019 4-Parameters and Overloading

    7/40

    Call-by-value/copy

    (Passing by Value)

    If more than one variable is passed by value,

    a copy of each of their values is sent toand is assigned tothe receiving functions parameters.

  • 8/3/2019 4-Parameters and Overloading

    8/40

    Sample:

  • 8/3/2019 4-Parameters and Overloading

    9/40

    Suppose that we called our first function addition usingthe following code:

    What we did in this case was to call to

    function addition passing the values of x

    and y, i.e. 5 and 3 respectively, but not thevariables x and y themselves.

  • 8/3/2019 4-Parameters and Overloading

    10/40

    When the function addition is called, the value of itslocal variables a and b become 5 and 3 respectively,

    but anymodification to either a or b within thefunction additionwill not have any effect in the valuesof x and y outside it, because variables x and y were notthemselves passed to the function, but only copies oftheir values at the moment the function was called.

  • 8/3/2019 4-Parameters and Overloading

    11/40

    Sample Problem:

    The following program asks users for their weight. Itthen passes that weight to a function that

    calculates the equivalent weight on the moon bydividing the weight by six.

    Notice the second function uses the passed value,and calculates with it. After weight is passed to the

    second function, that function can treat weight asthough it were a local variable.

  • 8/3/2019 4-Parameters and Overloading

    12/40

    Solution:#include

    moon(int weight); // Function Prototypes

    main(){

    int weight; // main()s local weight.

    cout > weight;

    moon(weight); // Call the moon() function and pass it the weight.return 0; // Return to the operating system.

    }

    moon(int weight) // Declare the passed parameter.

    { // Moon weights are 1/6thearthsweightsweight /= 6; // Divide the weight by six.

    cout

  • 8/3/2019 4-Parameters and Overloading

    13/40

  • 8/3/2019 4-Parameters and Overloading

    14/40

    BUT .

    there might be some cases whereyou need to manipulate from inside

    a function the value of an external

    variable. For that purpose we canuse arguments passed by reference

  • 8/3/2019 4-Parameters and Overloading

    15/40

    When you pass an argument (local variable) by address,

    the variables address is sent toand is assigned tothe

    receiving functions parameter. (If you pass more than

    one variable by address, each of their addresses is sent

    toand is assigned tothe receiving functions

    parameters.)

  • 8/3/2019 4-Parameters and Overloading

    16/40

    Sample:

    The first thing that should call your attention is

    that in the declaration of duplicate the type ofeach parameter was followed by an ampersand

    sign (&). This ampersand is what specifies that

    their corresponding arguments are to be passed

    by reference instead ofby value

  • 8/3/2019 4-Parameters and Overloading

    17/40

    Sample:

    When a variable is passed by reference we are

    not passing a copy of its value, but we aresomehow passing the variable itself to thefunction and any modification that we do tothe local variables will have an effect in theircounterpart variables passed as arguments in

    the call to the function.

  • 8/3/2019 4-Parameters and Overloading

    18/40

    To explain it in another way, we associate a, b and cwith the arguments passed on the function call (x, y

    and z) and any change that we do on a within the

    function will affect the value of x outside it. Any

    change that we do on b will affect y, and the samewith c and z.

  • 8/3/2019 4-Parameters and Overloading

    19/40

    Sample with output:

    Output:

  • 8/3/2019 4-Parameters and Overloading

    20/40

    WITHOUT THE AMPERSAND SIGNS

    (&), we would have not passed the

    variables by reference, but a copy of

    their values instead, and therefore,the output on screen of our program

    would have been the values of x, y

    and z without having been modified.

  • 8/3/2019 4-Parameters and Overloading

    21/40

    Passing by reference is also an

    effective way to allow afunction to return more than

    one value. For example, here is

    a function that returns the

    previous and next numbers of

    the first parameter passed.

  • 8/3/2019 4-Parameters and Overloading

    22/40

    Output:

    a function that returns more than one value.

  • 8/3/2019 4-Parameters and Overloading

    23/40

  • 8/3/2019 4-Parameters and Overloading

    24/40

    Types of Variables(Identifier)

    Local Variable

    Global Variable

  • 8/3/2019 4-Parameters and Overloading

    25/40

    GLOBAL VARIABLE

    a variable declared in

    the main body of thesource code, outside all

    functions

  • 8/3/2019 4-Parameters and Overloading

    26/40

    LOCAL VARIABLE

    a variable declared

    within the body of afunction or a block

  • 8/3/2019 4-Parameters and Overloading

    27/40

    SCOPE

    is an enclosing context where values and

    expressions are associated

  • 8/3/2019 4-Parameters and Overloading

    28/40

    Global variables can bereferred from anywherein the code, even inside

    functions, whenever it isafter its declaration.

  • 8/3/2019 4-Parameters and Overloading

    29/40

    The scope of local variables is limited to the block enclosed in braces ({}) where

    they are declared. For example, if they are declared at the beginning of thebody of a function (like in function main) their scope is between its declaration

    point and the end of that function. In the example above, this means that if

    another function existed in addition to main, the local variables declared in

    main could not be accessed from the other function and vice versa.

  • 8/3/2019 4-Parameters and Overloading

    30/40

    Advantages and Disadvantages Global variables can be dangerous because code can

    inadvertently overwrite a variable initialized elsewherein the program. It is better to make every variable localin your programs. Then, only functions that should be

    able to change the variables can do so.

    If a function defines a variable as local, thatvariablesscope is protected. The variable cannot be used,

    changed, or erased by any other function withoutspecial programming that you learn about shortly.

  • 8/3/2019 4-Parameters and Overloading

    31/40

  • 8/3/2019 4-Parameters and Overloading

    32/40

    When declaring a function we canspecify a default value for each of the

    last parameters

    This value will be used if the

    corresponding argument is left blank when

    calling to the function.

    If a value for that parameter

    is not passed when thefunction is called, the

    default value is used, but if avalue is specified this

    default value is ignored andthe passed value is used

    instead

  • 8/3/2019 4-Parameters and Overloading

    33/40

    As we can see in the body of the program there are two

    calls to function divide:

    we have only specified oneargument, but the function

    divide allows up to two. Sothe function divide has

    assumed that the secondparameter is 2 since that iswhat we have specified to

    happen if this parameter wasnot passed (notice thefunction declaration, which

    finishes with int b=2, not justint b). Therefore the result ofthis function call is 6 (12/2).

    there are twoparameters, so the

    default value for b (intb=2) is ignored and b

    takes the value passed as

    argument, that is 4,making the result

    returned equal to 5(20/4).

  • 8/3/2019 4-Parameters and Overloading

    34/40

    Output:

  • 8/3/2019 4-Parameters and Overloading

    35/40

  • 8/3/2019 4-Parameters and Overloading

    36/40

  • 8/3/2019 4-Parameters and Overloading

    37/40

    In C++ two different functions can havethe same name if their parameter types

    or number are different. That means

    that you can give the same name tomore than one function if they have

    either a different number of parametersor different types in their parameters.

    E l h d fi d t f ti

  • 8/3/2019 4-Parameters and Overloading

    38/40

    Example: we have defined two functionswith the same name, operate

    one of them accepts two parameters

    of type int

    accepts them of type float

    How thecompiler

    will identify

    which oneto call?

    By examining the types passed as

    arguments when the function is called

    E ample

  • 8/3/2019 4-Parameters and Overloading

    39/40

    Example:

    The behavior of a call

    to operate dependson the type of the

    arguments passed

    because the functionhas been overloaded.

    Output:

  • 8/3/2019 4-Parameters and Overloading

    40/40

    End