| |
FUNCTIONS in 'C' language
C supports modular programming. You can divide a large program into different parts called modules, so that it becomes easy for debugging and corrections. These modules are known as functions. A function may be defined as a self contained block of statements that perform a coherent task of some kind.
It is difficult to implement a large program even it's algorithm is available. To implement such a large program, it should be split into number of independent tasks, which can be easily designed, implemented and managed. This process of splitting a large program into small manageable tasks and designing them independently it popularly called divide and conquer technique.
A repeated group of instructions in a program ca be organized as a function. A function is a set of program statements that can be processed independently. A function can be invoked which behaves as though it's code is inserted at the point of the function call. The communication between calling function and called function takes place through parameters. The function can be designed, developed and implemented independently by different programs. The implemented function can be formed as a software library.
The following are the advantages of functions.
1) Modular programming.
2) Reduction in the amount of work.
3) Program and function debugging easier.
4) Division of work simplified due to the use.
5) Reduction in size of the program due to the code reusability.
6) Functions can be accessed repeatedly without redevelopment which in turn promote reuse of code.
7) Library of functions can be implemented by combining well designed, tested and proven functions.
Functions can be categorized in following 2 ways.
1) Built-in or System defined functions
2) User defined functions
(1) Built-in functions:- These are the default functions which are prewritten and preoccupied. All system-defined functions are present in C library. C is divided into different groups. Each group contains certain common functions and each group is called a header file with a .h extension. Eg: stdlib.h, conio.h, math.h etc., These header file functions can be used anywhere in the function and as many times as we want.
(2) User defined functions:-main() is a user defined function. A program should not have any compilation errors to debug the program. The functions that are defined by the user for his works are known as user defined functions.
Functions Components
Every function has the following elements associated with it.
1 Function declaration or function prototype
2 Function definition (function declarator and function body)
3 Function call
4 Function parameters
5 Return statement
Function declaration or Function prototype:- Functions can be defined any where in the program. They may be defined before the main() or after the main(). The function prototype provides the following information to the compiler.
1) The name of the function
2) Type of value returned (optional, by default it is integer)
3) The number and type of arguments that must be supplied in a call to the function.
When a function call is encountered, the compiler checks the function call with its prototype so that correct argument types are used. The following is the syntax of function prototype.
Syntax:-
return datatype function name(argument list)
{
local variable declarations
executable statements
return statement
}
Return data type is the type of data the function is returning back to the calling function from the called function. By default any function returns integer type of data. If there is no return value the keyword 'void' is placed before the function.
Argument list is the number and type of values that should be passed to the function, so that the function makes use of them for performing a particular task. These are also known as parameters. The function declaration should end with a semi colon.
Function Definition:- The function itself is referred to as a function definition.
Return datatype function name(datatype variable, datatype var2, ?)
{
local declarations
executable statements
}
The first line of the function definition is known as function declarator and is followed by function body. The declarator and the declaration should use the same function name, no. of arguments, type of arguments and the return type. No other function definitions are allowed in a function definition.
Function call:- A function is a dormant(in active) entity which gets life only when a call to the function is made. A function call is specified by the function name followed by the arguments enclosed in parentheses and terminated by a semicolon. The return type is not mentioned in the function call. Executing the call statement causes the control to be transferred to the first statement in the function body and after execution of the function body, the control is returned to the statement following the function call. The return value is assigned to the local variable in C program.
Function Parameters:-The parameters specified in the function call are known as actual parameters and those specified in the function declarator are known as formal parameters. When a function call is made a one-to-one correspondence is established between the actual and formal parameters. The scope of formal parameters is limited to its function only.
Function return:-Function can be grouped into two categories namely function that do not have a return value(void function) and the function that have a return value. The calling function must be able to receive the value return by the function. The return statement in a function need not be at the end of the function. It can occur anywhere in the function body and as soon as it is encountered, execution control will be returned to the calling program. Return statement returns a value from the called function to the calling function. The value may be directly a number, value in a variable or an expression.
Eg; return 10;
return b;
return (a+b);
There are 3 types of user defined functions available in C. They are as follows.
a) without parameters and without return value
b) with parameters and without return value
c) with parameters and with return value
a) Without parameters and without return value:- In this type of function, we don't pass any values to the function and also do not return any value to the main program. The result is displayed in the function itself.
Eg: main(){
add();
add();
add();
getch();
}
add()
{
int a,b;
printf("\nEnter 2 values");
scanf("%d%d",&a,&b);
printf("\nSum is %d",a+b);
}
b) With parameters and without return value:- In this type, we pass parameters to the function but we will not take the return value.
Eg:
main(){
int a,b;
clrscr();
printf("\nEnter 2 values :");
scanf("%d%d",&a,&b);
add(a,b);
getch(); }
add(int p,int q)
{
int t;
t=p+q;
printf("\nSum is %d",t); }
c) With parameters and with return value:- We pass parameters as well as return a value to the calling program.
Eg:- main(){
float p,t,r,si, simple(float,float,float);
clrscr();
printf("\nEnter amount, interest and time");
scanf("%f%f%f"{,&p,&r,&t);
si=simple(p,t,r);
printf("\nSimple interest is %0.2f",si);
getch();
}
float simple(float a, flaot b, float c)
{
float s;
s=(a*b*c)/100;
return s; }
Nested functions:- Calling a function inside another function is known as nested function.
Eg: main(){
int a,b,c,avg;
clrscr();
printf("\nEnter 3 numbers:");
scanf("%d%d%d",&a,&b,&c);
avg=average(a,b,c);
printf("\nThe average of 3 numbers is %d",avg);
getch();
}
average(int p, int q, int r)
{
int tot, av;
tot=total(p,q,r);
avg=tot/3; return av;
}
total(int x, int y, int z){
int sum;
sum=(x+y+z)/3;
return sum;
}
Please send comments to vgdarur.javafive@blogger.com
|