GE 6151- Computer Programming
Functions in C
The function is a derived data type which is a sub-program which
contains one or more statements to perform some tasks.
The job of functions is to divide the large program to many separate
modules based on their functionality.
There are 4 types of functions in C.
1. Functions with no arguments and no return value.
2. Functions with arguments but no return value.
3. Function with no arguments but with return value.
4. Functions with arguments and with return value.
1) Functions with no arguments and no return values:
Syntax :
function_name ()
{
Valid C Statements;
}
Sample Program :
#include<stdio.h>
#include<conio.h>
funct();
void main()
{
funct();
getch();
}
funct()
{
printf("Functions with no arguments and no return value");
}
2) Functions with arguments and no return values :
Syntax :
function_name(argument 1, - - - -, argument n)
{
Valid C Statements;
}
Sample Program :
#include<stdio.h>
#include<conio.h>
funct(int a1, int b1);
void main()
{
K. Balasubramanian, AP/CSE, EGSPEC 1
GE 6151- Computer Programming
int a,b;
a=10;
b=20;
funct(a,b);
getch();
}
funct(int a1, int b1)
{
int c;
c=a1+b1;
printf("\n Result = %d",c);
}
3) Function with no arguments but with return values :
Syntax :
data_type function_name()
{
Valid C Statements;
return(value);
}
Sample Program :
#include<stdio.h>
#include<conio.h>
int funct();
void main()
{
int result;
result=funct();
printf("\n Result = %d",result);
getch();
}
int funct()
{
int a,b,c;
a=10;
b=20;
c=a+b;
return(c);
}
4) Functions with arguments and with return values :
Syntax :
data_type function_name(arguments)
{
K. Balasubramanian, AP/CSE, EGSPEC 2
GE 6151- Computer Programming
Valid C Statements;
return(value);
}
Sample Program :
#include<stdio.h>
#include<conio.h>
int funct(int a1, int b1);
void main()
{
int a,b,result;
a=10;
b=20;
result=funct(a,b);
getch();
}
int funct(int a1, int b1)
{
int c;
c=a1+b1;
printf("\n Result = %d",c);
return(c);
}
Function call by Value
The call by value method of passing arguments to a function copies
the actual value of an argument into the formal parameter of the
function.
In this case, changes made to the parameter inside the function have
no effect on the argument.
Example Program:
void swap(int x, int y)
{
int temp;
temp = x;
x = y;
y = temp;
return;
}
#include <stdio.h>
void swap(int x, int y);
int main ()
K. Balasubramanian, AP/CSE, EGSPEC 3
GE 6151- Computer Programming
{
int a = 100;
int b = 200;
printf("Before swap, value of a : %d\n", a );
printf("Before swap, value of b : %d\n", b );
swap(a, b);
printf("After swap, value of a : %d\n", a );
printf("After swap, value of b : %d\n", b );
return 0;
}
Function call by reference
The call by reference method of passing arguments to a function
copies the address of an argument into the formal parameter.
Inside the function, the address is used to access the actual argument
used in the call.
It means the changes made to the parameter affect the passed
argument.
Example Program:
void swap(int *x, int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
return;
}
#include <stdio.h>
void swap(int *x, int *y);
void main ()
{
int a = 100;
int b = 200;
printf("Before swap, value of a : %d\n", a );
printf("Before swap, value of b : %d\n", b );
swap(&a, &b);
printf("After swap, value of a : %d\n", a );
printf("After swap, value of b : %d\n", b );
}
K. Balasubramanian, AP/CSE, EGSPEC 4
GE 6151- Computer Programming
Recursion
Recursion is the process of calling a same function again and again by
itself
Number Factorial
The following example calculates the factorial of a given number
using a recursive function −
#include <stdio.h>
int factorial(int i)
{
if(i <= 1)
{
return 1;
}
return i * factorial(i - 1);
}
void main()
{
int i = 15;
printf("Factorial of %d is %d\n", i, factorial(i));
getch();
}
K. Balasubramanian, AP/CSE, EGSPEC 5