Programs to learn functions and recursive functions.
Function : A function is a block of statements that performs a specific task. A function is a set of statements that take inputs, do some specific computation and produces output.
Types of functions
- Predefined standard library functions – such as puts(), gets(), printf(), scanf() etc – These are the functions which already have a definition in header files (.h files like stdio.h), so we just call them whenever there is a need to use them.
- User Defined functions – The functions that we create in a program are known as user defined functions.
Use of Functions :
- To improve the readability of code.
- Improves the reusability of the code, same function can be used in any program rather than writing the same code from scratch.
- Debugging of the code would be easier if you use functions, as errors are easy to be traced.
- Reduces the size of the code, duplicate set of statements are replaced by function calls.
Function Declaration
Syntax : return_type function_name(parameters) { //statements } Example : void fun(int x) { x = 30; } |
---|
Parameter Passing to functions
- The parameters passed to function are called actual parameters.
- The parameters received by function are called formal parameters.
There are two most popular ways to pass parameters.
Pass by Value: In this parameter passing method, values of actual parameters are copied to function’s formal parameters and the two types of parameters are stored in different memory locations. So any changes made inside functions are not reflected in actual parameters of caller.
Example :
#include <stdio.h> #include<conio.h> void f(int x) { x = 3; } int main(void) { int x = 2; f(x); printf("x = %d", x); return 0; } |
---|
Output :
x=2
Pass by Reference : Both actual and formal parameters refer to same locations, so any changes made inside the function are actually reflected in actual parameters of caller. In C references are passed using pointers .
Example :
# include <stdio.h> #include<conio.h> void f(int *p) { *p = 3; } int main() { int x = 2; f(&x); printf("x = %d", x); return 0; } |
---|
Output :
x=3
Example :
#include<stdio.h> #include<conio.h> int sum(int x, int y) //x and y are formal parameters. { return x+y; } // main function that doesn't receive any parameter and // returns integer. int main(void) { int a = 10, b = 20; //10 and 20 are actual parameters. // Calling above function to find max of 'a' and 'b' int m = sum(a, b); printf("sum is %d", m); return 0; } |
---|
Output :
sum is 30
Recursion : When a function call itself , then this process is called recursion .
Example-1 :
void f() { f(); // Function calls itself } int main() { f(); // Sets off the recursion return 0; } |
---|
Example-2 :
#include<stdio.h> #include<conio.h> int fib(int i) { if(i == 0) { return 0; } if(i == 1) { return 1; } return fib(i-1) + fib(i-2); } int main() { int i; for (i = 0; i < 10; i++) { printf("%d\t\n", fib(i)); } return 0; } |
---|
Output :
0
1
1
2
3
5
8
13
21
34
Advantages :
- Reduce unnecessary calling of function.
- Through Recursion one can Solve problems in easy way while its iterative solution is very big and complex.
Disdvantages :
- Recursive solution is always logical and it is very difficult to trace.(debug and understand).
- Recursion takes a lot of stack space, usually not considerable when the program is small and running on a PC.
- Recursion uses more processor time.
For more RTU/BTU II Sem Computer Programming Lab Experiments CLICK HERE