MMGameslogo  MMGames
TwitterSharebutton  FacebookSharebutton   
learn through sufferingC Language
learn through sufferingC Language

Return a number from a function.

Returning value function
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.

Source code
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:

Source code
value = sum(50, 100);

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.

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 output of this program is as follows:

Execution results
3825

"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.
Return value limits
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.


About This Site

Learning C language through suffering (Kushi C) is
This is the definitive introduction to the C language.
It systematically explains the basic functions of the C language.
The quality is equal to or higher than commercially available books.

Part 0: Program Overview
  1. What is a program?
Chapter 3: Displaying on the Screen
  1. String Display
  2. newline character
  3. Practice Problem 3
Chapter 4: Displaying and Calculating Numbers
  1. Display of numbers
  2. Basic calculations
  3. Numeric types
  4. Practice Problem 4
Chapter 6: Input from the Keyboard
  1. input function
  2. The fear of input
  3. Practice Problem 6
Chapter 9: Repeating a Fixed Number of Times
  1. Iterative sentence
  2. How Loops Work
  3. Practice Problem 9
Chapter 10: Repeating Without Knowing the Number of Times
  1. Unspecified loop
  2. Input validation
  3. Practice Problem 10
Chapter 13: Handling Multiple Variables at Once
  1. Handling multiple variables collectively.
  2. Arrays
  3. Practice Problem 13
Chapter 19: Dynamic Arrays
  1. Create arrays freely.
  2. Practice Problem 19