A function is a group of statements that helps in performing the task. If you have noticed in previous programs there is at least one function in every C program i.e. main().
To make your code more efficient you can divide up your code into different functions. A function declaration informs the compiler about the function's name, return type, and parameters. The function definition contains the actual body of the function.
The general syntax of function definition in C language is as follows-
return_type function_name( parameter list ) { body of the function } |
Let's know in brief about this syntax.
Function Name- It contains the name of the function and the parameter list together.
Return Type- We have used the return type in the above syntax. It is just a data type that returns the value of function returns. Some functions do not have to return a value.
Parameters- Parameters are simply like a placeholder. This value is referred to as the actual parameter.
Function Body- In the body of a function there is a collection of statements that define what the function does.
Program:
#include <stdio.h>
/* Declare a function */ int max(int num1, int num2);
int main () {
/* To define a local variable */ int a = 200; int b = 320; int ret;
/* TO Call a function and get maximum value */ ret = max(a, b);
printf( "The Maximum value is : %d\n", ret );
return 0; }
/* The function returning the maximum between two numbers */ int max(int num1, int num2) {
/* To Declare a local variable */ int result;
if (num1 > num2) result = num1; else result = num2;
return result; } |
Output:
The Maximum value is : 320