About C Function
In C programming, a function is a block of reusable code that performs a specific task when it is called.
A function can be called multiple times by its name followed by parentheses '()' and semi-colon ';'.
A function may be created with parameters or without any parameter.
A function may or may not have a return type.
Syntax of C function
Before defining a function, it needs to be declared (if the function is defined after 'main' function). It is a good practice to declare a function before defining it. The function declaration tells the compiler about the function name, return type, and parameters.
1. Function Declaration Syntax
xxxxxxxxxx
return_type function_name(data_type parameter);
-
return_type
: The 'return_type' refers to return type of the function. It may be any data type or void(for exampleint
). -
function_name();
: The 'function_name' will be replaced by a meaningful name for the function. The function name will be followed by a parentheses '()' (for examplesumNumber()
). -
(data_type parameter);
: Inside the parentheses '()', parameter with data type is placed (for example(int a, int b)
). Parameters are seperated by comma (,).
Complete example of function declaration: int sumNumber(int a, int b);
A function must be defined following the syntax of function definition, if the function is defined earlier. (Otherwise, a function can be defined as per user requirement.)
2. Function Definition Syntax
xxxxxxxxxx
return_type function_name(data_type parameter) {
// function body code
return value; // if the return type is not void
}
-
return_type function_name
: The 'return_type' refers to return type of the function. It may be any data type or void. The 'function_name' will be replaced by a meaningful name for the function. The function name will be followed by a parentheses '()'. For example:int sumNumber()
-
data_type parameter
: Inside the parentheses '()', parameters are placed. Data type of parameter must be defined before the name of the parameter. For example:(int a, int b)
-
// function body code
: Here goes the code block of the function where function task will be specified. -
return value;
: It returns the value of the return type data as specified in the function definition. If the function return type is void, this code is not necessary in the function. For example:return a + b;
To use a function, it needs to be called. A function can be called from the main function or other functions.
3. Function Calling Syntax
xxxxxxxxxx
function_name(parameter_argument);
-
function_name(parameter_argument);
: The code calls the 'function_name' function with an argument in parentheses '()' named 'parameter_argument'. For example:sumNumber(4, 5);
Create and Call a Function
A function named 'sumNumber' will be created and called in the following example:
Example of creating and calling a function
xxxxxxxxxx
#include <stdio.h>
// Function declaration
int sumNumber(int a, int b);
int main() {
// Function call
int result = sumNumber(5, 9);
printf("The result is: %d\n", result);
return 0;
}
// Function definition
int sumNumber(int a, int b) {
return a + b;
}
Output of the above example
The result is: 14
Explanation of code:
-
int sumNumber(int a, int b);
: The line declares a function named 'sumNumber' having two integer parameters (a and b) with integer return type. -
int sumNumber(int a, int b){return a + b;}
: The line defines a function named 'sumNumber' with two integer parameters 'int a' and 'int b'.return a + b;
returns the sum of two integer variables 'a' and 'b'. The function returns integer type. -
int result = sumNumber(5, 9);
: The line of code calls the 'sumNumber' function with two integer arguments 5 and 9 in the main function. The result is assigned to the integer variable 'result'. -
printf("The result is: %d\n", result);
: The line prints the result of the function. The '%d' is the format specifier in C program.
Create and call a function without declaring the function
If you define a function before the 'main' function, you need not to declare the function first. A function will be created and called in the following example:
Example of creating and calling a function without declaring the function
xxxxxxxxxx
#include <stdio.h>
// Function definition
int sumNumber(int a, int b) {
return a + b;
}
int main() {
// Function call
int result = sumNumber(5, 9);
printf("The result is: %d\n", result);
return 0;
}
Output of the above example
The result is: 14
Explanation of code:
-
int sumNumber(int a, int b){return a + b;}
: The line defines a function named 'sumNumber' with two integer parameters 'int a' and 'int b'. The function returns integer type. The function is defined before the 'main' function. -
return a + b;
: This is the function body that returns the sum of two integer variables 'a' and 'b'. -
int result = sumNumber(5, 9);
: In the 'main' function, the code calls the 'sumNumber' function with two integer arguments 5 and 9. The result is assigned to the integer variable 'result'. -
printf("The result is: %d\n", result);
: The line prints the result of the function. The '%d' is the format specifier in C program.