﻿ Returning a number from a function - Painfully Learning C
C language learned by suffering

### Returns a number from a function

##### Function returning a return value
In the previous section, we explained how to pass numbers to a function.
In this article, we will explain how to do the opposite, returning a numerical value from a function.

In fact, the sum function is already designed to return a numeric value.
For now, look at the sum function in the previous section.

source code
`````` int sum(int min, int max)
{
printf("%d\n", (min + max) * (max - min + 1) / 2);
return 0;
}``````

There are two points of interest.
The first is the string "int" that precedes the function name.
This is just an imitation of what was done in the main function.
In fact, it indicates the type of number returned from the function.

Another key point is the return statement.
Until now, we have always added a function to the end of a function without thinking about it, but now we can use the
In fact, the return statement has the function of returning a numerical value.

In other words, the sum function was supposed to return a numeric value of type int, 0.
Such a numeric value returned from a function is sometimes called a return value (function value ).

Keywords.
##### Return value

The number returned from the function.
Often the result of a calculation or a number indicating the presence or absence of an error.

If the caller wants to know the return value, it must be assigned to a variable.
To assign the return value of the sum function to the variable value, do the following

source code
`` value = sum(50, 100);``

With this return value, you can not only display the result of the calculation, but also
It can be taught to the caller as a numerical value.
The caller can then display it or use it in a calculation.
The following program is an example of modifying the sum function to return the result of a calculation.

Source code
`````` #include <stdio.h>

int sum(int, int); /* prototype declaration */

int main(void)
{
int value;
value = sum(50, 100);
printf("%d\n", value);
return 0;
}

int sum(int min, int max)
{
int num;
num = (min + max) * (max - min + 1) / 2;
return num;
}``````

The result of executing this program is as follows

Execution Result
3825

The result is the same as before, but this time the sum function only returns the result of the calculation.
The results are displayed in the main function using the printf function.
If necessary, the return value can be used in the main function for another calculation.
##### Return Value Limitations
In the arguments described in the previous section, it was possible to use multiple arguments.
Then, of course, we would like to say that the return value is also... but that is not the case.
To our surprise, only one return value can be returned.

Since this is the case, we have no choice but to give up on a single return value.
Actually, there is a trick to return a numerical value as an argument, but this will be discussed later.

argument, it was always necessary to specify it when calling
The return value may be ignored.
As a matter of fact, the printf function also returns information about how many characters were printed, but the
To be clear, such information is unimportant, so we have ignored it.

In addition to int, you can use any type you like for the return value, such as double.
It is also possible to create functions that do not return values.
In this case, specify void before the function name.
Surprisingly, very simple functions often do not require a return value.

Comment
COMMENT

Open the 💬 comment submission box