Professional Documents
Culture Documents
NMK10603 - Chapter 4 - Functions - Part 2
NMK10603 - Chapter 4 - Functions - Part 2
NMK10603 - Chapter 4 - Functions - Part 2
FUNCTIONS
(PART 2)
Chapter 4
Outline
2
Think!! Which
What argument
function returns no
names do I want
value and which
to feed in as
function returns a
parameters and
value.
what to return??
Local Variable vs. Global Variable
4
#include <stdio.h> float fnDeterminePrice(int iItemCode)
void fnMenu(); {
float fnDeterminePrice(int); float fPricing;
float fnCalc(float,int); switch(iItemCode)
Code Item Price
void fnPrintResult(float); { 1 Papaya 1.00
case 1:fPricing = 1.00;break; 2 Melon 2.00
int main() case 2:fPricing = 2.00;break; 3 Durian 3.00
{ case 3:fPricing = 3.00;break; Others 4.00
int iCode,iQty; Enter item code and quantity: 1 3
Local variable default:fPricing = 4.00; Payment is 3.00
float fPrice,fPay; }
fnMenu(); return(fPricing);
printf("Enter item code and quantity: "); }
scanf("%d %d", &iCode,&iQty); float fnCalc(float fItemPrice, int iQuantity)
fPrice = fnDeterminePrice(iCode);
{
fPay = fnCalc(fPrice,iQty);
float fTotal;
fnPrintResult(fPay);
return 0; fTotal = fItemPrice*iQuantity;
} return(fTotal);
void fnMenu() }
{ void fnPrintResult(float fPayment)
printf("Code\tItem\tPrice\n"); {
printf("1\tPapaya\t1.00\n"); printf("Payment is %.2f\n", fPayment);
printf("2\tMelon\t2.00\n"); }
printf("3\tDurian\t3.00\n");
printf("\tOthers\t4.00\n");
}
Local Variable vs. Global Variable
5
#include <stdio.h> float fnDeterminePrice(int iCode)
void fnMenu(); {
float fnDeterminePrice(int); switch(iCode)
float fnCalc(float,int); {
void fnPrintResult(float); case 1:fPrice=1.00;break;
int iCode,iQty; case 2:fPrice=2.00;break;
Global variable
float fPrice,fPay; case 3:fPrice=3.00;break;
int main() default:fPrice=4.00;
{ }
fnMenu(); return(fPrice);
Output:
printf("Enter item code and quantity:"); } Code Item Price
scanf("%d %d", &iCode,&iQty); float fnCalc(float fPrice,int iQty) 1 Papaya 1.00
fPrice= fnDeterminePrice(iCode); { 2 Melon 2.00
3 Durian 3.00
fPay=fnCalc(fPrice,iQty); fPay=fPrice*iQty; Others 4.00
fnPrintResult(fPay); return(fPay); Enter item code and quantity: 3 3
return 0; } Payment is 9.00
} void fnPrintResult(float fPay)
void fnMenu( ) {
{ printf("Payment is %.2f\n", fPay);
printf("Code\tItem\tPrice\n"); }
printf("1\tPapaya\t1.00\n");
printf("2\tMelon\t2.00\n"); Global variable can be used by any statements that are
printf("3\tDurian\t3.00\n"); being executed in the system
printf("\tOthers\t4.00\n");
}
Pass by Value
6
#include <stdio.h>
void fnFun1(int,int); //function prototype Output
int main(void) Before fnFun 1
{ iA = 5 iB = 10
int iA=5, iB=10;
printf("Before fun 1\n"); Inside fnFun 1
printf(" iA = %d iB = %d\n", iA,iB); iAA = 6 iBB = 9
fnFun1(iA, iB); //function call
printf("\nAfter fun 1\n"); After fnFun 1
printf(" iA = %d iB = %d\n", iA,iB);
iA = 5 iB = 10
return 0;
}
void fnFun1(int iAA,int iBB) //function definition
{
iAA++;
iBB--;
printf("\n\nInside fun 1\n");
printf("iAA = %d iBB = %d\n", iAA,iBB);
}
Functions that return more than one value
8
Output
Enter marks for test1 and test2 : 70 80
Average marks are : 75.00
Pass by Reference
11
Syntax:
In function prototype and function definition, put the * (star)
after the data type
Example : void fnReadMarks(float *,float *);
In function call, put the &(ampersand) before the argument
name to be passed by reference
Example : fnReadMarks(&fMarks1,&fMarks2);
Pass by reference is useful in two situations:
when you want to return more than one value from a
function
when the value of the actual parameter needs to be changed
Pass by Reference (Sample Application)
13
#include <stdio.h>
void fnFun1(int, int*); //function prototype Output
int main(void)
Before fun 1
{
int iA=5,iB=10; iA=5 iB = 10
printf("Before fun 1\n");
printf("iA = %d iB = %d",iA,iB); Inside fun 1
fnFun1(iA, &iB); //function call iAA = 6 iBB = 9
printf("\n\nAfter fun 1\n");
printf("iA = %d iB = %d\n",iA,iB);
After fun 1
return 0;
} iA = 5 iB = 9
void fnFun1(int iAA, int *iBB) //function definition
{
iAA++; Put (*) before the variable
(*iBB)--;
printf("\n\nInside fun 1\n");
printf("iAA = %d iBB = %d",iAA,*iBB);
}
Recursive Functions
16
Example: factorial
n! = n * ( n – 1 ) * ( n – 2 ) * … * 1
Recursive relationship:
( n! = n * ( n – 1 )! )
5! = 5 * 4!
4! = 4 * 3!…
Base case (1! = 0! = 1)
Recursive Functions (Example
18
Factorial
4 * 6 = 24 is returned
Factorial(4)
4* Factorial(3) 3 * 2 = 6 is returned
3* Factorial(2)
2 * 1 = 2 is returned
2* Factorial(1)
Value 1 is returned
1
Recursive Functions (Example)
19
#include <stdio.h>
int fnFactorial(int);
void main()
{
int N = 4;
printf("Factorial %d is %d",N, fnFactorial(N));
}
void main()
{
int N = 4;
printf("Fibonacci %d is %d",N, fnFibonacci(N));
}
int fnFibonacci( int lN )
{
if ( lN == 0 || lN == 1 ) //base case
return lN;
else
Q&A