EKT150 INTRODUCTION TO COMPUTER PROGRAMMING

Slides:



Advertisements
Similar presentations
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Advertisements

Chapter 5 C Functions The best way to develop and maintain a large program is to divide it into several smaller program modules, each of which is more.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
 2007 Pearson Education, Inc. All rights reserved C Functions.
C Lecture Notes Functions (Cont...). C Lecture Notes 5.8Calling Functions: Call by Value and Call by Reference Used when invoking functions Call by value.
Chapter 4:Functions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 1 Functions Lecture 4 by Jumail Bin.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
MAHENDRAN CHAPTER 6. Session Objectives Explain Type of Functions Discuss category of Functions Declaration & Prototypes Explain User Defined Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Lecture 6 – Functions (2). Outline Recall - sample application functions that return no value functions that return a value Recall – global variable vs.
1. Function prototype Function prototype is a declaration; indicates the function exists Should have function name, return type and parameter Placed before.
 2007 Pearson Education, Inc. All rights reserved C Functions -Continue…-
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Functions (1)
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions 5.5Function Definitions 5.6Function Prototypes.
UniMAP SemII-09/10EKT120: Computer Programming1 Week 6 – Functions (2)
Chapter 5 – Functions II Outline Recursion Examples Using Recursion: The Fibonacci Series.
Week 6: Functions - Part 2 BJ Furman 01OCT2012. The Plan for Today Comments on midterm exam (next week in lab!) Review of functions Scope of identifiers.
UniMAP SemI-09/10EKT120: Computer Programming1 Week 5 – Functions (1)
Principles of Programming - NI Chapter 6: Function In this chapter, you will learn about Introduction to function User define function Function prototype.
KIC/Computer Programming & Problem Solving 1.  Header Files  Storage Classes  Scope Rules  Recursion Outline KIC/Computer Programming & Problem Solving.
Lecture 10: Modular Programming (functions) B Burlingame 13 April 2015.
UniMAP Sem2-09/10 DKT121:Fundamental of Computer Programming1 Functions (2)
Lecture-3 Functions and Recursion. C Preprocessor Includes header files like stdio.h Expands macros defined Handles conditional compilations PreprocessorProcessor.
1 CSC103: Introduction to Computer and Programming Lecture No 16.
Senem Kumova Metin // CS115 // FUNCTIONS CHAPTER 5.
C++ Programming Lecture 12 Functions – Part IV
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
Lecture 7: Modular Programming (functions) B Burlingame 05 October, 2016.
Function – I What is a function Why we need functions?
EKT120: Computer Programming
Functions Course conducted by: Md.Raihan ul Masood
User-Written Functions
Introduction to Programming
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
C Functions -Continue…-.
EKT120: Computer Programming
Lesson #6 Modular Programming and Functions.
Lesson #6 Modular Programming and Functions.
Week 6 – Functions (2) PGT C Programming.
Functions and Structured Programming
Functions Dr. Sajib Datta
CSC113: Computer Programming (Theory = 03, Lab = 01)
Quiz 2.
Module 4 Functions – function definition and function prototype.
PGT 106: Computer Programming
Week 5 – Functions (1) EKT120: Computer Programming.
EKT120: Computer Programming
CSC113: Computer Programming (Theory = 03, Lab = 01)
EKT120: Computer Programming
DKT121:Fundamental of Computer Programming
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 5 - Functions Outline 5.1 Introduction
Functions.
User-Defined Functions
Formatted and Unformatted Input/Output Functions
Chapter 5 - Functions Outline 5.1 Introduction
Lesson #6 Modular Programming and Functions.
Chapter 6 - Functions Outline 5.1 Introduction
Functions.
A function with one argument
Function In this lesson, you will learn about Introduction to Function
Lesson #6 Modular Programming and Functions.
Introduction to Problem Solving and Programming
In C Programming Language
Presentation transcript:

EKT150 INTRODUCTION TO COMPUTER PROGRAMMING Function Part II Dr. Nur Hafizah Ghazali hafizahghazali@unimap.edu.my

Recaps… Functions Prototype #include <stdio.h> /* function prototype */ double product(double x, double y); int main() { double var1 = 3.0, var2 = 5.0; double ans; /*function call*/ ans = product(var1, var2); printf("var1 = %.2lf\n" "var2 = %.2lf\n",var1,var2); printf("var1*var2 = %lf\n", ans); } /* function definition */ double product(double x, double y) double result; result = x * y; return result; Function prototype Function prototype is a declaration; indicates the function exists Should have function name, return type and parameter Placed before main () Tells compiler that the function will be defined later

Recaps… Functions Call #include <stdio.h> /* function prototype */ double product(double x, double y); int main() { double var1 = 3.0, var2 = 5.0; double ans; /*function call*/ ans = product(var1, var2); printf("var1 = %.2lf\n" "var2 = %.2lf\n",var1,var2); printf("var1*var2 = %lf\n", ans); } /* function definition */ double product(double x, double y) double result; result = x * y; return result; Consists of a function name followed by an argument expression list enclosed in parentheses Function call has the following form: <function_name> (exp, exp ...) main is the calling function product is the called function

Recaps… Functions Definition #include <stdio.h> /* function prototype */ double product(double x, double y); int main() { double var1 = 3.0, var2 = 5.0; double ans; /*function call*/ ans = product(var1, var2); printf("var1 = %.2lf\n" "var2 = %.2lf\n",var1,var2); printf("var1*var2 = %lf\n", ans); } /* function definition */ double product(double x, double y) double result; result = x * y; return result; Function definition includes the body of a function Function definition has the following form: <return_type> <function_name> (arg_type arg_name, ...) { … statements … }

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. int sum(int, int); //function prototype int sum(int num1, int num2)//function definition sum(x,y); //function call

Functions that RETURN a 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 call printf(“Sum is : %d”,result); return 0; } int sum(int num1, int num2){ //function definition int add; add = num1+num2; return add;

Functions that DO NOT RETURN a value //This program sums up two numbers #include <stdio.h> void sum_print(int, int); //function prototype int main(){ int x,y; function1(); //function call printf(“Enter x and y: ”); scanf(“%d %d”, &x, &y); sum_print(x,y); //function call return 0; } void sum_print(int num1, int num2){ //function definition int add; add = num1+num2; printf(“Sum is: %d”,add);

Today’s Outline Miscellaneous about functions Scope and mechanics of passing values to functions

Function Call as Logical Expression Example: int calc(int,int); void main(void) { int num1, num2; scanf(“%d %d”,&num1,&num2); if(calc(num1,num2)>100) //function call used as logical expression printf(“result greater than 100”); else printf(“result less than 100”); } int calc(int n1,int n2) { int answer; answer=n1+n2; return answer;

Function Call in printf Statement Example: int calc(int,int); void main(void) { int num1,num2; scanf(“%d %d”,&num1,&num2); //function call returns a value and puts in printf printf(“Answer: %d”,calc(num1, num2)); } int calc(int n1,int n2) int answer; answer=n1+n2; return answer;

Naming Convention in Function 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 function void main(void) { int num1,num2,result; //declare like this scanf(“%d %d”,&num1,&num2); result = calc(num1,num2); //function call printf(“jawapan : %d“,result); } int calc(int n1,int n2) //function definition //simply declare like this int answer; answer=n1+n2; return answer;

Sample Application 1: Write a C program that calculates and prints addition and subtraction of integers. Users have to enter 2 integers and an operator ( `+` or `-`) Your program should have functions: add : receive two integer from main function, add those integers and return the value subtract : receive two integer from main function, subtract those integers and return the value print_result : prints results from the calculations

Sample Application 1 (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); default: printf(“Invalid operator”); } 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); 04-9762700

Sample Application 2: Write a C program that determines the maximum values between 3 integers. Your program should consists of function: maximum : receive 3 integers, determine and return the maximum value

Sample Application 2 (Cont) #include <stdio.h> int maximum(int,int,int); int main() { int a,b,c; printf(“Enter 3 integers: ”); scanf(“%d %d %d”, &a, &b, &c); printf(“\n Maximum value is %d”, maximum(a,b,c)); return 0; } int maximum(int x,int y,int z) { int max=x; if(y>max) max=y; if(z>max) max=z; return max; } 04-9762700 Enter three integers: 22 85 17 Maximum is: 85

Scope and Mechanics of Passing Values to Functions Scope refers to the region in which a declaration is active Two types: Global Variable/ File Scope Local Variable/ Function Scope

Global and Local Variables //Compute Area & Perimeter of a circle #include <stdio.h> float pi = 3.14159; /* Global */ int main() { float rad; /* Local */ printf( “Enter the radius “ ); scanf(“%f” , &rad); if ( rad > 0.0 ) { float area = pi * rad * rad; float peri = 2 * pi * rad; printf( “Area = %f\n” , area ); printf( “Peri = %f\n” , peri ); } else printf( “Negative radius\n”); return 0; Global variable These variables are declared outside all functions, at the top of a source file. Declarations not placed in any functions Life time of a global variable is the entire execution period of the program. Can be accessed by any function defined below the declaration

Global Variable : Example #include <stdio.h> void ChangeGlobal( ); int global = 3; //This is the global variable void main(void) { printf("%d\n“, global); //Reference to global variable ChangeGlobal(); printf("%d\n", global); } void ChangeGlobal( ) global = 5; //Reference to global variable

Global Variable : Example #include <stdio.h> void ChangeGlobal( ); int global = 3; //This is the global variable void main(void) { printf("%d\n“, global); //Reference to global variable ChangeGlobal(); printf("%d\n", global); } void ChangeGlobal( ) global = 5; //Reference to global variable The output will be: 3 5

Global and Local Variables //Compute Area & Perimeter of a circle #include <stdio.h> float pi = 3.14159; /* Global */ int main() { float rad; /* Local */ printf( “Enter the radius “ ); scanf(“%f” , &rad); if ( rad > 0.0 ) { float area = pi * rad * rad; float peri = 2 * pi * rad; printf( “Area = %f\n” , area ); printf( “Peri = %f\n” , peri ); } else printf( “Negative radius\n”); return 0; Local variables These variables are declared inside some functions (in a block { … }) Life time is the entire execution period of the function in which it is defined. Cannot be accessed by any other function  scope is within its block In general variables declared inside a block are accessible only in that block.

Local Variable : Example #include <stdio.h> void ChangeLocal(); int main() { int local = 3; //This is a local variable printf("%d\n", local); //Reference to local variable ChangeLocal(); printf("%d\n", local); return 0; } void ChangeLocal() int local = 5; //This is another local variable

Local Variable : Example #include <stdio.h> void ChangeLocal(); int main() { int local = 3; //This is a local variable printf("%d\n", local); //Reference to local variable ChangeLocal(); printf("%d\n", local); return 0; } void ChangeLocal() int local = 5; //This is another local variable The output will be: 3 5

Sample Application Write a C program that reads item code and quantity, then calculates the payment. Use functions: fnMenu – print item code menu fnDeterminePrice – determine price based on item code fnCalc - calculate payment fnPrintResult – 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.

Sample Application: Local Variables #include <stdio.h> void fnMenu(); float fnDeterminePrice(int); float fnCalc(float,int); void fnPrintResult(float); int main() { int iCode,iQty; float fPriceUnit,fPay; fnMenu(); printf("Enter item code and quantity:"); scanf("%d %d", &iCode,&iQty); fPriceUnit = fnDeterminePrice(iCode); fPay = fnCalc(fPriceUnit,iQty); fnPrintResult(fPay); return 0; }

Sample Application: Local Variables #include <stdio.h> void fnMenu(); float fnDeterminePrice(int); float fnCalc(float,int); void fnPrintResult(float); int main() { int iCode,iQty; float fPriceUnit,fPay; fnMenu(); printf("Enter item code and quantity:"); scanf("%d %d", &iCode,&iQty); fPriceUnit = fnDeterminePrice(iCode); fPay = fnCalc(fPriceUnit,iQty); fnPrintResult(fPay); return 0; } void fnMenu() { printf("Code\tItem\tPrice\n"); printf("1\tPapaya\t1.00\n"); printf("2\tMelon\t2.00\n"); printf("3\tDurian\t3.00\n"); printf("\tOthers\t4.00\n"); }

Sample Application: Local Variables #include <stdio.h> void fnMenu(); float fnDeterminePrice(int); float fnCalc(float,int); void fnPrintResult(float); int main() { int iCode,iQty; float fPriceUnit,fPay; fnMenu(); printf("Enter item code and quantity:"); scanf("%d %d", &iCode,&iQty); fPriceUnit = fnDeterminePrice(iCode); fPay = fnCalc(fPriceUnit,iQty); fnPrintResult(fPay); return 0; } void fnMenu() { printf("Code\tItem\tPrice\n"); printf("1\tPapaya\t1.00\n"); printf("2\tMelon\t2.00\n"); printf("3\tDurian\t3.00\n"); printf("\tOthers\t4.00\n"); } OUTPUT: Code Item Price 1 Papaya 1.00 2 Melon 2.00 3 Durian 3.00 Others 4.00

Sample Application: Local Variables #include <stdio.h> void fnMenu(); float fnDeterminePrice(int); float fnCalc(float,int); void fnPrintResult(float); int main() { int iCode,iQty; float fPriceUnit,fPay; fnMenu(); printf("Enter item code and quantity:"); scanf("%d %d", &iCode,&iQty); fPriceUnit = fnDeterminePrice(iCode); fPay = fnCalc(fPriceUnit,iQty); fnPrintResult(fPay); return 0; } OUTPUT: Code Item Price 1 Papaya 1.00 2 Melon 2.00 3 Durian 3.00 Others 4.00 Enter item code and quantity: 1 3

Sample Application: Local Variables float fnDeterminePrice(int iItemCode) { float fPricing; switch(iItemCode) case 1: fPricing = 1.00;break; case 2: fPricing = 2.00;break; case 3: fPricing = 3.00;break; default:fPricing = 4.00; } return fPricing; #include <stdio.h> void fnMenu(); float fnDeterminePrice(int); float fnCalc(float,int); void fnPrintResult(float); int main() { int iCode,iQty; float fPriceUnit,fPay; fnMenu(); printf("Enter item code and quantity:"); scanf("%d %d", &iCode,&iQty); fPriceUnit = fnDeterminePrice(iCode); fPay = fnCalc(fPriceUnit,iQty); fnPrintResult(fPay); return 0; }

Sample Application: Local Variables #include <stdio.h> void fnMenu(); float fnDeterminePrice(int); float fnCalc(float,int); void fnPrintResult(float); int main() { int iCode,iQty; float fPriceUnit,fPay; fnMenu(); printf("Enter item code and quantity:"); scanf("%d %d", &iCode,&iQty); fPriceUnit = fnDeterminePrice(iCode); fPay = fnCalc(fPriceUnit,iQty); fnPrintResult(fPay); return 0; } float fCalc(float fItemPrice, int iQuality) { float fTotal; fTotal = fItemPrice*iQuantity; return fTotal; }

Sample Application: Local Variables #include <stdio.h> void fnMenu(); float fnDeterminePrice(int); float fnCalc(float,int); void fnPrintResult(float); int main() { int iCode,iQty; float fPriceUnit,fPay; fnMenu(); printf("Enter item code and quantity:"); scanf("%d %d", &iCode,&iQty); fPriceUnit = fnDeterminePrice(iCode); fPay = fnCalc(fPriceUnit,iQty); fnPrintResult(fPay); return 0; } void fnPrintResult(float fPayment) { printf("Payment is %.2f\n", fPayment); } OUTPUT: Code Item Price 1 Papaya 1.00 2 Melon 2.00 3 Durian 3.00 Others 4.00 Enter item code and quantity: 1 3 Payment is 3.00

Sample Application: Global Variables #include <stdio.h> void fnMenu(); void fnDeterminePrice(); void fnCalc(); void fnPrintResult(); int iCode,iQty; float fPriceUnit,fPay; int main() { fnMenu(); printf("Enter item code and quantity:"); scanf("%d %d", &iCode,&iQty); fnDeterminePrice(); fnCalc(); fnPrintResult(); return 0; } void fnDeterminePrice() { switch(iCode) case 1: fPriceUnit=1.00; break; case 2: fPriceUnit=2.00; break; case 3: fPriceUnit=3.00; default:fPriceUnit=4.00; }

Sample Application: Global Variables #include <stdio.h> void fnMenu(); void fnDeterminePrice(); void fnCalc(); void fnPrintResult(); int iCode,iQty; float fPriceUnit,fPay; int main() { fnMenu(); printf("Enter item code and quantity:"); scanf("%d %d", &iCode,&iQty); fnDeterminePrice(); fnCalc(); fnPrintResult(); return 0; } void fCalc() { fPay = fPriceUnit*iQty; }

Sample Application: Global Variables #include <stdio.h> void fnMenu(); void fnDeterminePrice(); void fnCalc(); void fnPrintResult(); int iCode,iQty; float fPriceUnit,fPay; int main() { fnMenu(); printf("Enter item code and quantity:"); scanf("%d %d", &iCode,&iQty); fnDeterminePrice(); fnCalc(); fnPrintResult(); return 0; } void fnPrintResult() { printf("Payment is %.2f\n", fPay); } OUTPUT: Code Item Price 1 Papaya 1.00 2 Melon 2.00 3 Durian 3.00 Others 4.00 Enter item code and quantity: 1 3 Payment is 3.00

Pass by Value 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 only manipulate the data stored in its own memory space

Pass by Value (Example) #include <stdio.h> void fnFun1(int,int); //function prototype int main() { int iA=5, iB=10; printf("Before function1\n“); printf(" iA = %d iB = %d\n”, iA,iB); fnFun1(iA, iB); //function call printf("\nAfter function1\n“); return 0; } void fnFun1(int iAA,int iBB) //function definition iAA++; iBB--; printf("\n\nInside fnFun1\n)"; printf(“iA = %d iB = %d\n”, iAA,iBB);

Pass by Value (Example) #include <stdio.h> void fnFun1(int,int); //function prototype int main() { int iA=5, iB=10; printf("Before fnFun1\n“); printf(" iA = %d iB = %d\n”, iA,iB); fnFun1(iA, iB); //function call printf("\nAfter fnFun1\n“); return 0; } void fnFun1(int iAA,int iBB) //function definition iAA++; iBB--; printf("\n\nInside fnFun1\n)"; printf(“iAA = %d iBB = %d\n”, iAA,iBB); OUTPUT Before fnFun 1 iA = 5 iB = 10 Inside fnFun 1 iAA = 6 iBB = 9 After fnFun 1 iA = 5 iB = 10

Functions that Return More than One Value When we talk about functions that return more than one value it refers to passing arguments by reference passes addresses (references), NOT value or data allows direct manipulation changes will affect original data 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

Pass by Reference 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 have direct control to the memory space of the actual variable Syntax: In function protoype 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 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 (Example) #include <stdio.h> void fnFun1(int, int*); //function prototype int main(void) { int iA=5,iB=10; printf("Before fun 1\n”); printf(“iA = %d iB = %d”,iA,iB); fnFun1(iA, &iB); //function call printf(“\n\nAfter fun 1\n”); printf(“iA = %d iB = %d\n”,iA,iB); return 0; } void fnFun1(int iAA,int *iBB)//function definition iAA++; *iBB--; printf("\n\nInside fnFun1\n”); printf(“iAA = %d iBB = %d”,iAA,iBB);

Pass by Reference (Example) #include <stdio.h> void fnFun1(int, int*); //function prototype int main(void) { int iA=5,iB=10; printf("Before fun 1\n”); printf(“iA = %d iB = %d”,iA,iB); fnFun1(iA, &iB); //function call printf(“\n\nAfter fun 1\n”); printf(“iA = %d iB = %d\n”,iA,iB); return 0; } void fnFun1(int iAA,int *iBB)//function definition iAA++; *iBB--; printf("\n\nInside fnFun1\n”); printf(“iAA = %d iBB = %d”,iAA,iBB); OUTPUT Output Before fnFun1 iA=5 iB = 10 Inside fnFun1 iAA = 6 iBB = 9 After fnFun1 iA = 5 iB = 9

Sample Application Write a C program that calculates and prints average of 2 test marks. Your program should have functions: fnReadMarks – read 2 test marks fnCalcAvg – calculate average of two test marks fnPrint - print average

Sample Application #include <stdio.h> Function that returns more than one value - arguments are passed by reference #include <stdio.h> void fnReadMarks(float*,float*); float fnCalcAvg(float,float); void fnPrint(float); int main(void) { float fMarks1, fMarks2, fAvg; fnReadMarks(&fMarks1,&fMarks2); fAvg = fnCalcAvg(fMarks1,fMarks2); fnPrint(fAvg); return 0; } void fnReadMarks(float *fM1,float *fM2) { printf("Enter marks for test1 and test2 : "); scanf("%f %f", fM1,fM2); //notice no & } float fnCalcAvg(float fM1, float fM2) return((fM1 + fM2)/2); void fnPrint(float fAverage) printf("\nAverage marks are :%.2f\n",fAverage);

Sample Application #include <stdio.h> Function that returns more than one value - arguments are passed by reference #include <stdio.h> void fnReadMarks(float*,float*); float fnCalcAvg(float,float); void fnPrint(float); int main(void) { float fMarks1, fMarks2, fAvg; fnReadMarks(&fMarks1,&fMarks2); fAvg = fnCalcAvg(fMarks1,fMarks2); fnPrint(fAvg); return 0; } void fnReadMarks(float *fM1,float *fM2) { printf("Enter marks for test1 and test2 : "); scanf("%f %f", fM1,fM2); //notice no & } float fnCalcAvg(float fM1, float fM2) return((fM1 + fM2)/2); void fnPrint(float fAverage) printf("\nAverage marks are :%.2f\n",fAverage); Enter marks for test1 and test2 : 70 80 Average marks are : 75.00

Recursive Functions 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 Launches a new copy of itself to work on the smaller problem, slowly converging towards the base case 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 Useful in mathematical calculations and in sorting of lists

Recursive Functions: Factorial Example: factorial n! = n * ( n – 1 ) * ( n – 2 ) * … * 1 Recursive relationship: ( n! = n * ( n – 1 )! ) Example: 4! 4! = 4 * 3! 3! = 3 * 2! 2!=2*1! 1!=1*0! Base case (1! = 0! = 1)

Recursive Functions: Factorial Factorial of 4 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: Factorial #include <stdio.h> int fnFactorial(int); void main() { int iN=4; printf(“Factorial %d is %d“,n, fnFactorial(n)); } int fnFactorial(int iN) if(iN <= 1) //base case return 1; else return ( iN * fnFactorial(iN-1)); Call function name itself

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

Recursive Functions (Example) Diagram of Fibonacci function: f( 3 ) f( 1 ) f( 2 ) f( 0 ) return 1 return 0 return + = 2 = 1 = 1

Recursive Functions (Example) Sample code for fibonacci function long fnFibonacci( long lN ) {  if ( lN == 0 || lN == 1 ) //base case return lN; else return fnFibonacci(lN–1)+ fnFibonacci(lN–2); }

End Q & A!