Professional Documents
Culture Documents
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.
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.