A C-language program is nothing but collection of Function, these are the building blocks of a ‘C’ program. Generally, a function mans a task.
“Function is a collection of logically related instructions which performs a particular task.”
Functions are also used for modular programming in which a big task is divided into small modules and all the modules are inter connected.
How to create the function (Function declaration):-
(parameter list)
{
Body of the function;
[return];
}
: It is specify the data type of the returning value.
:It is specify the name of the function.
(Parameter list): It is the list of parameters used in the function.
Function definition:-
The collection of program statements in C that describes the specific task done by the function is called a Function definition. It consists of the function header and a function body.
(parameter list)
{
;
;
;
}
Function header:
Function header is similar to the function declaration but does not require the semicolon at the end. List of variables in the parenthesis are called Formal parameters. It consists of three parts:
- Return type.
- Function name.
- Formal parameters.
Example:
int add(a, b)
Function body:
After the function header the statements in the function body (including variables and statements) called body of the function.
Example:
int add (int x, int y)
{
return (x+y);
}
Return statement:
Return statement is used return some value given by the executable code within the function body to the point from where the call was made.
General form:
return expression;
or
return value;
Where expression must evaluate to a value of the type specified in the function header for the return value.
EX: return a+b;
Or
return a;
If the type of return value has been specified as void, there must be no expression appearing in the return statement it must be written simply as
return;
Function call (Function calling):
All functions within standard library or user-written, are called with in this main() function, the function call statements involves the function, which means the program control passes to that of the function. Once the function complete its task, the program control is passed back to the calling environment.
Syntax:
<(parameter list)>;
or
variable name=<(parameter list)>;
Example:
1.Write a program to find the mean of the two integer numbers.
#include
int mean(int, int);
main()
{
int p, q, m;
clrscr();
printf(“enter p,q values”);
scanf(“%d%d”,&p,&q);
m=mean(p,q);
printf(“mean value is %d”,m);
getch();
}
int mean(int x, int y)
{
int temp;
temp=(x+y)/2;
return temp;
}
- Write a program that uses a function to check whether a given year is leap or not.
#include
void leap(int);
main()
{
int year;
clrscr();
printf(“\n enter year \n”);
scanf(“%d”,&year);
leap(year);
}
void leap(int yr)
{
if(year%4==0&&year%100!=0||year%400==0)
printf(“leap year”);
else
printf(“not leap year”);
}
Types of functions:
The functions are also used for modular programming in which a big task is divided into small modules and all the modules are interconnected.
Functions are basically classified as:
- Standard library functions(built-in functions).
- user defined functions.
Standard library functions:-
library functions come along with the compiler and they can be used in any program directly. User can’t be modified the library functions.
Example:
sqrt();, printf();, clrscr();, abs();, strlen();, getch();etc………………
Example program:
#include
main()
{
int x;
clrscr();
printf(“enter x value \n”);
scanf(“%d”,&x);
printf(“square root value of x is %d”, sqrt(x));
getch();
}
user defined functions: user defined functions is one which will be defined by the user in program to group certain statements together. The scope of a user defined function is to the extent of the program only, in which it has been defined. main is also a user defined function.
Syntax:
)
{
Function body;
;
}
Example program:
#include
main()
{
int r,rd;
clrscr();
printf(“enter radius for circle\n”);
scanf(“%d”,&r);
rd=area(r);
printf(“area of the circle is %d”,rd);
getch();
}
area(int x)
{
return(3.14*x*x);
}
Types of parameters
The nature of data communication between the calling function and the called function with arguments (parameter).
The parameters are two types:
- Actual parameters or original parameters.
- formal parameters or duplicate parameters.
main()
{
………….
Function1(a1, a2, a3,………am);
………….
}
Function1(x1, x2, x3,………xn);
{
……….
…….....
……….
}
Actual parameters(arguments):
Actual parameters means original parameters for the function call. These parameters are declared at the time of function declaration. In the above example a1,a2,a3,…… am are the actual parameters. When a function call is made, only a copy of the values of actual arguments is passed into the called function.
Formal parameters (duplicate arguments):
Formal parameters means duplicate parameters for function call. These parameters are declared at the time of function definition. In the above example x1, x2, x3,………xn are the formal parameters, the actual and formal arguments should match in the number, type and order, the values of actual arguments are assigned to the formal arguments on a one to one basis, starting with the first argument.
Types of functions based on parameters:
A function depending on whether arguments are pres