A user-defined function consists of three components:
Function Declaration: This component specifies the function's interface, including its name, return type, and parameter types (if any). The declaration tells the compiler about the function's existence and how it can be used. It usually appears before the function is called or defined. While it's optional in some cases (especially when the function definition precedes its usage), providing a declaration is good practice for clarity and maintainability.
Function Definition: This component contains the implementation of the function, including the actual code that performs the desired task. It specifies what the function does when called with particular arguments. The function definition includes the function header (name, return type, parameters) followed by the function body, which contains the statements to execute.
Function Call: This component involves invoking the function to execute its defined task. It includes the function name followed by parentheses containing the arguments (if any) required by the function. The function call passes control to the function's definition, executes its code, and may return a value back to the calling code.
Example:
#include <stdio.h>
// Function declaration
int add(int , int );
int main() {
int result;
// Function call
result = add(3, 5);
printf("The sum is: %d\n", result);
return 0;
}
// Function definition
int add(int a, int b) { return a + b; }
In the above example:
1. Function Definition
A function definition in C consists of the function's header, followed by its body.
Syntax of Function Definition:
return_type function_name(parameter_list) {
// Function body
// Statements to execute
return value; // (optional) Return statement
}
Explanation of Each Part :
return_type: This specifies the type of data that the function will return after its execution. If the function doesn't return any value, its return type should be void.
function_name: This is the identifier used to call the function. Choose a descriptive name that reflects the purpose of the function.
parameter_list: This is a list of parameters (arguments) that the function expects to receive when it is called. Parameters are optional, and you can have zero or more of them. Each parameter consists of a data type followed by a parameter name. Multiple parameters are separated by commas.
Function body: This contains the set of statements that define what the function does when it is called. It is enclosed within curly braces {}.
Return statement: If the function has a return type other than void, it must contain a return statement that specifies the value to be returned to the calling code. This statement is optional for void functions.
Example of Function Definition:
#include <stdio.h>
// Function definition
int add(int a, int b) {
int sum = a + b;
return sum;
}
int main() {
int result;
// Function call
result = add(3, 5);
printf("The sum is: %d\n", result);
return 0;
}
The function add takes two integer parameters a and b and returns their sum.
Inside the function body, sum is calculated as the sum of a and b.
The calculated sum is returned using the return statement.
In the main function, add is called with arguments 3 and 5, and the returned value is stored in the variable result.
Finally, the value of the result is printed to the console.
2. Function Prototype (Declaration)
A function declaration is similar to a function prototype, but it's typically used to inform the compiler about the existence and signature of a function without providing its full definition. This allows you to use the function before it's actually defined in the source code.
The syntax for a function declaration is:
return_type function_name(parameter_list);
Example :
#include <stdio.h>
// Function declaration
int add(int a, int b);
int main() {
int result;
// Function call
result = add(3, 5);
printf("The sum is: %d\n", result);
return 0;
}
// Function definition
int add(int a, int b) {
int sum = a + b;
return sum;
}
3. Function Call
Syntax of Function Call:
return_type variable_name = function_name(argument_list);
Example :
#include <stdio.h>
// Function declaration
int add(int a, int b);
int main() {
int result;
// Function call
result = add(3, 5);
printf("The sum is: %d\n", result);
return 0;
}
// Function definition
int add(int a, int b) {
int sum = a + b;
return sum;
}
main
function, the add
function is called with arguments 3
and 5
.3
and 5
) is stored in the variable result
.result
is printed to the console using printf
.4. Function Parameters
Function parameters (also known as function arguments) are the values passed to a function when it's called. They allow the function to operate on data provided by the caller.
Syntax:
return_type function_name(parameter1_type parameter1_name, parameter2_type parameter2_name, ...);