Function
Function is a block of code that performs a specific task. A function is a group of statements that performs a specific task and is relatively independent of the remaining code. Functions are used to organize programs into smaller and independent units.
Advantages of using function
- Reduction in code redundancy
- Enabling code reuse
- Better Readability
- Information hiding
- Improved debugging and testing
- Improved maintainability
Classification of Functions
- User-defined functions
- Library functions
User – defined Functions
- User defined functions are the functions that are defined by the user at the time of writing a program.
- The user develops the functionality by writing the body of the function.
- These functions are sometimes referred to as programmer-defined functions
Library functions
- The standard library functions are built-in functions in C programming to handle tasks such as mathematical computations, I/O processing, string handling etc.
- These functions are defined in the header file
- The printf( ) is a standard library function to send formatted output to the screen.
Three aspects of working with user defined function
- Function Prototype (Function declaration)
- Function definition
- Function call (Function use or Function invocation)
Function Prototype
All identifiers (except labels) need to be defined before they are used. All the functions need to be declared or defined before they are used (i.e., called).
The general form of a function declaration is:
The function declaration consists of function name along with the return type and parameter list. Function names are identifiers
The specification of return type is optional. If specified the return type cannot be array or function type
Syntactic rules for writing a parameter_type list and parameter_list,
Parameter_type
- Is a comma separated list of parameter types.
- The parameter type can be any type (Ex: char, int, float, int *, int**, void) except function type
- If only a parameter type list is mentioned, the function declaration is said to have Abstract Parameter Declaration
Parameter_list
- If the parameter names follow parameter types in a parameter-type list, it becomes a parameter list.
- If the function declaration consists of a parameter list, it is said to have complete parameter declaration.
Combined declaration of abstract and complete parameter declaration is also valid
Example
int add (int x, int);
int add(int, int y);
- No two parameter names appearing in the parameter list can be same
- The shorthand declaration of parameters in parameter list is not allowed.
- Function prototype is a statement, so it must be terminated with semicolon
- A function need not be declared, if it is declared before it is defined.
Example
add( );
int add (int, int);
int* add(int,float);
int add(int a, int b);
int add(int, int b);
Function Definition
Function definition also known as function implementation, means composing a function.
Every function definition consists of two parts:
- Header of the function
- Body of the function
Header of the function
The general form of header of the function is:
- The header of a function can have complete parameter declaration
- No two parameter names appearing in the parameter list can be same
- The shorthand declaration of parameters in the parameter list is not defined
- The return type and the number and types of parameters in the function header should exactly match the corresponding return type and the number and types of parameters in the function declaration
- Not necessary to have same names for the parameters in the function declaration and function definition
- The header of a function is not terminated with a semicolon.
Body of the function
- The body of a function consists of a set of statements enclosed within braces
- The body of a function can have non-executable statements and executable statements
- The non executable statements can come before the executable statements
- The non executable statements declare the local variables in the function and the executable statements determine its functionality i.e. what the function does.
- A function can optionally have special executable statement known as return statement.
- The return statement is used to return the result of the computations done in the called function and / or to return the program control back to the calling function
[return_type] function_name([parameter_list])
{
//Body of Function
}
Function call / invocation / use
When a program calls a function, the program control is transferred to the called function. A called function performs task and when its return statement is executed or when its function – ending closing brace is reached, it returns the program control back to the main program.
General form
Function_name( );
Function_name(parameters);
Example
add( );
add(a,b);
Example: to add two numbers using function
#include<stdio.h>
void add();
int main()
{
add(); // function call
return 0;
}
void add()
{
int a,b,c;
printf("Enter two numbers\n");
scanf("%d%d",&a,&b);
c = a +b;
printf("\nResult = %d",c);
}
Output
Enter two numbers
3
2
Result = 5
Based on the arguments and return type, functions are classified as:
Function without arguments and without return type
In this type, no argument is passed through the function call and no output is returned to main function
#include<stdio.h>
void add();
int main()
{
add(); // function call
return 0;
}
void add()
{
int a,b,c;
printf("Enter two numbers\n");
scanf("%d%d",&a,&b);
c = a +b;
printf("\nResult = %d",c);
}
Output
Enter two numbers
3
2
Result = 5
Function with arguments and without return type
Arguments are passed through the function call but output is not returned to the main function
#include<stdio.h>
void add(int a, int b); // Function prototype
int main()
{
int a, b;
printf("Enter two numbers :");
scanf("%d%d",&a,&b);
add(a,b);
return 0;
}
// function definition
void add(int c, int d)
{
int sum;
sum = c+d;
printf("\nResult = %d", sum);
}
Output
Enter two numbers
3
2
Result = 5
Function without arguments and with return type
In this type no argument is passed through the function call but output is returned to the main function
#include<stdio.h>
float area_cir();
int main()
{
float c;
c=area_cir();
printf("Area =%f",c);
}
float area_cir()
{
int rad;
float area;
printf("Enter radius:");
scanf("%d",&rad);
area = 3.14 * rad * rad;
return area;
}
Output
Enter radius:3
Area =28.260000
Function with arguments and with return type
In this type arguments are passed through the function call and output is returned
#include<stdio.h>
float area_tri(int b, int h);
int main()
{
int base, height;
float c;
printf("Enter base and height:");
scanf("%d%d",&base,&height);
c=area_tri(base, height);
printf("\nArea = %f",c);
return 0;
}
float area_tri(int a, int b)
{
float d;
d = 0.5 * a * b;
return d;
}
Output
Enter base and height: 2
3
Area = 3.000000
Views: 0