In a C programming language a function is a group of statements that together perform a special task. In every C program has at least one function, which is main().
In a C library has a numerous predefined or built-in functions. For example, strcat() to concatenate two strings, main() from where program execution starts.
In a C programming function has a 3 part:
- Function declaration
- Function definition
- Function call
Function declaration : function declaration tells the compiler about a function’s name, return type, and parameters.
A syntax of function declaration –
return_type function_name( parameter list );
Example of the function declaration is as follows −
int max(int num1, int num2);
here int is a return type of function which tells function will return integer value and max is a name of the function.
“ int num1, int num2” is a parameter list which tells max function will receive two integer arguments.
Arguments are optional which depends on program requirements.
Function definition : function definition is an actual body of the function.
Syntax of function definition:
return_type function_name( parameter list )
{
body of the function
}
function definition consist a return type, function name, parameter list and body of the function.
Function call
whenever program calls a function, the flow of control is transferred to the called function definition.
A called function performs a defined task and when flow of control reached to function-ending closing brace, flow of control returns to back to the main program.
Function can be categories as:
- Function with no argument, no return value
- Function with argument but no return value
- Function with argument and return value
- Function with no argument, but return value
Function with no argument & no return value
Example: Write a program to find the area of circle using function with no argument, no return value.
The formula for the area of the circle is :
Area_circle = Π * r * r
Program
#includevoid areaOfcircle(); //Function declaration int main() { areaOfcircle(); // Function Call return 0; } void areaOfcircle() // function definition to calculate area of circle { float area, radius; printf("Enter the radius of circle : "); scanf("%f", & radius); // take radius as input area = 3.14 * radius * radius; printf("\n Area of circle= : %.2f", area); }
Output
Enter the radius of circle : Area of circle= : 78.50
Function with argument but no return value
Example: Write a program to find the area of circle using function with argument but no return value.
#includevoid areaOfcircle(float r); //Function declaration with argument int main() { float radius; printf("Enter the radius of circle : "); scanf("%f", & radius); // take radius as input areaOfcircle(radius); // Function Call return 0; } void areaOfcircle(float r) // function definition to calculate area of circle { float area; area = 3.14 * r * r; printf("\n Area of circle : %.2f", area); }
Output
Enter the radius of circle : Area of circle= : 78.50
Function with argument & return value
Example: Write a program to find the area of circle using function with argument but return value.
#includefloat areaOfcircle(float r); //Function declaration with argument int main() { float radius, a; printf("Enter the radius of circle : "); scanf("%f", & radius); // take radius as input a = areaOfcircle(radius); // Function Call printf("\n Area of circle : %.2f", a); return 0; } float areaOfcircle(float r) // function definition to calculate area of circle { float area; area = 3.14 * r * r; return area; }
Output
Enter the radius of circle : Area of circle= : 78.50
Function with no argument but return value
Example: Write a program to find the area of circle using function with no argument but return value.
#includefloat areaOfcircle(); //Function declaration int main() { float a; a = areaOfcircle(); // Function Call printf("\n Area of circle : %.2f", a); return 0; } float areaOfcircle() { // function definition to calculate area of circle float area, radius; printf("Enter the radius of circle : "); scanf("%f", & radius); // take radius as input area = 3.14 * radius * radius; return area; // return value }
Output
Enter the radius of circle : Area of circle= : 78.50