About C function with return type
In C programming, each function must have a defined return type, which means when a function will be called, it will return the specified type of value to the caller.
If a function does not return any value, its return type is void.
The data type before a function name specifies the return type of the function.
In function body, return
keyword is used to return the return type.
A function can be called multiple times by its name followed by parentheses '()' and semi-colon ';'.
Create and call a function with return type
A function will be created and called in the following example:
Example of creating a function
xxxxxxxxxx
#include <stdio.h>
// declaring a function
int addNumber(int a, int b); //It is good practice to declare a function
// definiing the function with int return type
int addNumber(int a, int b) {
return a + b; //return type of function
}
int main() {
// Function will be called here
// other code goes here
}
Explanation of code:
-
int addNumber(int a, int b);
: The line declares a function named 'addNumber'. 'int' before 'addNumber' is the return type of the function. 'int a' and 'int b' inside the parentheses '()' are two integer parameters of the function. -
int sumNumber(int a, int b)
: The line defines a function named 'addNumber' with two integer parameters 'int a' and 'int b'. The function returns integer type. -
return a + b;
: This returns the sum of two integer variables 'a' and 'b'. The return result will be of an integer type. Thereturn
keyword ensures the return type of the function. - The funtction will be called in the next example.
Example of calling the function
xxxxxxxxxx
#include <stdio.h>
// declaring a function
int addNumber(int a, int b); //It is good practice to declare a function
// definiing the function with int return type
int addNumber(int a, int b) {
return a + b;
}
int main() {
int sum = addNumber(9, 4); //function called
printf("%d", sum);
return 0;
}
Output of the above example
13
Explanation of code:
-
int sum = addNumber(9, 4);
: The code 'addNumber(9, 4)' calls the 'addNumber' function with two integer arguments 9 and 4. The returned result is assigned to the integer variable 'sum'. -
printf("%d", sum);
: The line prints the returned result of the function. It prints 13 which is the sum of 9 and 4. The '%d' is the format specifier in C program.
Create and call a function with void return type
The void return type function performs the specified tasks but does not return anything. It's return type is void. A function with void return type will be created and called in the following examples:
Example of creating a function with void return type
xxxxxxxxxx
#include <stdio.h>
// function with void return type
void greetingMessage(){
printf("Welcome to C world.");
}
int main() {
// Function will be called
// other code goes here
}
Explanation of code:
-
void greetingMessage()
: The line defines a function named 'greetingMessage'. 'void' before 'greetingMessage' is the return type of the function. 'void' is used if the function does not return any specific data type. -
printf("Welcome to C world.");
: The function will print the message "Welcome to C world".
Example of calling the function
xxxxxxxxxx
#include <stdio.h>
// function with void return type
void greetingMessage(){
printf("Welcome to C world.");
}
int main() {
greetingMessage(); //function called
return 0;
}
Output of the above example
Welcome to C world.
Explanation of code:
-
greetingMessage();
: The code calls the 'greetingMessage' function. The output of calling the function is "Welcome to C world.".