How to create and call a function with return type in C

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

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. The return keyword ensures the return type of the function.
  • The funtction will be called in the next example.
Example of calling the function
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

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
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.".

Live Code Playground

In the following C code editor, you can practice by creating more C functions with different return types. Click the 'Execute Code' button to execute the code; the executed output will be displayed in the following C Code Output frame. Practice until you become comfortable and proficient with your code.

C logo C Online Editor
C Code Output