+ All Categories
Home > Documents > UniMAP SemI-09/10EKT120: Computer Programming1 Week 5 – Functions (1)

UniMAP SemI-09/10EKT120: Computer Programming1 Week 5 – Functions (1)

Date post: 13-Dec-2015
Category:
Upload: eileen-adams
View: 220 times
Download: 3 times
Share this document with a friend
Popular Tags:
29
UniMAP SemI- 09/10 EKT120: Computer Programming 1 Week 5 – Functions (1)
Transcript
Page 1: UniMAP SemI-09/10EKT120: Computer Programming1 Week 5 – Functions (1)

UniMAP SemI-09/10

EKT120: Computer Programming 1

Week 5 – Functions (1)

Page 2: UniMAP SemI-09/10EKT120: Computer Programming1 Week 5 – Functions (1)

UniMAP SemI-09/10

EKT120: Computer Programming 2

Outline Why use functions? Functions in C

Pre-defined functions User-defined functions

Function prototype Function definition Function call 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

Page 3: UniMAP SemI-09/10EKT120: Computer Programming1 Week 5 – Functions (1)

UniMAP SemI-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 <stdio.h>int main(){ int i, j;

//print one row of number 8 for(i=1; i<=10; i++) printf(“8"); printf("\n"); //go to new line

//print one row of number 9 for(j=1; j<=10; j++) printf(“9“); printf("\n"); //go to new line

return 0;}

Page 4: UniMAP SemI-09/10EKT120: Computer Programming1 Week 5 – Functions (1)

UniMAP SemI-09/10

EKT120: Computer Programming 4

Why use functions?(cont)

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

Page 5: UniMAP SemI-09/10EKT120: Computer Programming1 Week 5 – Functions (1)

UniMAP SemI-09/10

EKT120: Computer Programming 5

Why use functions?(cont)#include <stdio.h>void display(int); //function prototypeint main(){

display(8); //function call display(9); //function call return 0;}

void display(int value) //function definition{ int i; for(i=1; i<=10; i++) printf("%d", value); printf("\n"); //go to new line}

Page 6: UniMAP SemI-09/10EKT120: Computer Programming1 Week 5 – Functions (1)

UniMAP SemI-09/10

EKT120: Computer Programming 6

Functions in C Functions can be created to execute small,

frequently-used tasks In C, there are predefined functions or

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

Predefined functions are already available functions that can be used, called library

The usage is like stdio.h, in which the library name must be #included at the top of the source code (preprocessor directive)

Page 7: UniMAP SemI-09/10EKT120: Computer Programming1 Week 5 – Functions (1)

UniMAP SemI-09/10

EKT120: Computer Programming 7

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

string.h, and stdlib.h stdio.h related functions: printf,

scanf,etc math.h related functions: sin, cos, exp,

pow, sqrt, etc. string.h related functions: strcmp,

strcpy, strlen, etc. stdlib.h related functions: abs, fabs

Page 8: UniMAP SemI-09/10EKT120: Computer Programming1 Week 5 – Functions (1)

UniMAP SemI-09/10

EKT120: Computer Programming 8

Predefined function (Library)-example#include <stdio.h>#include <math.h>#include <string.h>void main(){

string name;int vol1, vol2, n, R, kTemp, length;

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

Page 9: UniMAP SemI-09/10EKT120: Computer Programming1 Week 5 – Functions (1)

UniMAP SemI-09/10

EKT120: Computer Programming 9

User-defined function

What do we need to define and make use of user-defined function? Function prototype Function definition Function call

Page 10: UniMAP SemI-09/10EKT120: Computer Programming1 Week 5 – Functions (1)

UniMAP SemI-09/10

EKT120: Computer Programming 10

Function prototype Function prototype is a declaration;

indicates the function exists Should have function name, return type

and parameter Argument name is not compulsory in

function header Function prototype has the following form:

<return_type> <function_name> (arg_type arg_name, ...);

int sum (int num1,int num2); int sum (int,int); //is also acceptable semicolon

Page 11: UniMAP SemI-09/10EKT120: Computer Programming1 Week 5 – Functions (1)

UniMAP SemI-09/10

EKT120: Computer Programming 11

Function definition Function definition includes the body of a

function Function definition has the following form:

<return_type> <function_name> (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 must be included in function header

no semicolonfunction header

Page 12: UniMAP SemI-09/10EKT120: Computer Programming1 Week 5 – Functions (1)

UniMAP SemI-09/10

EKT120: Computer Programming 12

Function call Consists of a function name followed by an

argument expression list enclosed in parentheses

Function call has the following form: <function_name> (exp, exp ...) exp is an expression – can be variable or

constant result = sum(x,y);

Page 13: UniMAP SemI-09/10EKT120: Computer Programming1 Week 5 – Functions (1)

UniMAP SemI-09/10

EKT120: Computer Programming 13

Example of function in program//This program sums up two numbers

#include <stdio.h>int sum(int, int); //function prototypeint 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);}

}

function header

Page 14: UniMAP SemI-09/10EKT120: Computer Programming1 Week 5 – Functions (1)

UniMAP SemI-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 MUST match.

If function prototype and definition have three parameters then the function call must have three parameters.

If the types are int, float and double in the prototype, the types in the function call should be int, float and double, respectively.

Page 15: UniMAP SemI-09/10EKT120: Computer Programming1 Week 5 – Functions (1)

UniMAP SemI-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 int and the second is double. With these three we have met the number, order and type requirements.

Page 16: UniMAP SemI-09/10EKT120: Computer Programming1 Week 5 – Functions (1)

UniMAP SemI-09/10

EKT120: Computer Programming 16

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

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

int sum(int num1, int num2) //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 in order i.e respectively and first parameter is int and second parameter is int.

Page 17: UniMAP SemI-09/10EKT120: Computer Programming1 Week 5 – Functions (1)

UniMAP SemI-09/10

EKT120: Computer Programming 17

Functions that do not return a value

//This program sums up two numbers#include <stdio.h>

void sum_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;

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

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

}void function1(){ printf(“Welcome to this program\n”); }

Page 18: UniMAP SemI-09/10EKT120: Computer Programming1 Week 5 – Functions (1)

UniMAP SemI-09/10

EKT120: Computer Programming 18

Functions that return one value

//This program sums up two numbers#include <stdio.h>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 callprintf(“Sum is : %d”,result);return 0;

}

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

add = num1+num2;return(add);

}

Page 19: UniMAP SemI-09/10EKT120: Computer Programming1 Week 5 – Functions (1)

UniMAP SemI-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”);

elseprintf(“result less than 100”);

return 0;}

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

answer=n1+n2;return(answer);

}

Page 20: UniMAP SemI-09/10EKT120: Computer Programming1 Week 5 – Functions (1)

UniMAP SemI-09/10

EKT120: Computer Programming 20

Miscellaneous about functions

Function call used in printf statementint calc(int,int); //function prototypeint main(void){ int num1,num2;

scanf(“%d %d”,&num1,&num2);printf(“Jawapan : %d”,calc(num1, num2)); //function call returns a

//value and puts in printf

return 0;}

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

answer=n1+n2;return(answer);

}

Page 21: UniMAP SemI-09/10EKT120: Computer Programming1 Week 5 – Functions (1)

UniMAP SemI-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 main

AND 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);

}

Page 22: UniMAP SemI-09/10EKT120: Computer Programming1 Week 5 – Functions (1)

UniMAP SemI-09/10

EKT120: Computer Programming 22

Sample application Write a C program that calculates and

prints addition and subtraction of numbers.

Your program should have functions: add : adds two numbers subtract : subtracts two numbers print_result : prints results from

calculation

Page 23: UniMAP SemI-09/10EKT120: Computer Programming1 Week 5 – Functions (1)

UniMAP SemI-09/10

EKT120: Computer Programming 23

Sample application(cont)#include <stdio.h>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); }

Page 24: UniMAP SemI-09/10EKT120: Computer Programming1 Week 5 – Functions (1)

UniMAP SemI-09/10

EKT120: Computer Programming 24

1. Function prototype (3 parameters)

2. Function call

3. Function definition

Program Output

1 /* Fig. 5.4: fig05_04.c2 Finding the maximum of three integers */3 #include <stdio.h>45 int maximum(int, int, int); /* function prototype */67 int main()8 {9 int a, b, c;1011 printf( "Enter three integers: " );12 scanf( "%d %d %d", &a, &b, &c );13 printf( "Maximum is: %d\n", maximum( a, b, c ) );1415 return 0;16 }1718 /* Function maximum definition */19 int maximum(int x, int y, int z)20 {21 int max = x;2223 if ( y > max )24 max = y;2526 if ( z > max )27 max = z;2829 return max;30 }

Enter three integers: 22 85 17Maximum is: 85

Page 25: UniMAP SemI-09/10EKT120: Computer Programming1 Week 5 – Functions (1)

UniMAP SemI-09/10

EKT120: Computer Programming 25

Scope and Mechanics of Passing 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 statements that are being

executed in the system Function scope is also called local variable

declared in a block { … } scope is within its block – lifetime while the block is

executed

Page 26: UniMAP SemI-09/10EKT120: Computer Programming1 Week 5 – Functions (1)

UniMAP SemI-09/10

EKT120: Computer Programming 26

Scope and Mechanics of Passing Values to Functions

Page 27: UniMAP SemI-09/10EKT120: Computer Programming1 Week 5 – Functions (1)

UniMAP SemI-09/10

EKT120: Computer Programming 27

Page 28: UniMAP SemI-09/10EKT120: Computer Programming1 Week 5 – Functions (1)

UniMAP SemI-09/10

EKT120: Computer Programming 28

Scope and Mechanics of Passing Values to Functions

Page 29: UniMAP SemI-09/10EKT120: Computer Programming1 Week 5 – Functions (1)

UniMAP SemI-09/10

EKT120: Computer Programming 29

End Week 5 – Functions (1)

Q & A!


Recommended