Week 5 Functions 1 Uni MAP Sem I1112

  • Slides: 30
Download presentation
Week 5 – Functions (1) Uni. MAP Sem. I-11/12 EKT 120: Computer Programming 1

Week 5 – Functions (1) Uni. MAP Sem. I-11/12 EKT 120: Computer Programming 1

Outline n n Why use functions? Functions in C n n n Pre-defined functions

Outline n n Why use functions? Functions in C n n n 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 Uni. MAP Sem. I-11/12 EKT 120: Computer Programming 2

Why use functions? n Let say you want to print one row of number

Why use functions? n Let say you want to print one row of number 8 and one row of number 9 #include <stdio. h> int main() { int i. Loop 1, i. Loop 2; //print one row of number 8 for(i. Loop 1=1; i. Loop 1<=10; i. Loop 1++) printf(“ 8"); printf("n"); //go to new line //print one row of number 9 for(i. Loop 2=1; i. Loop 2<=10; i. Loop 2++) printf(“ 9“); printf("n"); //go to new line } return 0; Uni. MAP Sem. I-11/12 EKT 120: Computer Programming 3

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

Why use functions? n n n 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 Uni. MAP Sem. I-11/12 EKT 120: Computer Programming 4

Why use functions? #include <stdio. h> void fn. Display(int); int main() { fn. Display(8);

Why use functions? #include <stdio. h> void fn. Display(int); int main() { fn. Display(8); fn. Display(9); return 0; } //function prototype //function call void fn. Display(int value) //function definition { int i. Loop; for(i. Loop=1; i. Loop<=10; i. Loop++) printf("%d", value); printf("n"); //go to new line } Uni. MAP Sem. I-11/12 EKT 120: Computer Programming 5

Functions in C n n Functions can be created to execute small, frequently-used tasks

Functions in C n n Functions can be created to execute small, frequently-used tasks In C, there are predefined functions or sometimes called standard functions, and there are userdefined 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) Uni. MAP Sem. I-11/12 EKT 120: Computer Programming 6

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

Predefined Functions (Library) n n n 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 Uni. MAP Sem. I-11/12 EKT 120: Computer Programming 7

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

Predefined Functions (Library)example #include <stdio. h> #include <math. h> #include <string. h> void main() { string s. Name; int i. Vol 1, i. Vol 2, i. N, i. R, i. KTemp, i. Length; } strcpy(s. Name, “Marina”); i. Vol 2 = i. Vol 1 * exp(i. N * i. R * i. KTemp); i. Length = strlen(“Mahathir”); Uni. MAP Sem. I-11/12 EKT 120: Computer Programming 8

User-Defined Functions n What do we need to define and make use of user-defined

User-Defined Functions n What do we need to define and make use of user-defined functions? n n n Function prototypes Function definitions Function calls Uni. MAP Sem. I-11/12 EKT 120: Computer Programming 9

Function Prototypes n n Function prototype is a declaration; indicates the function exists Should

Function Prototypes n n 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: n n n <return_type> <function_name> (arg_type arg_name, . . . ); int fn. Sum (int i. Num 1, int i. Num 2); int fn. Sum (int, int); //is also acceptable semicolon Uni. MAP Sem. I-11/12 EKT 120: Computer Programming 10

Function Definitions n n Function definition includes the body of a function Function definition

Function Definitions n n Function definition includes the body of a function Function definition has the following form: n { } n § § <return_type> <function_name> (arg_type arg_name, . . . ) … statements … int fn. Sum (int i. Num 1, int i. Num 2) function header { int i. Add; i. Add = i. Num 1 + i. Num 2; return(i. Add); } no semicolon Notice that argument name is used in the function body Unlike function prototype, argument name in function definition must be included in function header Uni. MAP Sem. I-11/12 EKT 120: Computer Programming 11

Function Calls n n Consists of a function name followed by an argument expression

Function Calls n n Consists of a function name followed by an argument expression list enclosed in parentheses Function call has the following form: n <function_name> (exp, exp. . . ) n exp is an expression – can be variable or constant n i. Result = fn. Sum(x, y); Uni. MAP Sem. I-11/12 EKT 120: Computer Programming 12

Example of function in program //This program sums up two numbers #include <stdio. h>

Example of function in program //This program sums up two numbers #include <stdio. h> int fn. Sum(int, int); //function prototype int main() { int i. X, i. Y, i. Result; printf( “Enter x and y : ”); scanf(“%d %d”, &i. X, &i. Y); i. Result = fn. Sum(i. X, i. Y); //function call printf(“Sum is : %d”, i. Result); return 0; function header } int fn. Sum(int i. Num 1, int i. Num 2)//function definition { int i. Add; i. Add = i. Num 1+i. Num 2; return(i. Add); } } EKT 120: Computer Programming 13

What about number, order and type of parameter? n n n Number, order and

What about number, order and type of parameter? n n n 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. Uni. MAP Sem. I-11/12 EKT 120: Computer Programming 14

What about number, order and type of parameter? (e. g 1) n prototype void

What about number, order and type of parameter? (e. g 1) n prototype void fn. Function 2(int i. N, double d. X); function header void fn. Function 2(int i. N, double d. X) function call fn. Function 2 (i. M, d. Y); 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. Uni. MAP Sem. I-11/12 EKT 120: Computer Programming 15

What about number, order and type of parameter? (e. g 2) int fn. Sum(int,

What about number, order and type of parameter? (e. g 2) int fn. Sum(int, int); //function prototype int fn. Sum(int i. Num 1, int i. Num 2) //function definition fn. Sum(i. X, i. Y); n n //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. Uni. MAP Sem. I-11/12 EKT 120: Computer Programming 16

Functions that do not return a value //This program sums up two numbers #include

Functions that do not return a value //This program sums up two numbers #include <stdio. h> void fn. Sum. Print(int, int); void fn. Function 1(); //function prototype int main() { int i. X, i. Y; fn. Function 1(); //function call printf(“Enter x and y: ”); scanf(“%d %d”, &i. X, &i. Y); fn. Sum. Print(i. X, i. Y); //function call return 0; } void fn. Sum. Print(int i. Num 1, int i. Num 2) //function definition { int i. Add; i. Add = i. Num 1+i. Num 2; printf(“Sum is: %d”, i. Add); } void fn. Function 1() { printf(“Welcome to this programn”); } Uni. MAP Sem. I-11/12 EKT 120: Computer Programming 17

Functions that return a value //This program sums up two numbers #include <stdio. h>

Functions that return a value //This program sums up two numbers #include <stdio. h> int fn. Sum(int, int); //function prototype int main() { int i. X, i. Y, i. Result; printf(“Enter x and y: ”); scanf(“%d %d”, &i. X, &i. Y); i. Result = fn. Sum(i. X, i. Y); //function call printf(“Sum is : %d”, i. Result); return 0; } int fn. Sum(int i. Num 1, int i. Num 2) { int i. Add; i. Add = i. Num 1+i. Num 2; return(i. Add); } Uni. MAP Sem. I-11/12 //function definition EKT 120: Computer Programming 18

Miscellaneous about functions Function call used as logical expression int fn. Calc(int, int); //function

Miscellaneous about functions Function call used as logical expression int fn. Calc(int, int); //function prototype int main(void) { int i. Num 1, i. Num 2; scanf(“%d %d”, &i. Num 1, &i. Num 2); if(fn. Calc(i. Num 1, i. Num 2)>100) //function call used as logical expression printf(“result greater than 100”); else printf(“result less than 100”); return 0; } n int fn. Calc(int i. N 1, int i. N 2) { int i. Answer; i. Answer=i. N 1+i. N 2; return(i. Answer); } Uni. MAP Sem. I-11/12 EKT 120: Computer Programming 19

Miscellaneous about functions Function call used in printf statement int fn. Calc(int, int); //function

Miscellaneous about functions Function call used in printf statement int fn. Calc(int, int); //function prototype int main(void) { int i. Num 1, i. Num 2; scanf(“%d %d”, &i. Num 1, &i. Num 2); printf(“Sum = %d”, fn. Calc(i. Num 1, i. Num 2)); //function call returns a //value and puts in printf return 0; } n int fn. Calc(int i. N 1, int i. N 2) { int i. Answer; i. Answer=i. N 1+i. N 2; return(i. Answer); } Uni. MAP Sem. I-11/12 EKT 120: Computer Programming 20

Miscellaneous about functions n n n Rules regarding naming convention for variables i. Num

Miscellaneous about functions n n n Rules regarding naming convention for variables i. Num 1 passes value to i. N 1, i. Num 2 passes value to i. N 2 Better use different variable names for parameters in main AND parameters in function definition int fn. Calc(int, int); int main(void) { int i. Num 1, i. Num 2, i. Result; scanf(“%d %d”, &i. Num 1, &i. Num 2); i. Result = fn. Calc(i. Num 1, i. Num 2); printf(“sum = %d“, i. Result); return 0; } //function definition int fn. Calc(int i. N 1, int i. N 2) { int i. Answer; i. Answer=i. N 1+i. N 2; return(i. Answer); } Uni. MAP Sem. I-11/12 //prototype function //declare like this //function call //simply declare like this EKT 120: Computer Programming 21

Sample application n n Write a C program that calculates and prints addition and

Sample application n n Write a C program that calculates and prints addition and subtraction of numbers. Your program should have functions: n n n fn. Add : adds two numbers fn. Subtract : subtracts two numbers fn. Print. Result : prints results from calculation Uni. MAP Sem. I-11/12 EKT 120: Computer Programming 22

Sample application(cont) #include <stdio. h> int fn. Add(int, int); int fn. Subtract(int, int); void

Sample application(cont) #include <stdio. h> int fn. Add(int, int); int fn. Subtract(int, int); void fn. Print. Result(int); int main() { int i. Num 1, i. Num 2, i. Answer; char c. Op; printf(“Enter two numbers and operator: ”); scanf(“%d %d %c”, &i. Num 1, &i. Num 2, &c. Op); switch(c. Op) { case ‘+’ : i. Answer=fn. Add(i. Num 1, i. Num 2); break; case ‘-’ : i. Answer=fn. Subtract(i. Num 1, i. Num 2); break; default: printf(“Invalid operator”); } } fn. Print. Result(i. Answer); return 0; Uni. MAP Sem. I-11/12 int fn. Add(int i. X, int i. Y) { int i. Sum; i. Sum = i. X+i. Y; return(i. Sum); } int fn. Subtract(int i. X, int i. Y) { int i. Sub; i. Sub=i. X-i. Y; return(i. Sub); } void fn. Print. Result(int i. Ans) { printf(“Answer is %d”, i. Ans); } EKT 120: Computer Programming 23

1/* Fig. 5. 4: fig 05_04. c 2 Finding the maximum of three integers

1/* Fig. 5. 4: fig 05_04. c 2 Finding the maximum of three integers */ 3#include <stdio. h> 4 5 int fn. Maximum(int, int); /* function prototype */ 6 7 int main() 8{ 9 int i. A, i. B, i. C; 10 11 printf( "Enter three integers: " ); 12 scanf( "%d %d %d", &i. A, &i. B, &i. C ); 13 printf( "Maximum is: %dn", fn. Maximum( i. A, i. B, i. C ) ); 14 15 return 0; 16 } 17 18 /* Function maximum definition */ 19 int fn. Maximum(int i. X, int i. Y, int i. Z) 20 { 21 int i. Max = i. X; 22 23 if ( i. Y > i. Max ) 24 i. Max = i. Y; 25 26 if ( i. Z > i. Max ) 27 i. Max = i. Z; 28 29 return i. Max; 30 } Enter three integers: 22 85 17 Maximum is: 85 1. Function prototype (3 parameters) 2. Function call 3. Function definition Program Output 24

Scope and Mechanics of Passing Values to Functions n n n Scope refers to

Scope and Mechanics of Passing Values to Functions n n n Scope refers to the region in which a declaration is active File scope is also called global variable n declared at the top of a source file n declarations not placed in any functions n can be used by any statements that are being executed in the system Function scope is also called local variable n declared in a block { … } n scope is within its block – lifetime while the block is executed Uni. MAP Sem. I-11/12 EKT 120: Computer Programming 25

Global Variable : Example n #include <stdio. h> int i. Global = 3; void

Global Variable : Example n #include <stdio. h> int i. Global = 3; void fn. Change. Global( ); //This is the global variable int main(void) { printf("%dn“, i. Global); //Reference to global //variable in a function fn. Change. Global(); printf("%dn", i. Global); return 0; } void fn. Change. Global( ) { i. Global = 5; } Uni. MAP Sem. I-11/12 //Reference to global //variable in a function EKT 120: Computer Programming 26

Global Variable : Example The output will be: 3 5 Uni. MAP Sem. I-11/12

Global Variable : Example The output will be: 3 5 Uni. MAP Sem. I-11/12 EKT 120: Computer Programming 27

Local Variable : Example n #include <stdio. h> void fn. Change. Local(); int main(void)

Local Variable : Example n #include <stdio. h> void fn. Change. Local(); int main(void) { int i. Local = 3; //This is a local variable printf("%dn", i. Local); //Reference to local //variable in a function fn. Change. Local(); printf("%dn", i. Local); return 0; } void fn. Change. Local() { int i. Local = 5; //This is another local variable printf("%dn", i. Local); } Uni. MAP Sem. I-11/12 EKT 120: Computer Programming 28

Local Variable : Example The output will be: 3 5 3 Uni. MAP Sem.

Local Variable : Example The output will be: 3 5 3 Uni. MAP Sem. I-11/12 EKT 120: Computer Programming 29

End Week 5 – Functions (1) Q & A! Uni. MAP Sem. I-11/12 EKT

End Week 5 – Functions (1) Q & A! Uni. MAP Sem. I-11/12 EKT 120: Computer Programming 30