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

Passing numbers to functions

Functions with arguments
In the previous section, we created the sum function and componentized the calculation of the sum from 1 to 100.
However, it is inconvenient to be able to calculate only totals from 1 to 100.
It would be more convenient to be able to calculate a more flexible range of totals.

For such occasions, information (numerical values) can be passed to the function.
The mechanism for this is called arguments.
Arguments allow you to pass numeric values to a function.

Keywords.
Arguments]

General term for information passed to a function


Arguments must be specified by writing them inside the function's ().
In the sum function created in the previous section, the argument was void.
This void is specified when the argument is not used.

When using arguments, inside the (), declare a variable that will store the number passed in.
To store a number in the variable max of type int in the sum function, do the following

Source code
 int sum(int max);

The part that specifies the variable to be used is called a temporary argument.

keyword
Temporary Arguments]

The type and name of the arguments written in the function declaration.


If the function is modified, the prototype declaration must do the same, but the
In the prototype declaration, variables may be omitted and just the type may be used.
The following program is an example of modifying the sum function to accept arguments.

source code
 #include <stdio.h>

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

int main(void)
{
    return 0;
}

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

Variables specified as arguments can be used within the function as if they were ordinary variables.
Passing numbers to functions
When calling a function with arguments, you must pass a numeric value.
To do so, write the number inside the () when you call it.
The following program is an example of passing the number 50 to the sum function.
Note that the number passed to a function is sometimes referred to as the actual argument.

Keywords.
[Actual Argument

A numerical value passed when calling a function.



Source Code
 #include <stdio.h>

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

int main(void)
{
    sum(50); /* 50 is passed */
    return 0;
}

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

The result of executing this program is as follows

Execution Result
1275

This means that the number 50 specified when calling the sum function is
The variable max, which is a temporary argument of the sum function, is assigned to the variable max.
Naturally, changing the numerical value when calling the sum function will also change the displayed result.

When calling a function with arguments, you must always pass a numerical value.
For example, the following call to the sum function will result in an error

Source Code
 sum();

Multiple Arguments
Arguments are not limited to just one.
You can create a function with as many arguments as you like, if needed.

To turn the sum function into a function that finds the sum of min to max, use the
This can be accomplished by increasing the number of arguments to two and replacing the expression inside.
The following function is an example of a function modified to find the sum of min to max.

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

When multiple arguments are used, each is separated by a , character.
Here we use the int type for both, but you can mix different types.

When calling this function, also separate the numbers with ,.
Real and dummy arguments correspond one-to-one, in the order in which they are listed.
The following program is an example of calling the modified sum function.

source code
 #include <stdio.h>

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

int main(void)
{
    sum(50, 100);
    return 0;
}

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

The result of executing this program is as follows

Execution Result
3825

The prototype declaration has also been modified.
Also, a function with multiple arguments cannot be called unless all arguments are passed numerical values.


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