Ch2part3

  • Upload
    sam-zee

  • View
    219

  • Download
    0

Embed Size (px)

Citation preview

  • 8/12/2019 Ch2part3

    1/33

    1

    Chapter 2:

    C++ FUNDAMENTALS(Part 3)

  • 8/12/2019 Ch2part3

    2/33

    DCS5088 :: Chapter 2 (part 3) 2

    Objectives

    At the end of this lecture, students should

    be able to :

    Create functions and implement themcall by value

    call by reference (pointers)

    call by reference (reference arguments) Understand variables scope

  • 8/12/2019 Ch2part3

    3/33

    DCS5088 :: Chapter 2 (part 3) 3

    2.15 Functions

    Modules

    The C++ library- a rich collection of functions and classes

    Programmer defined functions, classes

    A function invoked by a function call.

    The function call specifies the function nameand

    provides informationthat the call function needs to do

    its job.

    Funct ionsallow the programmer to modular izea

    program.

  • 8/12/2019 Ch2part3

    4/33

    DCS5088 :: Chapter 2 (part 3) 4

    2.15.1 Analogy

    Boss to worker analogy

    A boss (the calling function or caller) asks a

    worker (the called function) to perform a taskand return (i.e., report back) the results

    when the task is done.

  • 8/12/2019 Ch2part3

    5/33

    DCS5088 :: Chapter 2 (part 3) 5

    2.16 Math Library Functions

    Perform common mathematical calculations Include the header file

    Functions called by writing

    functionName (argument);

    or functionName(argument1, argument2, );

    Example

    cout

  • 8/12/2019 Ch2part3

    6/33

    DCS5088 :: Chapter 2 (part 3) 6

    2.17 Functions: 3 things to include in a

    program

    Function definition / called function

    Function prototype

    Function call / invocation or callingfunction

  • 8/12/2019 Ch2part3

    7/33

    DCS5088 :: Chapter 2 (part 3) 7

    #include

    using namespace std;// function prototypes here

    .

    void main(){ .

    //function call

    ..

    }

    //function definition

  • 8/12/2019 Ch2part3

    8/33

    DCS5088 :: Chapter 2 (part 3) 8

    2.17.1 Function Definition

    Below is the format:Return-value-type function-name (parameter-list)

    {

    declarations and statements}

    Parameter list may be empty or have 1 or

    more variablesVariables in the parameter list have a local scope.

  • 8/12/2019 Ch2part3

    9/33

    DCS5088 :: Chapter 2 (part 3) 9

    Function Def. Example 1

    Create a function that accepts 2 integers and return

    the total as an integer value

    intfunc1(int n1, int n2){ int total;

    total = n1 + n2;

    return total;

    }

    How many

    localvariables are

    there?

  • 8/12/2019 Ch2part3

    10/33

    DCS5088 :: Chapter 2 (part 3) 10

    Function Def. Example 2

    Create a function that accepts 2 integers and

    displays the total result

    voidfunc2(int n1, int n2)

    { int total;

    total = n1 + n2;

    cout

  • 8/12/2019 Ch2part3

    11/33

    DCS5088 :: Chapter 2 (part 3) 11

    Function Def. Example 3

    Create a function that gets an integer number from

    user and returns the number

    intfunc3( ){ int num;

    cin>>num;

    return num;

    }

  • 8/12/2019 Ch2part3

    12/33

    DCS5088 :: Chapter 2 (part 3) 12

    Function Def. Example 4

    Create a function that gets an integer number from

    user and displays the square of the number

    void func4( ){ int num, result;

    cin>>num;

    result = num * num;

    cout

  • 8/12/2019 Ch2part3

    13/33

    DCS5088 :: Chapter 2 (part 3) 13

    2.17.2 Function prototypes

    A function prototypetells the compiler the

    the nameof a funct ion,

    the typeof the datareturned,

    the numberof parameters,

    the typeof those parameters,

    the orderin which the parameterof those types

    are expected.

    The compiler use function prototype to validate

    functioncalls.

  • 8/12/2019 Ch2part3

    14/33

    DCS5088 :: Chapter 2 (part 3) 14

    Function Prototype. Example 1

    Given the function definition:

    intfunc1(int n1, int n2)

    { int total;total = n1 + n2;

    return total;

    }

    So, the function prototype:

    int func1(int, int);

  • 8/12/2019 Ch2part3

    15/33

    DCS5088 :: Chapter 2 (part 3) 15

    Function Prototype. Example 2

    Given the function definition:intfunc3( )

    { int num;

    cin>>num;

    return num;

    }

    So, the function prototype:int func3();

  • 8/12/2019 Ch2part3

    16/33

    DCS5088 :: Chapter 2 (part 3) 16

    2.17.3 Function call

    Function that returns a value to the caller.

    need to assign the value returned by the function to avariable on the left

    Example:

    val = func1(a, b);

    n1 = calculate (100, 2);

    Function that does not return any value to the caller

    simple

    Example:

    func4( );

    display(199.59);

  • 8/12/2019 Ch2part3

    17/33

    DCS5088 :: Chapter 2 (part 3) 17

    2.18 Methods of Data accessibility for

    functions

    Call by value

    Call by reference using pointers

    Call by reference using referenceparameters

    Global variables

  • 8/12/2019 Ch2part3

    18/33

    DCS5088 :: Chapter 2 (part 3) 18

    Call by Value

    When the data is passed to a function,

    the value of the variable is copied into the

    input variable parameters. Called function has own copies of data

    passed from calling function

  • 8/12/2019 Ch2part3

    19/33

    DCS5088 :: Chapter 2 (part 3) 19

    Full program Example 1

    Create a function that accepts 2

    arguments from the main() and returns

    the total to main() In the main, get user inputs for 2 integers

    and call the function to return the total.

    Display the total

  • 8/12/2019 Ch2part3

    20/33

    DCS5088 :: Chapter 2 (part 3)20

    #includeusing namespace std;int calc(int, int);

    int main(){ int n1,n2, total;

    coutn1>>n2;

    total = calc(n1, n2);

    cout

  • 8/12/2019 Ch2part3

    21/33

    DCS5088 :: Chapter 2 (part 3) 21

    Output:

  • 8/12/2019 Ch2part3

    22/33

    DCS5088 :: Chapter 2 (part 3) 22

    Full program Example 2

    Create a function that accepts 2

    arguments from the main() and displays

    the total result In the main, get user inputs for 2 integers

    and call the function to show the total.

  • 8/12/2019 Ch2part3

    23/33

    DCS5088 :: Chapter 2 (part 3)23

    #includeusing namespace std;void calc(int, int);

    int main(){ int n1,n2;

    coutn1>>n2;

    calc(n1, n2);

    return 0;}

    void calc(int a, int b){

    int total;total = a + b;cout

  • 8/12/2019 Ch2part3

    24/33

    DCS5088 :: Chapter 2 (part 3) 24

    Output:

    Still same as example 1 but differentimplementation

  • 8/12/2019 Ch2part3

    25/33

    DCS5088 :: Chapter 2 (part 3) 25

    2.18 Methods of Data accessibility for

    functions

    Call by value

    Call by reference using pointers

    Call by reference using referenceparameters

    Global variables

  • 8/12/2019 Ch2part3

    26/33

    DCS5088 :: Chapter 2 (part 3) 26

    Call by Reference (using pointers)

    The address of the data variable is passed

    from the calling function(from main) to a called

    function. The address is copied into a pointer

    variable that belongs to the called function.

    Called function does not have its own local

    copy of the variable.

    The indirection operator(*) is used with thepointer to access the calling functions data.

  • 8/12/2019 Ch2part3

    27/33

    DCS5088 :: Chapter 2 (part 3) 27

    Example 1

    Create a function that accepts 3 arguments

    from the main() through pointers. The first two

    pointers refers to the integers from main and

    the third pointer refers to the result variable.The function will set the result variable to the

    addition of the 2 integers at main().

    In the main, get user inputs for 2 integers and

    call the function. After that show the total.

  • 8/12/2019 Ch2part3

    28/33

    DCS5088 :: Chapter 2 (part 3)

    28

    #includeusing namespace std;void calc(int*, int*, int*);

    int main(){ int n1, n2, total;coutn1>>n2;

    calc(&n1,&n2, &total);cout

  • 8/12/2019 Ch2part3

    29/33

    DCS5088 :: Chapter 2 (part 3) 29

    Call by Reference (using

    reference arguments) Another technique for passing the variables address to

    a function

    A reference parameter is an address. Reference

    parameters employ the reference operator (&) in thefunction declaration. The reference variables in called

    function are implicit pointers

    The variable name is used in the call statement(from

    main) but actually the address of the variable is passed

    to the called functions parameters.

  • 8/12/2019 Ch2part3

    30/33

    DCS5088 :: Chapter 2 (part 3) 30

    Example 1

    Create a function that accepts 3 arguments

    from the main() through reference arguments.

    The first two reference argument refers to the

    integers from main and the third referencerefers to the result variable. The function will

    set the result variable to the addition of the 2

    integers at main().

    In the main, get user inputs for 2 integers and

    call the function. After that show the total.

  • 8/12/2019 Ch2part3

    31/33

    DCS5088 :: Chapter 2 (part 3)

    31

    #includeusing namespace std;void calc(int&, int&, int&);

    int main(){ int n1, n2, total;coutn1>>n2;

    calc(n1,n2,total);cout

  • 8/12/2019 Ch2part3

    32/33

    DCS5088 :: Chapter 2 (part 3) 32

    2.19 Variable Scope

    WHERE you declare a variable

    Local

    A local variable is a variable that is declared

    within a function, or within any block of code.

    Global

    A variable declared outside of any function

    can be used in any function

  • 8/12/2019 Ch2part3

    33/33

    DCS5088 :: Chapter 2 (part 3) 33

    2.20 Static Variable

    A local variable that retains its value in a

    function that can be called a number of

    times till the entire program terminates.

    They are still known only in the function in

    which they are declared,

    Retain their values when the function

    returns to its caller.