Function Definition in C
“A function in the C programming language is an independent unit of code that carries out a particular purpose. A fundamental idea in programming, functions aid in organising code and make it easier to comprehend and maintain.”
The function header, function body, and any other function declarations are all components of a function definition in C. Here is a thorough breakdown of every component of a C function definition.
The function header, function body, and any optional function declarations are all components of a function definition in C.
return_type-function_name(datatype-parameter1,-datatype-parameter2,-...)- { ---------------------------------------//-Function-body ---return-return_value; }
Function Header: The first element of a function definition is the function header. Return type, function name, and function parameters make up its three components.
Return Type: The data type of the value that the function returns is known as the return type. Functions can return values of several data kinds in the C programming language, including integers, floating-point numbers, characters, arrays, and pointers. A function's return type is void if it makes no output at all.
Function Name: The function name serves as a special identifier that other programme elements use to invoke the function. It ought to have a name that accurately describes the function's function.
Function Parameters: Every argument has a name and a data type. The parameters are separated by commas and enclosed in brackets (). The brackets are empty in the absence of any parameters for a function.
Function Body: The section of the function specification containing the instructions for carrying out the function's task is known as the function body. It contains one or more statements and is enclosed in curly braces. Assignments, loops, conditional statements, and other function calls are examples of statements.
Return Statement: A value from a function is returned to the calling programme using the return statement. It starts with the word return and then has an expression. The return type supplied in the function header must match the data type of the expression. The return statement is omitted or the return keyword is followed by nothing if the function returns nothing.
Example:
#include <stdio.h> // Function definition int add(int n1, int n2) { function type ,function name and parameters int sum = n1 + n2; //function body return sum; //return statement } int main() { int a = 20, b = 30, c; // Call the function and store the result in variable c c = add(a, b); printf("sum of %d and %d is %d\n", a, b, c); return 0; }
Output:
Add is a function that we define that has two integer inputs called n1 and n2. The return statement is used to return the result once the function has calculated the sum of n1 and n2.
We declare the three integer variables a, b, and c in the main() method. The result is then saved in the variable c after the add function is called with the arguments a and b. Finally, we use the printf function to output the sum of a and b.
The return value type, method name, and argument list are all specified in the function header. The code that carries out the function's intended function is contained in the function body. The calling programme receives a value from the function via the return statement
The add function was successfully invoked and gave back the accurate total of a and b.
Example :
#include <stdio.h> // Function definition int fact(int n) { if (n == 0 || n == 1) { return 1; } else { return n * fact(n - 1); } } int main() { int n = 5, res; // Call the function and store the result in variable result res = fact(n); printf("The factorial of %d is %d\n", n, res); return 0; }
Output:
We create a fact function that has a single integer parameter called n. Recursion is used by the function to calculate the fact of n, and the return statement is used to return the res.
We declare two integer variables, n and res, in the main() method. The fact function is then used with n as the argument, and the output is saved in the res variable.
Example:
#include <stdio.h> // function declaration int max(int num1, int num2); int main() { int x = 90; int y = 50; int z = max(x, y); printf("The maximum number is %d\n", x, y, z); return 0; } // function definition int max(int num1, int num2) { if (num1 > num2) { return num1; } else { return num2; } }
The max function, which in this program takes two integer parameters and returns the larger of the two integers, is defined. A function declaration, which informs the compiler of the function's return type and parameter types, is used to define the function before the main function.
Output:
In the two integer variables x and y are declared and given the values 90 and 50, respectively, in the main function. The max function is then invoked with the variables x and y, and the output is saved in the variable z. We use printf to output the function call's outcome last.
With the help of functions, this programme shows how to modularize code and make it simpler to read and maintain. We can reuse this code in other parts of our programme without having to copy it by creating a new function that calculates the maximum of two values.