MMGames Introduction to C C Language Development Environment C language now Useful Apps Contact Us
MMGames

Automatic version identification

SpineViewer

It's easy to tell by looking at it.

Response Time Checker

I can leave my computer on and do it.

Mouse cleaning time

I can leave my computer on and do it.

Mouse cleaning time

C language learned by suffering
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.


About this Site

The C language (bitter C), which is learned by suffering, is
This is the definitive C language introductory site.
It systematically explains the basic functions of the C language and
It is as complete as or better than any book on the market.

Part 0: Program Overview
  1. What is the program?
Chapter 2: How to write a program
  1. Writing Rules
  2. Writing conventions
  3. Exercise 2
Chapter 3: Display on Screen
  1. String display
  2. newline character
  3. Exercise 3
Chapter 4: Numeric Display and Calculation
  1. Numeric Display
  2. Basic Calculations
  3. Type of value
  4. Exercise 4
Chapter 5: Numerical Memory and Calculation
  1. Memorize values
  2. Variable Type
  3. Type conversion
  4. Numeric justification
  5. Exercise 5
Chapter 6: Input from the keyboard
  1. Functions for input
  2. Fear of Input
  3. Exercise 6
Chapter 9: Repetition with a fixed number of times
  1. Sentences that repeat themselves
  2. Loop Operation Mechanism
  3. Exercise 9
Chapter 10: Unknown number of repetitions
  1. Loop of unknown frequency
  2. input check
  3. Exercise 10
Chapter 13: Handling Multiple Variables at Once
  1. Multiple variables are handled together.
  2. How to use arrays
  3. Exercise 13
Chapter 19: Dynamic Arrays
  1. Create arrays at will
  2. Exercise 19
Chapter 20: Multiple Source Files
  1. Minimal division
  2. The Stone of Partition
  3. Exercise 20

Comment
COMMENT

Open the 💬 comment submission box