chap 5 - function

Upload: narahiko-fujiwara

Post on 06-Apr-2018

222 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/3/2019 Chap 5 - Function

    1/10

    CHAPTER 5FUNCTIONS

    Predefined functions

    Independent Functions

    Functions data type

    Functions with parameter

    Value

    Reference

  • 8/3/2019 Chap 5 - Function

    2/10

    a mini-program containing algorithms to do a

    specific task

    a function could be invoked (called) many timesthrough its name (to execute its task), by the

    main function or other functions

    when a function is invoked, the computer will

    execute all the instructions in the function from

    start to end, and then it goes to the instruction

    after the function call

    What is a Function?

  • 8/3/2019 Chap 5 - Function

    3/10

    2 types of functions :-

    1. Predefined function or built-in or library

    function2. Independent function or user-defined function

    Function Classification

  • 8/3/2019 Chap 5 - Function

    4/10

    PREDEFINED FUNCTION

    Functions that are ready-made by the producer of a compiler

    and stored in the header files (.h) called libraries

    To use the predefined function, the appropriate library file must

    be included in the preprocessor directive (# include)Common header files are :-

    a) iostream.h b) iomanip.h c) string.h

    d) char.h e) math.h f) stdlib.h

  • 8/3/2019 Chap 5 - Function

    5/10

    Independent Function

    functions written by the programmer

    the way the functions are used (called) is the same

    as library functions

    to use a function, only the prototype and adescription on what the function does is sufficient

    3 requirements to use the independent function :-

    a) Function prototype declaration

    b) Function callc) Function definition

  • 8/3/2019 Chap 5 - Function

    6/10

    Types of Functions1. Function with no return value and no parameter

    Function Prototype Function Call Function Definition

    void display(); { .

    ..

    display();

    ..

    }

    void display()

    {

    cout radius;

    area = radius * radius * 3.142;

    cout

  • 8/3/2019 Chap 5 - Function

    7/10

    2. Function with no return value and has a parameter

    Function Prototype Function Call Function Definition

    void average(int, int); { int x, y;

    cin >> x >> y;

    average(x, y);..

    }

    void average(int a, int b)

    { int avg;

    avg = (a + b)/2

    ;cout

  • 8/3/2019 Chap 5 - Function

    8/10

    3. Function with return value and no parameter

    Function Prototype Function Call Function Definition

    float areaCircle(); { double area;

    area =

    areaCircle();

    cout radius;A = 3.142 * radius * radius;

    return A;

    }

  • 8/3/2019 Chap 5 - Function

    9/10

    4. Function with return value and has a parameter

    Function Prototype Function Call Function Definition

    int factorial(int); { int x, fac;

    cin >> x;

    fac = factorial(x);cout 0){ result = result * a;

    a = a 1;

    }

    return result;

    }

    int average(int, int); { int x, y;

    cin >> x >>y;

    cout

  • 8/3/2019 Chap 5 - Function

    10/10

    Function Prototype Function Call Function Definition

    void swap(int &, int

    &);

    { int x, y;

    cin >> x >>y;

    swap(x, y);

    }

    void swap(int &a, int &b)

    { int temp;

    temp = a;

    a = b;

    b = temp;}

    void calc(int, int, int

    &, float &);

    { int x, y, total;

    float avg;

    cin >> x >>y;

    calc(x, y, total,

    avg);

    }

    void calc(int a, int b, int &jum, float &purata)

    {

    jum = a + b;

    purata = jum/2;

    }

    5. Function with return value and has a reference

    parameter reference parameter used to return values as a return output to

    called function.

    it is used where more than one value or variable need to be returned to

    the called function.