You are on page 1of 3

Experiment no-4.

TO DEMONSTRATE DIFFERENT COMPONENTS OF FUNCTIONS

OBJECTIVE:
 To learn about different components functions of C
 To learn to use functions in programs.

THEORY
A function is a group of connected statements used to carry out a certain activity. Every time the
function is called, the specific task is repeated. Every program must have a function called main() from
which execution will always start. Each module may be put into use separately before being merged to
form a single unit.
A function definition has four components:
1.Return type
2.Function name
3.Function parameters
4.Body of function
Syntax:
returnType functionName(functionParameters...){
//functionBody
}
DEMONSTRATION
Program 1 : To enter two values and add using function,
#include<stdio.h>
int add(int,int); //declaring function
void main()
{
int a,b,result;
printf("Enter the values of two numbers:");
scanf("%d%d",&a,&b);
result=add(a,b); //calling function
printf("\nThe sum=%d",result);
}
int add(int p,int q) //defining function
{
int sum;
sum=p+q;
return(sum);
}
Experiment no-4.1

Output:

Discussion:
In above program add function is added to program which adds two numbers. “ int” is the
returning type, the function name is “add” and the parameters are “(int p,int q)” and function body
defines to add the two numbers.

Program 2: To calculate the area and perimeter of a rectangle using function.


#include<stdio.h>
void area(int,int);
void perimeter(int,int);
void main()
{
int l,b;
printf("Enter length and breadth:");
scanf("%d%d",&l,&b);
area(l,b);
perimeter(l,b);
}
void area(int x,int y)
{
int area1;
area1=x*y;
printf("\n Area=%d",area1);
}
void perimeter(int x,int y)
{
int peri;
peri=2*(x+y);
printf("\n Perimeter=%d",peri);
}
Output:
Experiment no-4.1

Discussion:
In above program , function is initiated to find the area and perimeter of a rectangle. The functions have
return type “void” and function name “area” & “perimeter” with parameters”(int x , int y)”.

CONCLUSION:
Hence,different components of functions in c were demonstrated.

You might also like