UNIT 4
USER DEFINED FUNCTIONS
Similar to other languages C language also provides the facility of function. Function is the block of code which is used to perform a specific task. In c language the complete program is composed of function.
Functions are useful to divide c programs into smaller modules. Programmer can invoked these modules anywhere inside c program for any number of times.
Functions are used to increase readability of the code. Size of program can be reduce by using functions. By using function, programmer can divide complex tasks into smaller manageable tasks and test them independently before using them together.
Functions of C language are defined with the type of function. The type of functions indicates the data type of value which will return by function. In order to use function in the program, initially programmer have to inform compiler about the function. This is also called as defining a function.
In C programme all the function definition present outside the main function. All function need to be declared and defined before use. Function declaration requires function name, argument list, and return type.
Return Type Function name (Argument list)
{
Statement 1;
Statement 2;
…………...
Statement n;
}
User Defined Functions
User define function is the block of code written by programmer to perform a particular task. As compiler doesn’t have any idea about the user define function so programmer has to define and declare these functions inside the program body. Programmer can define these function outside the main function but declaration of user define function should present in main function only. Whenever compiler executes function call (function declaration) then compiler shift the flow of program execution to the definition part of user define function.
Example
#include <stdio.h>
#include<conio.h>
int add (int x, int y)
{
int sum;
sum = x + y;
return (sum);
}
main ()
{
int a,b,c;
a = 15;
b = 25;
c = add(a,b);
printf ("\n Addition is %d ", c);
}
Output:
Addition is 40
There are two ways to pass the parameters to the function
- Parameter Passing by value
In this mechanism, the value of the parameter is passed while calling the function.
2. Parameter Passing by reference
In this mechanism, the address of the parameter is passed while calling the function.
Recursion
In programming languages, if a program allows you to call a function inside the same function, then it is called a recursive call of the function.
void recursion() {
recursion(); /* function calls itself */
}
int main() {
recursion();
}
The C programming language supports recursion, i.e., a function to call itself. But while using recursion, programmers has to be careful to define an exit condition from the function, otherwise it will go into an infinite loop.
Recursive functions are useful to solve many mathematical problems, such as calculating the factorial of a number, generating Fibonacci series, etc.
The following example calculates the factorial of a given number using a recursive function − #include <stdio.h> unsigned long long int factorial(unsigned int i) { if(i <= 1) { return 1; } return i * factorial(i - 1); | ||
} int main() { int i = 12; printf("Factorial of %d is %d\n", i, factorial(i)); return 0; } When the above code is compiled and executed, it produces the following result − Factorial of 12 is 479001600 The following example generates the Fibonacci series for a given number using a recursive function − #include <stdio.h> int fibonacci(int i) { if(i == 0) { return 0; } if(i == 1) { | ||
return 1; } return fibonacci(i-1) + fibonacci(i-2); } int main() { int i; for (i = 0; i < 10; i++) { printf("%d\t\n", fibonacci(i)); } return 0; } When the above code is compiled and executed, it produces the following result − 0 1 1 2 3 5 8 13 21 34
| ||
Finding Factorial of a positive integers
The factorial of a positive number is the product of the integral values from 1 to number itself. Eg. 5!=5*4*3*2*1 120 Recursive Factorial: The factorial algorithm can be defined recursively as follows Factorial(n) = 1 if n=0 n*(Factorial(n-1)) if n>0
Algorithm: Calculate factorial of n fact(n)
return 1 2. else return(n*fact(n-1)) 3. endif end fact(n) |
Fig.9 Recursive Factorial
P12: Program to find factorial of a number using recursion.
#include<stdio.h> int rec (int x); void main() } int rec (int x) |
Fibonacci Series
The first two numbers of Fibonacci series are 0,1. The next number is the sum of previous two numbers.
Eg. 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...
- 1 is calculated by adding previous two numbers(0+1)
- 2 is calculated by adding the two numbers before it (1+1)
- Similarly, the 3 is found by adding the two numbers before it (1+2), and so on.
FIBONACCI SERIES using RECURSION:
Fibonacci(n) = 0 if n=0
1 if n=1
Fibonacci(n-1)+Fibonacci(n-2)) otherwise
Algorithm:
int fib(int n)
{
- if (n <= 1)
return n;
2. else
return (fib(n-1) + fib(n-2));
}
Fig.10 Fibonacci series using Recursion
P13: Program to print Fibonacci series using recursion.
#include<stdio.h> #include<conio.h> int Fibonacci(int); void main() { int n, i, c=0;// n is no. of terms in Fibonacci series. printf(“Enter no. of terms in Fibonacci series”); scanf("%d",&n); | ||
printf("Fibonacci series\n"); for( i= 1 ; i <= n ; i++ ) { printf("%d\n", Fibonacci(c)); c++; } getch(); } int Fibonacci(int x) { if ( x == 0 ) return 0; else if ( x == 1 ) return 1; else return ( Fibonacci(x-1) + Fibonacci(x-2) ); } | ||
References:
- The C programming language by Dennis Ritchie
- C programming by K.N. King
- The Complete Reference C Fourth Edition by Herbert Schilt
- Computer Fundamentals and Programming in C by Reema Theraja.
- C in a nutshell by Peter Prinz.