In the previous section, we explained how to pass numbers to a function.
This time, we'll explain how to return a number from a function.
In fact, the sum function is already designed to return a number.
For now, please take a look at the sum function from the previous section.
int sum(int min, int max)
{
printf("%d\n", (min + max) * (max - min + 1) / 2);
return 0;
}
There are two key points to note.
One is the string "int" that appears before the function name.
"I've just mimicked how it was done in the main function, though."
Actually, this indicates the data type of the value returned from a function.
Another key point is the return statement.
So far, I've habitually added it to the end of functions without really thinking about it.
Actually, this return statement has the functionality of returning a number.
That meant the sum function was supposed to return the integer value 0.
Sometimes, the number returned from a function is referred to as a "[Return Value (Function Value)]".
【return value】
functionから返されるnumerics。
計算結果か、orErrorの有無を示すnumericsであることが多い。
If you need to access the return value in the calling context, you must assign it to a variable.
To assign the return value of the sum function to a variable named value, do the following:
Using this return value, you can do more than just display the calculation result.
You can tell the caller as a number.
It can be displayed or used in calculations by the caller.
The following program is an example of modifying the sum function to return a calculation result.
#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 output of this program is as follows:
"The result remains the same as before, but this time the sum function only returns a calculation result."
The results are displayed using the printf function within the main function.
If necessary, the return value from the main function can also be used in another calculation.
In the preceding section, it was possible to use multiple arguments.
Well, naturally, I'd like to say the return value as well... but that's not possible.
Unfortunately, it can only return a single value.
Given the circumstances, we have no choice but to settle for a single return value.
Actually, there's also a technique to return a number as an argument, but I'll leave that for later.
"In the case of arguments, they were always required to be specified when calling."
You can ignore the return value.
Actually, the printf function also returns information about how many characters were printed.
To be honest, I've been ignoring it because it's irrelevant information.
"You can also use your preferred type, such as double, for the return value in addition to int."
You can also create functions that don't return a value.
In that case, you specify "void" before the function name.
It's surprisingly common for very simple functions to not need a return value.