Syntax Of Function:
Syntax of Function in ‘C’ Classified mainly in three parts:
- Function Declaration
- Function Definition
- Function Call
1. Function Declaration:
It specifies return data type of functions, name of function and data type of arguments, if any. Function Declaration is necessary if Function is called First and Defined Later. Number of Arguments/Parameters and Corresponding Data Type must match when calling function and when writing Function Declarator. Function Declaration can be Dropped if definition of function appears before Function Call. Function Declaration can be Global or Local.
Syntax:
return_type function_name(parameter_type parameter_name, …);
- return_type: Specifies the data type of the value the function returns. It can be int, float, void, etc.
- function_name: The name of the function, which is used to call it in the program.
- parameter_type: Specifies the data type of the input parameters the function accepts.
- parameter_name: The name of the parameter, which is used within the function to refer to the passed values.
Example:
// Function declaration
int multiply(int a, int b);
2. Function Definition:
Function Definition contains Function Declarator (or function header line) and Function Body. Function Body includes all those statements which will complete the task for which Function is made.
Syntax:
return_type function_name(parameter_type parameter_name, …) {
// Function body
// Statements and calculations
return value; // Optional return statement
}
- return_type: Same as in the declaration, specifies the data type of the value returned by the function.
- function_name: Same as in the declaration, the name of the function.
- parameter_type: Same as in the declaration, specifies the data type of the input parameters.
- parameter_name: Same as in the declaration, names of the parameters.
Example:
// Function definition
int multiply(int a, int b) {
int result = a * b;
return result;
}
3. Function Call:
A function call is a statement that invokes (or “calls”) a specific function to execute its defined set of instructions. The function call is where the actual execution of the function’s code takes place. It allows you to use the functionality encapsulated within a function at a specific point in your program.
Syntax:
return_type result_variable = function_name(argument1, argument2, …);
- return_type: The data type of the value returned by the function.
- result_variable: The variable where the returned value will be stored.
- function_name: The name of the function to be called.
- argument1, argument2, …: The actual values passed to the function as arguments.
Example:
// Function call
int output = multiply(3, 5);
4. Recursive Function:
A function call is a statement that invokes (or “calls”) a specific function to execute its defined set of instructions. The function call is where the actual execution of the function’s code takes place. It allows you to use the functionality encapsulated within a function at a specific point in your program.
Syntax:
return_type function_name(parameters) {
// Base case (termination condition)
if (base_case_condition) {
// Return base case value
return base_case_value;
}
// Recursive case
else {
// Recursive call to the function
return recursive_function_call(arguments);
}
}
- return_type: The data type of the value that the function returns. It can be int, float, void, etc.
- function_name: The name of the recursive function.
- parameters: Input parameters that the function takes. These are used in both the base case and recursive case.
- base_case_condition: A condition that, when true, indicates that the recursion should stop. This is the termination condition that prevents an infinite loop.
- base_case_value: The value returned when the base case condition is true. It is the result of the simplest case, which doesn’t involve a recursive call.
- recursive_function_call: A call to the same function within its own definition.
- arguments: The actual values passed to the function during the recursive call. These values should typically be modified to make progress towards the base case.
Example:
// Recursive function to calculate factorial
int factorial(int n) {
if (n == 0 || n == 1) {
return 1;
} else {
return n * factorial(n – 1);
}
}
5. Function with No Return Value (void function):
Example:
void printMessage() {
printf(“Hello, World!n”);
}
6. Function with Multiple Parameters:
Example:
float calculateAverage(int num1, int num2, int num3) {
return (num1 + num2 + num3) / 3.0;
}