Function Definition
A function is a self-block contained statements, it perform some task as per requirement. Generally functions used for the (Whatever) statements repeatedly used by programmer, those statements put into the separate block of program. When ever need those statements we can call that block. We have already use some functions such as printf() , scanf() , strcat() and so on.
In C the functions can be divided into two types.
- Library Functions.
- User-defined functions.
The main Difference between these two categories library functions are not written by the programmer, those are already developed tested and ready to use. Whereas user-defined functions are written by the programmer at the time of writing a program.
The general syntax of user defined function definition is as follows.
Syntax:
return_type function_name([argument_list])
{
statement1;
statement2;
return value;
}
Return_type:
This is nothing but a datatype. Either int, char, float, long and void. When function gives a value to calling function, that value is either int,char, float and long. Based on that put the return type. For example strlen() function, it gives the value int type . x=strlen() . In such cases programmer use return type as int. If a function cannot return any value in such cases we can use “void”.
Function_name:
A function name is a user defined, but do not use a predefined keywords. Generally function names are related to it task performance, it helps to remember and find out easily. For example user wants to addition of number. In such case “add()” is appropriate as a function name. Finally whatever rules followed by the variable name same rules also applied for function name.
Function Argument List:
This nothing but input values taken by the calling function those are called argument list or parameters. For example if want perform multiply two numbers user can enter two values, those values can put as argument list. Argument list is optional.
Syntax of function with argument list:
return_type function_name(datatype variable,datatype variable)
ex1:
(int a,int b)
ex2:
(char s,float d)
return:
This is one of the keyword. When function is given a value to the calling function, It should be either int, char, float and long. Based on that given the return statement. Function’s return value must be match with define return type. If the function’s return type is ‘void’, return statement will write without value.
syntax:
return value;
ex1:
c=a+b;
return c;
ex2:
return ‘W’;
Based on the function syntax the functions can be divided into three categories.
- A Function with no arguments and no return values.
- A function with arguments and no return values.
- A function with argument and return values.
Function with no arguments and no return values
These type of functions doesn’t take any arguments and doesn’t return any type of values. this functions mostly uses display messages.
Syntax:
void function( )
{
input;
process;
output;
}
ex:
void add()
{
int a,b,c;
a=10;
b=3;
c=a+b;
printf(“Addition = %d\n”,c);
}
How to call in main()
main()
{
add();
}
A function with arguments and no return values
These type of functions does take arguments and doesn’t return any type of values. this functions mostly uses to display alert messages.
Syntax:
void function( argument list)
{
process;
output;
}
ex:
void add(inta,int b)
{
int c;
c=a+b;
printf(“Addition =%d\n”,c);
}
How to call in main()
main()
{
add(10,5);
}
A function with argument and return values
Mostly we use this type of functions. These functions are take arguments then process the arguments based on the given commends then return the value.
Syntax:
Return_type function( argument list)
{
process;
return value;
}
ex:
int add(int a,int b)
{
int c;
c=a+b;
return c;
}
How to call in main()
main()
{
int r;
r=add(10,5);
printf(“Addition =%d\n”,r);
}