c++ function part 1

Upload: paancute8982

Post on 08-Apr-2018

220 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/6/2019 C++ Function Part 1

    1/41

    1

    BITG 1113:

    Function

    (Part 1)

    LECTURE 6

  • 8/6/2019 C++ Function Part 1

    2/41

    Objectives

    Prototype and function declaration

    The function call and returning value

    Local and global variables

    2

  • 8/6/2019 C++ Function Part 1

    3/41

    Types of Function

    Predefined Function

    User-defined Function

    3

  • 8/6/2019 C++ Function Part 1

    4/41

    Predefined Function

    ` Its programming code is already written.

    ` A programmer only need to know how to use it.

    `Need to include the header file in the program

    exp : #include

    ` Some of the predefined mathematical functions in

    header file cmath are:

    ` The power function, pow(x,y)

    ` Thesquare root function, sqrt(x)

    ` The floor function, floor(x)

    4

  • 8/6/2019 C++ Function Part 1

    5/41

    User-Defined Function

    The name of function is used in three ways : fordeclaration, in a call, and for definition.

    Function declaration is done first with a prototype

    declaration.

    Function definition contains the code to completethe task.

    Function is invoked or called by Functioncall.

    5

  • 8/6/2019 C++ Function Part 1

    6/41

    Declaring, calling and defining functions

    6

  • 8/6/2019 C++ Function Part 1

    7/41

    Function Definition

    Contains the code for a function.

    Two parts : thefunction header and thefunction body

    7

  • 8/6/2019 C++ Function Part 1

    8/41

    8

    Function Header

    Consist of : the return type, the function name and formalparameter list

    Return type

    The type of value that will return by the function, the type

    of the expression in the return statement must match thereturn type in the function header. For example void, int,

    char and double.

    Formal Parameter List

    List that defines and declares the variables that will

    contained the data received by the function

    Each variables must be defined and declared fully with

    multiple parameters separated by commas.

  • 8/6/2019 C++ Function Part 1

    9/41

    9

    Function Body

    Contains the declarations and statements for the function

    Start with local definitions that specify the variables required by

    the function.

    The functions statement, terminating with a return statement are

    coded after local definitions.

  • 8/6/2019 C++ Function Part 1

    10/41

    10

    Function local variables

  • 8/6/2019 C++ Function Part 1

    11/41

    11

    Prototype declaration Consist of three parts : the return part, function name,

    and the formal parameter list(has to be same as in functionheader).

    Terminated with semicolon.

    Placed in global area of the program

    General format:

    Type Function_name(parameter_list);

    Example :

    double average (int x, int y);

    double average (int, int);

    void display ( );

    char pilihan ( );

  • 8/6/2019 C++ Function Part 1

    12/41

    12

    The Function Call

    The operand in a function call is the function name.

    The operator is the parentheses set,(), which contains the actual

    parameters.

    Examples of the function calls :

    cout

  • 8/6/2019 C++ Function Part 1

    13/41

    13More examples of function calls

  • 8/6/2019 C++ Function Part 1

    14/41

    14

    Parts of a function call

    The Function Call

  • 8/6/2019 C++ Function Part 1

    15/41

    15Calling a void function with no parameters

    Void functions with no parameters

  • 8/6/2019 C++ Function Part 1

    16/41

    16

    Void functions with parameters

  • 8/6/2019 C++ Function Part 1

    17/41

    17Pass by Value

    Functions that return value

  • 8/6/2019 C++ Function Part 1

    18/41

    Programming Example

    18

    int max(int num1, int num2){

    int result;

    if (num1 > num2)result = num1;

    elseresult = num2;

    return result;}

    return value type method name formal parameters

    return value

    functionbody

    function

    header

    parameter list

    Define a function Invoke a funciton

    int z = max(x, y);

    actual parameters(arguments)

  • 8/6/2019 C++ Function Part 1

    19/41

    19

    int main(){

    int i = 5;

    int j = 2;int k = max(i, j);

    cout

  • 8/6/2019 C++ Function Part 1

    20/41

    Trace Function Invocation

    20

    int main()

    {

    int i = 5;

    int j = 2;

    int k = max(i, j);

    cout

  • 8/6/2019 C++ Function Part 1

    21/41

    Trace Function Invocation

    21

    int main(){

    int i = 5;int j = 2;int k = max(i, j);

    cout

  • 8/6/2019 C++ Function Part 1

    22/41

    Trace Function Invocation

    22

    int main()

    {

    int i = 5;

    int j = 2;

    int k = max(i, j);

    cout

  • 8/6/2019 C++ Function Part 1

    23/41

    Trace Function Invocation

    23

    int main(){

    int i = 5;int j = 2;int k = max(i, j);

    cout

  • 8/6/2019 C++ Function Part 1

    24/41

    Trace Function Invocation

    24

    int main()

    {

    int i = 5;

    int j = 2;

    int k = max(i, j);

    cout

  • 8/6/2019 C++ Function Part 1

    25/41

    Trace Function Invocation

    25

    int main()

    {

    int i = 5;

    int j = 2;

    int k = max(i, j);

    cout

  • 8/6/2019 C++ Function Part 1

    26/41

    Trace Function Invocation

    26

    int main()

    {

    int i = 5;

    int j = 2;

    int k = max(i, j);

    cout

  • 8/6/2019 C++ Function Part 1

    27/41

    Trace Function Invocation

    27

    int main(){

    int i = 5;int j = 2;int k = max(i, j);

    cout

  • 8/6/2019 C++ Function Part 1

    28/41

    Trace Function Invocation

    28

    int main()

    {

    int i = 5;

    int j = 2;

    int k = max(i, j);

    cout

  • 8/6/2019 C++ Function Part 1

    29/41

    Trace Function Invocation

    29

    int main(){

    int i = 5;int j = 2;int k = max(i, j);

    cout

  • 8/6/2019 C++ Function Part 1

    30/41

    30

    Variable ScopeScope-determines the the part of program in which you can use

    defined object.

    Global scope any object defined in the global area of the

    program is visible from its definition until the end of the

    program.

    -global variables : variables that are declared outside the function,recognised by any function or program that start after its declaration

    Local scope variable defined within a block, visible only in

    the block in which they are declares.

    - local variables :variables that are declared in the function body and

    can only be used in that particular function.

    - do not relate to any variable in other function (can have the same name

    as variables in other functions)

  • 8/6/2019 C++ Function Part 1

    31/41

    31Scope for global and block areas

  • 8/6/2019 C++ Function Part 1

    32/41

    Local Variables

    You can declare a local variable with the

    same name multiple times in different non-

    nesting blocks in a function, but you cannot

    declare a local variable twice in nested

    blocks.

    32

  • 8/6/2019 C++ Function Part 1

    33/41

    Local VariablesA variable declared in the initial action part of a for loop

    header has itsscope in theentire loop. But a variabledeclared inside a for loop body has itsscope limited in theloop body from its declaration and to theend of theblockthat contains the variable.

    33

    void method1() {

    .

    .

    for (int i = 1; i < 10; i++)

    {

    .

    int j;

    .

    .

    .

    }

    }

    The scope of j

    The scope of i

  • 8/6/2019 C++ Function Part 1

    34/41

    Local Variables

    34

    voidfunction1()

    {

    int x = 1;

    int y = 1;

    for (int i = 1; i < 10; i++)

    {

    x += i;

    }

    for (int i = 1; i < 10; i++)

    {

    y += i;

    }}

    It is fine to declare i in two

    non-nesting blocks

    voidfunction2()

    {

    int i = 1;

    int sum = 0;

    for (int i = 1; i < 10; i++)

    {

    sum += i;

    }

    cout

  • 8/6/2019 C++ Function Part 1

    35/41

  • 8/6/2019 C++ Function Part 1

    36/41

    #include

    using namespace std;

    int y;

    void t1();

    void t2();

    int main()

    {

    t1();

    t2();

    return 0;

    }

    void t1()

    {int x = 1;

    cout

  • 8/6/2019 C++ Function Part 1

    37/41

    Unary Scope Resolution

    If a local variable name is thesame as a global variable name,you can access the global variable using ::globalVariable. The ::operator is known as theunary scope resolution. For example,the following code:

    37

    #include using namespace std;

    int v1 = 10;

    int main()

    {

    int v1 = 5;

    cout

  • 8/6/2019 C++ Function Part 1

    38/41

  • 8/6/2019 C++ Function Part 1

    39/41

    When static is used

    #include

    using namespace std;

    void t1();

    int main()

    {

    t1();

    t1();

    return 0;

    }

    void t1()

    {

    static int x = 1;

    int y = 1;

    x++;

    y++;cout

  • 8/6/2019 C++ Function Part 1

    40/41

  • 8/6/2019 C++ Function Part 1

    41/41

    THANK YOU

    41