Week 6 Functions 2 Uni MAP Sem II1011

  • Slides: 21
Download presentation
Week 6 – Functions (2) Uni. MAP Sem. II-10/11 EKT 120: Computer Programming 1

Week 6 – Functions (2) Uni. MAP Sem. II-10/11 EKT 120: Computer Programming 1

Outline n n n n Recall - sample application n functions that do not

Outline n n n n Recall - sample application n functions that do not return value n functions that return a value Recall – global variable vs. local variable Passing parameters in functions : - Pass by value Functions that return more than one value Sample application-functions that return more than one value Passing parameters in functions : - Pass by reference Sample application Recursive function Uni. MAP Sem. II-10/11 EKT 120: Computer Programming 2

Sample application n Write a C program that reads item code and quantity, then

Sample application n Write a C program that reads item code and quantity, then calculates the payment. Use functions: n fn. Menu – print item code menu n fn. Determine. Price – determine price based on item code n fn. Calc - calculate payment n fn. Print. Result – print payment What argument names do I want to feed in as parameters and what to return? ? Think!! Which function returns no value and which function returns a value. 3

Sample application #include <stdio. h> void fn. Menu(); float fn. Determine. Price(int); float fn.

Sample application #include <stdio. h> void fn. Menu(); float fn. Determine. Price(int); float fn. Calc(float, int); void fn. Print. Result(float); int main() { int i. Code, i. Qty; float f. Price, f. Pay; fn. Menu(); printf("Enter item code and quantity: "); scanf("%d %d", &i. Code, &i. Qty); f. Price = fn. Determine. Price(i. Code); f. Pay = fn. Calc(f. Price, i. Qty); fn. Print. Result(f. Pay); return 0; } EKT 120: Computer Programming 4

void fn. Menu() { printf("Codet. Itemt. Pricen"); printf("1t. Papayat 1. 00n"); printf("2t. Melont 2.

void fn. Menu() { printf("Codet. Itemt. Pricen"); printf("1t. Papayat 1. 00n"); printf("2t. Melont 2. 00n"); printf("3t. Duriant 3. 00n"); printf("t. Otherst 4. 00n"); } float fn. Determine. Price(int i. Item. Code) { float f. Pricing; switch(i. Item. Code) { case 1: f. Pricing = 1. 00; break; case 2: f. Pricing = 2. 00; break; case 3: f. Pricing = 3. 00; break; default: f. Pricing = 4. 00; } return(f. Pricing); } float f. Calc(float f. Item. Price, int i. Quality) { float f. Total; f. Total = f. Item. Price*i. Quantity; return(f. Total); } void fn. Print. Result(float f. Payment) { printf("Payment is %. 2 fn", f. Payment); } Code 1 2 3 Item Price Papaya 1. 00 Melon 2. 00 Durian 3. 00 Others 4. 00 Enter item code and quantity: 1 3 Payment is 3. 00 ************** Code Item Price 1 Papaya 1. 00 2 Melon 2. 00 3 Durian 3. 00 Others 4. 00 Enter item code and quantity: 9 3 Payment is 12. 00 5

Pass by Value n n If a parameter is passed by value, then the

Pass by Value n n If a parameter is passed by value, then the value of the original data is copied into the function’s parameter (scope: local variable(s)) In other words, it (i. e. local variable) has its own copy of the data changes to copy do not change original data During program execution, it (i. e. local variable) will manipulate the data stored in its own memory space Uni. MAP Sem. II-10/11 EKT 120: Computer Programming 6

Pass by Value (Example) #include <stdio. h> void fn. Fun 1(int, int); //function prototype

Pass by Value (Example) #include <stdio. h> void fn. Fun 1(int, int); //function prototype int main(void) { int i. A=5, i. B=10; printf("Before fun 1n“); printf(" i. A = %d i. B = %dn”, i. A, i. B); fn. Fun 1(i. A, i. B); //function call printf("n. After fun 1n“); printf(" i. A = %d i. B = %dn”, i. A, i. B); return 0; } Output Before fn. Fun 1 i. A = 5 i. B = 10 Inside fn. Fun 1 i. AA = 6 i. BB = 9 After fn. Fun 1 i. A = 5 i. B = 10 void fn. Fun 1(int i. AA, int i. BB) //function definition { i. AA++; i. BB--; printf("nn. Inside fun 1n)"; printf(“i. AA = %d i. BB = %dn”, i. AA, i. BB); } Uni. MAP Sem. II-10/11 EKT 120: Computer Programming 7

Functions that return more than one value n When we talk about functions that

Functions that return more than one value n When we talk about functions that return more than one value it also means that we want to pass arguments by reference n n n passes addresses (references), NOT value or data allows direct manipulation changes will affect original data Uni. MAP Sem. II-10/11 EKT 120: Computer Programming 8

Functions that return more than one value n There are cases where you need

Functions that return more than one value n There are cases where you need to manipulate the value of an external variable from inside a function, thus we pass the value by reference Uni. MAP Sem. II-10/11 EKT 120: Computer Programming 9

Sample application n n Write a C program that calculates and prints average of

Sample application n n Write a C program that calculates and prints average of 2 test marks. Your program should have functions: n n n fn. Read. Marks – read 2 test marks fn. Calc. Avg – calculate average of two test marks fn. Print - print average Uni. MAP Sem. II-10/11 EKT 120: Computer Programming 10

Sample application void fn. Read. Marks(float *f. M 1, float *f. M 2) {

Sample application void fn. Read. Marks(float *f. M 1, float *f. M 2) { printf("Enter marks for test 1 and test 2 : "); scanf("%f %f", f. M 1, f. M 2); //notice no & } #include <stdio. h> void fn. Read. Marks(float*, float*); float fn. Calc. Avg(float, float); void fn. Print(float); int main(void) { float f. Marks 1, f. Marks 2, f. Avg; } fn. Read. Marks(&f. Marks 1, &f. Marks 2); f. Avg = fn. Calc. Avg(f. Marks 1, f. Marks 2); fn. Print(f. Avg); return 0; Output Enter marks for test 1 and test 2 : 70 80 Average marks are : 75. 00 Uni. MAP Sem. II-10/11 Function that returns more than one value arguments are passed by reference float fn. Calc. Avg(float f. M 1, float f. M 2) { return((f. M 1 + f. M 2)/2); } void fn. Print(float f. Average) { printf("n. Average marks are : %. 2 fn", f. Average); } EKT 120: Computer Programming 11

Pass by Reference n n n A function’s parameter that receives the location (memory

Pass by Reference n n n A function’s parameter that receives the location (memory address) of the corresponding actual variables When we attach * (star) after the arg_type in the parameter list of a function, then the variable following that arg_type is passed by reference It stores the address of the actual variable, NOT the value During program execution to manipulate the data, the address stored will direct control to the memory space of the actual variable Syntax: n n In function protoype and function definition, put the * (star) after the data type Example : void fn. Read. Marks(float *, float *); In function call, put the &(ampersand) before the argument name to be passed by reference Example : fn. Read. Marks(&f. Marks 1, &f. Marks 2); Uni. MAP Sem. II-10/11 EKT 120: Computer Programming 12

Pass by Reference n Pass by reference is useful in two situations: n when

Pass by Reference n Pass by reference is useful in two situations: n when you want to return more than one value from a function n when the value of the actual parameter needs to be changed Uni. MAP Sem. II-10/11 EKT 120: Computer Programming 13

Sample application n n Write a C program that reads character and calculates numbers

Sample application n n Write a C program that reads character and calculates numbers of vowel and consonant Your program should have function: n n n fn. Read – read character fn. Find. Count. VC – determine and calculate number of vowel or consonant fn. Print - print number of vowel or consonant Uni. MAP Sem. II-10/11 EKT 120: Computer Programming 14

Sample application #include <stdio. h> #include <string. h> char fn. Read(); void fn. Find.

Sample application #include <stdio. h> #include <string. h> char fn. Read(); void fn. Find. Count. VC(char, int*); void fn. Print(int, int); Enter character : f Do you want to continue? y Enter character : I Do you want to continue? y Enter character : k Do you want to continue? n Number of vowel : 1 Number of consonant : 2 void fn. Find. Count. VC(char c. Ch 1, int *i. Vowel, int *i. Consonant) int main() { { char c. Ch, c. Choice; int i. Count. V=0, i. Count. C=0; switch(c. Ch 1) do Functions that “return” { case 'A': { c. Ch = fn. Read(); more than one value i. e. case 'a': fn. Find. Count. VC(c. Ch, &i. Count. V, &i. Count. C); arguments are passed by case 'E': printf("Do you want to continue? "); reference case 'e': scanf("%c", &c. Choice); case 'I': getchar(); case 'i': }while((c. Choice == 'y') ||(c. Choice =='Y')); case 'O': fn. Print(i. Count. V, i. Count. C); case 'o': return 0; case 'U': } case 'u': *i. Vowel = *i. Vowel +1; break; default: *i. Consonant = *i. Consonant + 1; char fn. Read() } { char c. Ch 1; } printf("Enter character : "); void fn. Print(int i. Vowel, int i. Consonant) scanf("%c", &c. Ch 1); { getchar(); printf("Number of vowel : %dn", i. Vowel); return(c. Ch 1); printf("Number of consonant : %dn", i. Consonant); } 15 }

Pass by Reference (Example) #include <stdio. h> void fn. Fun 1(int, int*); //function prototype

Pass by Reference (Example) #include <stdio. h> void fn. Fun 1(int, int*); //function prototype int main(void) { int i. A=5, i. B=10; printf("Before fun 1n”); printf(“i. A = %d i. B = %d”, i. A, i. B); fn. Fun 1(i. A, &i. B); //function call printf(“nn. After fun 1n”); printf(“i. A = %d i. B = %dn”, i. A, i. B); return 0; } void fn. Fun 1(int i. AA, int * i. BB)//function definition { i. AA++; *i. BB--; printf("nn. Inside fun 1n”); printf(“i. AA = %d i. BB = %d”, i. AA, i. BB); } Uni. MAP Sem. II-10/11 EKT 120: Computer Programming Output Before fun 1 i. A=5 i. B = 10 Inside fun 1 i. AA = 6 i. BB = 9 After fun 1 i. A = 5 i. B = 9 16

Recursive Functions n n n Recursion is a term describing functions which are called

Recursive Functions n n n Recursion is a term describing functions which are called by themselves (functions that call themselves) Recursive function has two parts i. e. base case and not base case If not base case, the function breaks the problem into a slightly smaller, slightly simpler, problem that resembles the original problem and n Launches a new copy of itself to work on the smaller problem, slowly converging towards the base case n Makes a call to itself inside the return statement Eventually the base case gets solved and then that value works its way back up to solve the whole problem Recursion is very useful in mathematical calculations and in sorting of lists Uni. MAP Sem. II-10/11 EKT 120: Computer Programming 17

Recursive Functions n Example: factorial n! = n * ( n – 1 )

Recursive Functions n Example: factorial n! = n * ( n – 1 ) * ( n – 2 ) * … * 1 n Recursive relationship: n ( n! = n * ( n – 1 )! ) 5! = 5 * 4! n 4! = 4 * 3!… Base case (1! = 0! = 1) n n Uni. MAP Sem. II-10/11 EKT 120: Computer Programming 18

Recursive Functions(Example) n Factorial 4 * 6 = 24 is returned Factorial(4) 4* 3

Recursive Functions(Example) n Factorial 4 * 6 = 24 is returned Factorial(4) 4* 3 * 2 = 6 is returned Factorial(3) 3* Factorial(2) 2* Factorial(1) 1 Uni. MAP Sem. II-10/11 2 * 1 = 2 is returned Value 1 is returned EKT 120: Computer Programming 19

Recursive Functions(Example) #include <stdio. h> int fn. Factorial(int); void main() { int n=4; printf(“Factorial

Recursive Functions(Example) #include <stdio. h> int fn. Factorial(int); void main() { int n=4; printf(“Factorial %d } is %d“, n, fn. Factorial(n)); int fn. Factorial(int i. N) { if(i. N <= 1) //base case return 1; else return ( i. N * fn. Factorial(i. N-1)); } Uni. MAP Sem. II-10/11 EKT 120: Computer Programming Call function name itself 20

End Week 6 – Functions (2) Q & A! Uni. MAP Sem. II-10/11 EKT

End Week 6 – Functions (2) Q & A! Uni. MAP Sem. II-10/11 EKT 120: Computer Programming 21