In the previous section, we factored the calculation of the sum from 1 to 100 by creating a sum function.
"However, it would be inconvenient if it could only calculate the sum of numbers from 1 to 100."
It would be more convenient if I could calculate sums over a wider range.
For situations like these, you can pass information (numbers) to a function.
The mechanism for that is called the argument.
Arguments allow you to pass numbers to a function.
キーワード
【argument】
arguments
Arguments must be specified within the parentheses () of the function.
"In the sum function created in the previous section, the argument was void."
This `void` is specified when there are no arguments.
When using arguments, the parentheses () contain the variable that will store the passed value.
When storing numeric values into an int variable max using the sum function, proceed as follows.
Source code
int sum(int max);
In this way, the part that specifies the variables to be used is called arguments.
キーワード
【default argument】
The types and names of arguments specified in a function declaration.
If you modify a function, you also need to update its prototype declaration.
Prototype declarations allow you to omit the variable and specify only the type.
"The following program is an example of how to modify 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;
}
The variables specified as arguments can be used within the function just like regular variables.
Passing a number to a function.
When calling a function with arguments, you must pass a number.
To use it, enter a number within the parentheses when you call it.
The following program demonstrates passing the value 50 to the sum function.
Sometimes, the numbers passed to a function are called arguments.
キーワード
【actual arguments】
The numeric value passed when calling a function.
Source code
#include <stdio.h>
int sum(int); /* Prototype Declaration */
int main(void)
{
sum(50); /* 50を渡している */
return 0;
}
int sum(int max)
{
printf("%d\n", (1 + max) * max / 2);
return 0;
}
The output of this program is as follows.
Execution results
1275
This is because the number 50 specified when calling the sum function...
"It's the variable max that's being passed as an argument to the sum function."
Of course, changing the numbers when calling the sum function will change the displayed result.
When calling a function with arguments, you must always pass a number.
For example, calling the sum function like this will result in an error.
Source code
sum();
Multiple arguments
The argument is not limited to just one.
If needed, you can create a function with as many arguments as you like.
"To make the sum function calculate the sum of values between min and max,"
It can be achieved by increasing the number of arguments to two and swapping the expressions inside.
"The following function is an example modified to calculate the sum within a range from min to max."
Source code
int sum(int min, int max)
{
printf("%d\n", (min + max) * (max - min + 1) / 2);
return 0;
}
When using multiple arguments, separate each with ,.
Here, we're using `int` for both, but it's fine to mix different types.
When calling this function, specify the numbers separated by commas as well.
Actual arguments and formal parameters correspond one-to-one in the order they are listed.
The following program demonstrates an example of calling an improved 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 output of this program is as follows.
Execution results
3825
"We have also revised the prototype declaration."
Furthermore, functions with multiple arguments cannot be called unless a numeric value is passed to all of them.
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.