﻿ Passing numbers to functions - Painful C
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.

Comment
COMMENT

Open the 💬 comment submission box