Function In C
Modular programming:
Modular programming is programming concept. This concept allow us to divide a large solution into several small modules. Each module is devloped independently and act is a solution of small problem.
Need/Advantages of modular programming :
- A module developed once and reuse several times in a program.
- Due to readability, A problem will have less no of instruction.
- A problem has less numbers of instruction has less number of errors.
- program is easily maintainable .
- program is easily extendable.
In C language to apply modular programming we use function or structures.
Function:-
A function is an individual unit of small set of instruction to solve a specific task. Function may be inbuilt or user Define.
User define function:- If a function not exist in language, it can be devloped by the user. The development of a function has 3 steps.
- Function declaration ( Prototype )
- Function calling
- Function definition
Function declaration:-
Function declaration is a statement that declare a user define function, It describe the name of function, and return type of a function. The definition of a function must be placed out of other function in C language.
e.g.
Argument :-
Argument can be value or variable which given to a function during it’s calling.
Function calling :-
This is a statement where we call a function by using it’s name. During calling a function we have to pass all the Argument to the function.we can call a function several times.
Function Definition:-
Function Definition is the body of function (set of instruction). this body will execute when we call a function.The defection begin from header row. This header row contain return type,name of function and Argument variable of a function. A function definition may/may not return a value.
Defection of a function can not be placed within another function. Definition must be placed in globle scope of program.
A simple Function who calculate average of 4 integers :
#include<stdio.h>;
void main()
{
int A,B,C;
float res;
// here we are declaring a function
float average ( int , int , int , int );
A = 12, B = 23 , C = 34;
//now i am calling my function
res = average(A, B, C, 45 );
// see how to pass Arguments
printf("Result is %.2f n",res);
// here %.2f will allow 2 values printing after decimel
}
// now i am defining my function
// here you have to give names to argument
// here you can give names to argument what ever you want
float average ( int num1 , int num2 , int num3 , int num4 )
// here value of A copy in num1
// here value of B copy in num2
// here value of C copy in num3
// here value of 45 copy in num4
{
float z;
z = num1 + num2 + num3 + num4 ;
z = z/4;
return z;
// we use return keyword to return a value back to main
//function
}