Week 6 Functions 2 PGT 106 C Programming

  • Slides: 25
Download presentation
Week 6 – Functions (2) PGT 106 - C Programming 1

Week 6 – Functions (2) PGT 106 - C 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 PGT 106 - C 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; } PGT 106 - C 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

Global Variable vs. Local Variable #include <stdio. h> modification void fn. Menu(); float fn.

Global Variable vs. Local Variable #include <stdio. h> modification void fn. Menu(); float fn. Determine. Price(int); float fn. Calc(float, int); void fn. Print. Result(float); int i. Code, i. Qty; float f. Price, f. Pay; int main() { fnmenu(); 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; } 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. Code) Output: { Code Item Price i. Code--; 1 Papaya 1. 00 switch(i. Code) 2 Melon 2. 00 { 3 Durian 3. 00 case 1: f. Price=1. 00; break; Others 4. 00 case 2: f. Price=2. 00; break; Enter item code and quantity: 1 4 case 3: f. Price=3. 00; break; Payment is 16. 00 default: f. Price=4. 00; } Output: return(f. Price); Code Item Price } 1 Papaya 1. 00 float fn. Calc(float f. Price, int i. Quantity) 2 Melon 2. 00 { 3 Durian 3. 00 f. Pay=f. Pay+1; Others 4. 00 f. Pay=f. Price*i. Quantity; Enter item code and quantity: 3 1 return(f. Pay); Payment is 2. 00 } void fn. Print. Result(float f. Pay) { printf("Payment is %. 2 fn", f. Pay); } However, sometimes we need to do some modifications from inside a function; using global variable will make things worse!!! PGT 106 - C Programming 6

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 PGT 106 - C Programming 7

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; } 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); } PGT 106 - C Programming 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 8

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 PGT 106 - C Programming 9

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 PGT 106 - C Programming 10

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 PGT 106 - C Programming 11

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; } Function that returns more than one value arguments are passed by reference 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; 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); } Output Enter marks for test 1 and test 2 : 70 80 Average marks are : 75. 00 PGT 106 - C Programming 12

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); PGT 106 - C Programming 13

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 PGT 106 - C Programming 14

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 PGT 106 - C Programming 15

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); } 16 }

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); } PGT 106 - C 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 17

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 PGT 106 - C Programming 18

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 PGT 106 - C Programming 19

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* 2 * 1 = 2 is returned Factorial(1) 1 Value 1 is returned PGT 106 - C Programming 20

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

Recursive Functions(Example) #include <stdio. h> int fn. Factorial(int); void main() { int i. 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)); } PGT 106 - C Programming Call function name itself 21

Recursive Functions (Example) n Fibonacci series: 0, 1, 1, 2, 3, 5, 8. .

Recursive Functions (Example) n Fibonacci series: 0, 1, 1, 2, 3, 5, 8. . . n n Each number is the sum of two previous numbers Example of a recursive formula: fib(n) = fib(n-1) + fib(n-2) PGT 106 - C Programming 22

Recursive Functions (Example) n Diagram of Fibonacci function: f( 3 ) return f( 1

Recursive Functions (Example) n Diagram of Fibonacci function: f( 3 ) return f( 1 ) return 1 f( 2 ) + + f( 0 ) f( 1 ) return 1 return 0 PGT 106 - C Programming 23

Recursive Functions (Example) n Sample code for fibonacci function long fn. Fibonacci( long l.

Recursive Functions (Example) n Sample code for fibonacci function long fn. Fibonacci( long l. N ) { if ( l. N == 0 || l. N == 1 ) //base case return l. N; else return fn. Fibonacci( l. N - 1 ) + fn. Fibonacci( l. N – 2 ); } PGT 106 - C Programming 24

End Week 6 – Functions (2) Q & A! PGT 106 - C Programming

End Week 6 – Functions (2) Q & A! PGT 106 - C Programming 25