Lab6 Function Pt1 CSEB114 Sem2 20112012.

download Lab6 Function Pt1 CSEB114 Sem2 20112012.

of 5

Transcript of Lab6 Function Pt1 CSEB114 Sem2 20112012.

  • 8/11/2019 Lab6 Function Pt1 CSEB114 Sem2 20112012.

    1/5

    C Programming LabUniversiti Tenaga Nasional

    6 Function (Part I)

    Do you still remember this?

    #.()

    ("")

    Referring to the same program but through function, your program might look somethinglike this:

    #.

    ()

    ()

    ()

    ()

    ("")

    Some recap from lecture: A function is a module (or block) in program code that deals with a particular task. Two purposes of using a function: -

    To allow a program to say:This piece of code does a specific job which stands by itself and should not bemixed up with anything else

    Function allows a block of codes to be reused

    Function helps us to organise a program in a simpler way.

    The difference between looping and function is:

    A function can be called at any part of a program but a loop can only be done orused at the looping statement in a program.

    3 elements of function are:1. Definition- a set of instructionsthat a function is supposed to do /execute.2. Prototype- Like variable, a function must also be declaredbefore using in a

    program. This declaration is referred as function prototype.3. Function call- The execution part of a function (function call) must be instructed

    or called by other function. Function main ( ) normally acts as the caller function.Anyway, it can be called from other functions too. Function will return any value (if

    theres a return value) to the caller function after it has finished executing its taskin the called function.

    Block of codesfor function

    main()

    Block of codesfor functionprint()

  • 8/11/2019 Lab6 Function Pt1 CSEB114 Sem2 20112012.

    2/5

    C Programming LabUniversiti Tenaga Nasional

    #. ()

    ()

    ()

    ()

    ("")

    Looking at function prototype in detail:

    ()

    Looking at function call in detail:

    ()

    Looking at function definition in detail:

    ()

    Function prototype

    Function call

    Function definition

    The datatype of

    thereturn

    Functionname

    The data typeof the

    parameter(s)

    Functionname

    Value(s) to be passed tothe corresponding

    function

    Variablethat will

    accept thereturnvalue

    Functionname

    Variable list (data typeand variable name

    separated by comma)

    The datatype of the

    returnvalue

  • 8/11/2019 Lab6 Function Pt1 CSEB114 Sem2 20112012.

    3/5

    C Programming LabUniversiti Tenaga Nasional

    How does function work? Lets modify the previous example.

    #. ()

    ()

    ()

    (" ' ")()

    ()

    ("")

    As you can see, the difference between looping and function is: A function can be called atany part of a program but a loop can only be done or used at the looping statement in aprogram.

    Types of functions:

    Receive no input parameter and return nothingSample program without function Sample program with function// 1#.

    ()

    ("")

    // 2#.

    ()

    ()

    ()

    ()

    ("")

    Receive no input parameter and return a value

    Sample program without function Sample program with function

    // 3#.

    ()

    (" :")("%",&)(" %",)

    // 4#.

    ()

    ()

    ( )(" %", )

    ()

    (" :")

    ("%",&)

    12

    34

  • 8/11/2019 Lab6 Function Pt1 CSEB114 Sem2 20112012.

    4/5

    C Programming LabUniversiti Tenaga Nasional

    Receive input parameter and return nothing

    Sample program without function Sample program with function

    // 5#.

    ()

    15 + 8

    (" % ",)

    // 6#.()

    ()

    15 + 8()

    ()

    (" % ",)

    Receive input parameter and return a value

    Sample program without function Sample program with function// 7#.

    ()

    , 1, 2(" ")

    ("%%", &1, &2) 1 + 2(" %", )

    // 8#.

    (,)

    ()

    , 1, 2

    (" ") ("%%", &1, &2)

    (1, 2)(" %", )

    (1, 2)

    1 + 2

    How to create program with function?

    Sample question:

    Write a program that has a function multiplythat accept 2 integers and returns themultiplication of those two integers.

    One of the methods to create function:

    1 Create the skeleton of function main()2 Develop the function definition (instruction in the function, function name,

    parameters, return value)3 Set the function call, based on the function definition

    4 Fill in other stuff in function main()(variable declarations, other instructions)5 End with function prototype

  • 8/11/2019 Lab6 Function Pt1 CSEB114 Sem2 20112012.

    5/5

    C Programming LabUniversiti Tenaga Nasional

    Refer to sample 8. Create as many function(s) as you can from the program

    Another method:

    1 Create full program without function (except for function main())2 Select and extract part(s) that want to be converted to function

    3 Create function(s)

    Exercise:

    Write a main()function and another function, named message()that main()calls. Ask

    the users for their annual income in main()function. Pass the value of income from

    main()function to the message()function where message()function will print acongratulatory message if the user makes more than RM40,000 a year or an encouragementmessage if the user makes less.