Menu
Coddy logo textTech

Return Value

Lesson 4 of 17 in Coddy's Functions in C course.

A function can return a result to be used outside the function. To return a result, use the return keyword like this:

return;
return expression;

The first form (return;) is used for functions that do not return any value (i.e., they have a return type of void).

The second form (return expression;) is used for functions that return a value. The expression is the value to be returned.

Let's look at an example:

void sum(int a)
{
  printf("%d + %d = %d \n", a, 2, a + 2);
}

This function doesn't explicitly use return, but the compiler adds it automatically at the end.

void plus(int a)
{
  printf("%d + %d = %d \n", a, 2, a + 2);
  return;
}

A function can also return a specific value. For example:

int plus(int a, int b)
{
   return a + b;
}

Now, the plus function returns an int. When a function's return type is not <mark class="pen-red">void</mark>, it must return a value of that type. To return a value, use return and specify the value.

If a function returns a value, you can store the result in a variable like this:

int result = plus(3, 4);
challenge icon

Challenge

Easy

Write a program to print the multiplication of two numbers:

  • Define a function called <strong>multiply</strong> that takes two parameters, <strong>x</strong> and <strong>y</strong>, of type <strong>int</strong> and returns the result of the multiplication of <strong>x</strong> and <strong>y</strong>.
  • In the main function, define a variable z that will store the result of passing the numbers 9 and 10 to the multiply function. 
  • Print the value of z to the screen.

Try it yourself

#include <stdio.h>

// Define the function 


int main(void)
{
    // Define the variable z 
 
    
    // Print the value of z 
    
    
    return 0;
}

All lessons in Functions in C