EKT120_WEEK05_FUNCTIONS1

Embed Size (px)

Citation preview

  • 8/12/2019 EKT120_WEEK05_FUNCTIONS1

    1/30

    UniMAP SemII-09/10 EKT120: Computer Programming 1

    Week 5Functions (1)

  • 8/12/2019 EKT120_WEEK05_FUNCTIONS1

    2/30

    UniMAP SemII-09/10 EKT120: Computer Programming 2

    Outline Why use functions? Functions in C

    Pre-defined functions User-defined functions

    Function prototypes Function definitions Function calls What about number, order and type of parameter? Functions that do not return a value Functions that return a value Miscellaneous about functions Sample application Scope and mechanics of passing values to functions

  • 8/12/2019 EKT120_WEEK05_FUNCTIONS1

    3/30

    UniMAP SemII-09/10 EKT120: Computer Programming 3

    Why use functions? Let say you want to print one row of number 8 and one row of number 9

    #include int main()

    { int i, j;

    //print one row of number 8for(i=1; i

  • 8/12/2019 EKT120_WEEK05_FUNCTIONS1

    4/30

    UniMAP SemII-09/10 EKT120: Computer Programming 4

    Why use functions? It seems that you are doing the same

    thing twice!!(i.e. printing two rows of

    numbers)

    This is wasting time and not flexible!!

    So, need to use function

  • 8/12/2019 EKT120_WEEK05_FUNCTIONS1

    5/30

    UniMAP SemII-09/10 EKT120: Computer Programming 5

    Why use functions?#include void display(int); //function prototypeint main(){

    display(8); //function calldisplay(9); //function callreturn 0;

    }

    void display(int value) //function definition{ int i;for(i=1; i

  • 8/12/2019 EKT120_WEEK05_FUNCTIONS1

    6/30

    UniMAP SemII-09/10 EKT120: Computer Programming 6

    Functions in C Functions can be created to execute small,

    frequently-used tasks In C, there are predefined functionsor sometimes

    called standard functions, and there are user-defined functions.

    Predefined functionsare already availablefunctions that can be used, called library

    The usage is like stdio.h, in which the libraryname must be #includedat the top of the sourcecode (preprocessor directive)

  • 8/12/2019 EKT120_WEEK05_FUNCTIONS1

    7/30

    UniMAP SemII-09/10 EKT120: Computer Programming 7

    Predefined Functions (Library) Common libraries are stdio.h, math.h,

    string.h, andstdlib.h

    stdio.hrelated functions:printf, scanf,etc math.hrelated functions: sin, cos, exp, pow,

    sqrt, etc.

    string.hrelated functions: strcmp, strcpy,strlen, etc.

    stdlib.hrelated functions: abs, fabs

  • 8/12/2019 EKT120_WEEK05_FUNCTIONS1

    8/30

    UniMAP SemII-09/10 EKT120: Computer Programming 8

    Predefined Functions

    (Library)-example#include #include #include

    void main(){string name;int vol1, vol2, n, R, kTemp, length;

    strcpy(name, Marina);vol2 = vol1 * exp(n * R * kTemp);length = strlen(Mahathir);

    }

  • 8/12/2019 EKT120_WEEK05_FUNCTIONS1

    9/30

    UniMAP SemII-09/10 EKT120: Computer Programming 9

    User-Defined Functions What do we need to define and make

    use of user-defined functions?

    Function prototypes

    Function definitions

    Function calls

  • 8/12/2019 EKT120_WEEK05_FUNCTIONS1

    10/30

    UniMAP SemII-09/10 EKT120: Computer Programming 10

    Function Prototypes Function prototype is a declaration; indicates

    the function exists

    Should have function name, return type andparameter

    Argument name is notcompulsory in functionheader

    Function prototype hasthe following form: (arg_type arg_name, ...);

    int sum (int num1,int num2);

    int sum (int,int); //is also acceptablesemicolon

  • 8/12/2019 EKT120_WEEK05_FUNCTIONS1

    11/30

    UniMAP SemII-09/10 EKT120: Computer Programming 11

    Function Definitions Function definition includes the body of a function Function definition hasthe following form:

    (arg_type arg_name, ...){

    statements } int sum (int num1,int num2)

    {int add;add = num1 + num2;return(add);}

    Notice that argument name is used in the function body Unlike function prototype, argument name in function definition

    mustbe included in function header

    no semicolon

    function header

  • 8/12/2019 EKT120_WEEK05_FUNCTIONS1

    12/30

    UniMAP SemII-09/10 EKT120: Computer Programming 12

    Function Calls Consists of a function name followed by an argument

    expression list enclosed in parentheses

    Function call hasthe following form: (exp, exp ...)

    exp is an expressioncan be variable or constant

    result = sum(x,y);

  • 8/12/2019 EKT120_WEEK05_FUNCTIONS1

    13/30

    EKT120: Computer Programming 13

    Example of function in

    program//This program sums up two numbers#include

    int sum(int, int); //function prototype

    int main()

    { int x,y, result;printf( Enter x and y : );

    scanf(%d %d, &x, &y);

    result = sum(x,y); //function call

    printf(Sum is : %d, result);

    return 0;

    }

    int sum(int num1, int num2)//function definition

    { int add;

    add = num1+num2;

    return(add);}

    }

    function header

  • 8/12/2019 EKT120_WEEK05_FUNCTIONS1

    14/30

    UniMAP SemII-09/10 EKT120: Computer Programming 14

    What about number, order

    and type of parameter? Number, order and type of parameters in the

    argument list of a function call and function

    definition MUSTmatch. If function prototype and definition have three

    parameters then the function call must havethree parameters.

    If the types are int, floatand doublein the

    prototype, the types in the function call shouldbe int, floatand double, respectively.

  • 8/12/2019 EKT120_WEEK05_FUNCTIONS1

    15/30

    UniMAP SemII-09/10 EKT120: Computer Programming 15

    What about number, order

    and type of parameter?(e.g1)

    Note that there are two arguments for function prototype,function definition and function call; the first is intand the

    second is double. With these three we have met thenumber, order and type requirements.

  • 8/12/2019 EKT120_WEEK05_FUNCTIONS1

    16/30

    UniMAP SemII-09/10 EKT120: Computer Programming 16

    What about number, orderand type of parameter?(e.g2)

    int sum(int, int); //function prototype

    int sum(intnum1, intnum2) //function definition

    sum(x,y); //function call

    Refer to program in slide 13 Number, order and type parameter are met because:

    there are two parameters, the parameters are listed inorder i.e respectively and first parameter is intandsecond parameter is int.

  • 8/12/2019 EKT120_WEEK05_FUNCTIONS1

    17/30

    UniMAP SemII-09/10 EKT120: Computer Programming 17

    Functions that do not return avalue

    //This program sums up two numbers#include

    voidsum_print(int, int); //function prototypevoid function1(); //function prototype

    int main(){ int x,y;

    function1(); //function callprintf(Enter x and y: );scanf(%d %d, &x, &y);sum_print(x,y); //function callreturn 0;

    }voidsum_print(int num1, int num2)//function definition{ int add;

    add = num1+num2;printf(Sum is: %d,add);

    }voidfunction1(){ printf(Welcome to this program\n); }

  • 8/12/2019 EKT120_WEEK05_FUNCTIONS1

    18/30

    UniMAP SemII-09/10 EKT120: Computer Programming 18

    Functions that return a value

    //This program sums up two numbers#include intsum(int,int); //function prototype

    int main()

    { int x,y,result;

    printf(Enter x and y: );

    scanf(%d %d, &x, &y);

    result = sum(x,y); //function callprintf(Sum is : %d,result);

    return 0;

    }

    int sum(int num1, int num2) //function definition{ int add;

    add = num1+num2;

    return(add);}

  • 8/12/2019 EKT120_WEEK05_FUNCTIONS1

    19/30

    UniMAP SemII-09/10 EKT120: Computer Programming 19

    Miscellaneous about functions

    Function call used as logical expressionint calc(int,int); //function prototypeint main(void)

    { int num1, num2;

    scanf(%d %d,&num1,&num2);

    if(calc(num1,num2)>100) //function call used as logical expressionprintf(result greater than 100);

    else

    printf(result less than 100);

    return 0;

    }

    int calc(int n1,int n2)

    { int answer;

    answer=n1+n2;

    return(answer);

    }

  • 8/12/2019 EKT120_WEEK05_FUNCTIONS1

    20/30

    UniMAP SemII-09/10 EKT120: Computer Programming 20

    Miscellaneous about functions

    Function call used inprintf statementint calc(int,int); //function prototypeint main(void)

    { int num1,num2;

    scanf(%d %d,&num1,&num2);printf(Jawapan : %d,calc(num1, num2));//function callreturns a

    //value and puts in printfreturn 0;

    }

    int calc(int n1,int n2){ int answer;

    answer=n1+n2;

    return(answer);

    }

  • 8/12/2019 EKT120_WEEK05_FUNCTIONS1

    21/30

    UniMAP SemII-09/10 EKT120: Computer Programming 21

    Miscellaneous about functions

    Rules regarding naming convention for variables num1 passes value to n1, num2 passes value to n2 Better use different variable names for parameters in mainAND

    parameters in function definition

    int calc(int,int); //prototype functionint main(void){ int num1,num2,result; //declare like this

    scanf(%d %d,&num1,&num2);result = calc(num1,num2); //function callprintf(jawapan : %d,result);return 0;

    }

    //function definitionint calc(int n1,int n2) //simply declare like this{ int answer;

    answer=n1+n2;return(answer);

    }

  • 8/12/2019 EKT120_WEEK05_FUNCTIONS1

    22/30

    UniMAP SemII-09/10 EKT120: Computer Programming 22

    Sample application

    Write a C program that calculates and printsaddition and subtraction of numbers.

    Your program should have functions: add : adds two numbers

    subtract: subtracts two numbers

    print_result: prints results from calculation

  • 8/12/2019 EKT120_WEEK05_FUNCTIONS1

    23/30

    UniMAP SemII-09/10 EKT120: Computer Programming 23

    Sample application(cont)#include int add(int,int);int subtract(int,int);void print_result(int);

    int main()

    { int num1,num2,answer;char op;printf(Enter two numbers and operator:);scanf(%d %d %c, &num1,&num2,&op);switch(op){ case + :answer=add(num1,num2);break;

    case - :answer=subtract(num1,num2);break;default: printf(Invalid operator);exit(0);

    }

    print_result(answer);return 0;

    }

    int add(int x,int y){

    int sum;sum = x+y;return(sum);

    }

    int subtract(int x,int y){

    int sub;sub=x-y;return(sub);

    }

    void print_result(int ans){

    printf(Answer is %d, ans);}

    1/* Fig. 5.4: fig05 04.c

  • 8/12/2019 EKT120_WEEK05_FUNCTIONS1

    24/30

    UniMAP SemII-09/10 EKT120: Computer Programming 24

    1. Function prototype

    (3 parameters)

    2. Function call

    3. Function definition

    Program Output

    g g _

    2 Finding the maximum of three integers */

    3#include

    4

    5intmaximum(int, int, int); /* function prototype */

    6

    7intmain()

    8{

    9 inta, b, c;

    10

    11 printf( "Enter three integers: " );

    12 scanf( "%d %d %d", &a, &b, &c );

    13 printf( "Maximum is: %d\n", maximum( a, b, c ) );

    14

    15 return0;

    16 }17

    18 /* Function maximum definition */

    19 intmaximum(intx, inty, intz)

    20 {

    21 intmax = x;

    22

    23 if( y > max )

    24 max = y;25

    26 if( z > max )

    27 max = z;

    28

    29 returnmax;

    30 }

    Enter three integers: 22 85 17

    Maximum is: 85

  • 8/12/2019 EKT120_WEEK05_FUNCTIONS1

    25/30

    UniMAP SemII-09/10 EKT120: Computer Programming 25

    Scope and Mechanics ofPassing Values to Functions

    Scope refers to the region in which a declaration is active

    File scope is also called global variable

    declared at the top of a source file

    declarations not placed in any functions

    can be used by any statementsthat are being executedin the system

    Function scope is also called local variable

    declared in a block { } scope is within its blocklifetime while the block is

    executed

  • 8/12/2019 EKT120_WEEK05_FUNCTIONS1

    26/30

    UniMAP SemII-09/10 EKT120: Computer Programming 26

    Global Variable : Example

    #include

    int global = 3; //This is the global variablevoid ChangeGlobal( );

    int main(void){ printf("%d\n, global); //Reference to global

    //variable in a functionChangeGlobal();

    printf("%d\n", global);

    return 0;}

    void ChangeGlobal( ){ global = 5; } //Reference to global

    //variable in a function

  • 8/12/2019 EKT120_WEEK05_FUNCTIONS1

    27/30

    UniMAP SemII-09/10 EKT120: Computer Programming 27

    Global Variable : Example

    The output will be:

    3

    5

  • 8/12/2019 EKT120_WEEK05_FUNCTIONS1

    28/30

    UniMAP SemII-09/10 EKT120: Computer Programming 28

    Local Variable : Example #include

    void ChangeLocal();

    int main(void)

    { int local = 3; //This is a local variable

    printf("%d\n", local); //Reference to local//variable in a function

    ChangeLocal();printf("%d\n", local);

    return 0;}

    void ChangeLocal(){ int local = 5; //This is another local variableprintf("%d\n", local); }

  • 8/12/2019 EKT120_WEEK05_FUNCTIONS1

    29/30

    UniMAP SemII-09/10 EKT120: Computer Programming 29

    Local Variable : Example

    The output will be:

    3

    5

    3

  • 8/12/2019 EKT120_WEEK05_FUNCTIONS1

    30/30

    UniMAP SemII-09/10 EKT120: Computer Programming 30

    End Week 5Functions (1)

    Q & A!