Chapter 5 : Function and Recursion
Function and Recursion
Sometimes our program gets bigger in size and its not possible for a programmer to track which piece of code is doing what function is a way to break our code into chunks so that it is possible for a programmer to reuse them.
What is a Function?
A function is a block of code that performs a particular task,
A function can be reused by the programmer in a given program any number of time
#include<stdio,h>
void display () ——> function prototype
int main(){
int a;
display(); ———> function call
return 0;
}
void display () { ———> function definition
printf(”Hi i am display”);
}
Function prototype
Function prototype Declaration
The function prototype is a way to tell the compiler about the function we are going to define in the program
Here void indicates that the function returns nothing
Function call
Function call is a way to tell the compiler to execute the function body at the time the call is made
Note that the program execution starts from the main function in the sequence the instructions are written
Function definition
This part contains the exact set of instructions which are executed during the function call when a function is called from main(), the main function falls asleep and gets temporarily suspended. During this time the control goes to the function being called . When the function body is done executing main() resumes.
main() should call all of these in order 1 →2→3
Important points
→ execution of a C program starts from main()
→ A c program can have more than one function
→ Every function gets called directly or indirectly from main()
→ There are two types of function in C. Lets talk about them
Types of functions
- Library functions→ Commonly requires function grouped together in a library file on disk.
- User defined Functions→ These are the functions declared and defined by the user
why use functions?
- To avoid rewriting the same logic again and again
- To keep track of what we ar doing in a program
- To test and check logic independently.
Passing values to functions
We can pass values to a function and can get value in return from a function
int sum(int a, int b)
The above prototype means that sum is a function that takes values a(of type int) and b (of type in) and returns a value of type int
Function definition of sum can be;
int sum (int a , int b) {
int c; ==⇒ a and b are parameters
c= a+b;
return c;
}
Now we can call sum (2,3); from main to get 5 in return . |—→ Here 2&3 are arguments
int d=sum (2,3); ⇒ d becomes 5
Note :
- Parameters are the value or variable placeholders in the function definition. Ex a & b.
- Arguments are the actual values passed to the function to make a call . Ex 2&3
- A function can return only one value at a time
- If the passed variable is changed inside the function, the function call doesn’t change the value in the calling function
int change (int a) {
a=77; ⇒ Misnomer
return0;
}
Change is a function which changes a to 77 No if we call it from amin like this
int b =22
change (b); =. The value of b remains 22
printf(”b is %d”, b); print “b is 22”
This happen because a copy of b is passed to the change function
Recursion
A function defined in c program can call itself . This is called recursion.
A Function calling itself is also called a ‘recursive’ function.
Example of recursion
A very good example of recursion is factorial
factorial (x) = 123…*n
factorial (x) = 123….n-1 *n
factorial (x) = factorial n-1 *n
Since we can write factorial of a number in terms of itself, we can program it using recursion.
int factorial ( int x) {
int f;
if (x==0 || x==1)
return 1;
else
f=x*factorial (x-1);
return f;
}
Important notes
- Recursion is sometimes the most direct way to code an algorithm
- The condition which doesn’t call the function any further in a recursive function is called as the base condition.
- Sometimes due to a mistake is made by the programmer, a recursive function cdan keep running without returning resulting in a memory error.
Comments
Post a Comment