C Language / Functions

It consists of a set of instructions used to perform a particular task and is used for code reusability. Functions are of 2 types they were

Functions Type Details
Predefined / Library functions Already defined functions we will use.
user defined function User defines / writes functions for his purpose.

Note
Normally large problem is divide it into smaller sub problems and each sub problems is implemented using a function and at last all functions are combined for a solution.

User defined Function Syntax
function-Name ()
{
Declaration statements;
Executable statements; // function body
}

Sample Program to add 2 numbers Output

#include<stdio.h>
void main()
{
printf("sum of 2 numbers = %d",add(10,10));
}
int add(int n1, int n2)
{
int total = n1 + n2;
return total;
}

sum of 2 numbers = 20

Calling and Called Functions
Calling Function is from where other function is called.
Called Function which is called or referred with or without arguments / parameters is called as called Function.

Sample Program to add 2 numbers using Output

#include<stdio.h>
void main()
{ printf("sum of 2 numbers = %d",add(10,10));
}

int add(int n1, int n2)
{
int total = n1 + n2;
return total;
}

sum of 2 numbers = 20

Calling and Called Functions
Calling Function main()
Called Function add(int n1, int n2)

Call by value and call by reference
call by value It means calling a function by passing value.
call by reference It means calling a function by reference of its name.

Example call by value
Sample Program Output
#include<stdio.h>  void main()
{
printf("sum of 2 numbers = %d",add(10,10));
}
int add(int n1, int n2)
{
int total = n1 + n2;
return total;
}

sum of 2 numbers = 20

Example for call by reference
Sample Program Output

#include<stdio.h>

void main()
{
printf("sum of 2 numbers = %d",add());
}

int add()
{
return 10 + 10;
}

sum of 2 numbers = 20

Recursion
The function calling itself is called as Recursion. It purpose is code reusability.

Recursion factorial Program Output

#include <stdio.h>
int factorial(unsigned int i)
{
if(i <= 1)
return 1;
return i * factorial(i - 1);
}

void main()
{
printf("5 Factorial = %d \n",factorial(5));
}

5 Factorial = 120
Fibonacci series recursion Program Output

void printFibseries(int);
void main()
{
int n=5;
printf("Fibonacci Series: ");
printFibseries(n);
}

void printFibseries(int n)
{
static long int a=0,b=1,c;
if(n>0)
{
c = a + b;
a = b;
b = c;
printf("%d\t",c);
printFibseries(n-1);
}
}

Fibonacci Series: 1
2
3
5
8
recursion program for Sum of digits of a number Output

#include <stdio.h>
int Numsum(int num);
void main()
{
int num, sum;
printf("Enter a number to find sum of digits: ");
scanf("%d", &num);
sum = Numsum(num);
printf("Sum of digits of %d = %d", num, sum);

}

int Numsum(int num)
{
if(num == 0)
return 0;
return ((num % 10) + Numsum(num / 10));
}

Enter any number to find sum of digits: 12345
Sum of digits of 12345
= 15
GCD recursion program for 2 numbers Output

#include<stdio.h>
int gcd(int n1, int n2);
void main()
{
int n1, n2;
printf("Enter 2 Numbers\n");
scanf("%d %d", &n1, &n2);
printf("G.C.D of %d and %d is %d.", n1, n2,
gcd(n1,n2));
}

int gcd(int n1, int n2)
{
if (n2 != 0)
return gcd(n2, n1%n2);
else
return n1;
}

Enter 2 Numbers
10
20
G.C.D of 10 and 20 is
10.


Home     Back