- ..clause 1..: Function with argument
- ..clause 2..: Pass the function the numerical value.
- ..clause 3..: Two or more arguments
The sum function was made from the preceding clause, and the calculation of the total of 1-100 was made parts.
However, it is inconvenient in only total of 1-100 computable.
It is convenient to be able to calculate the total of a freer range.
Information (numerical value) can be passed to the function for such time.
It is a mechanism that the mechanism for that is called an argument.
The numerical value can be passed to the function by using the argument.
The argument should be written in () of the function and be specified.
[ Argument ]
Generic name of information passed to function
When the argument is used, the variable that stores the passed numerical value is declared in ().
Make it to as follows when you store the numerical value in variable max of the int type by the sum function.
Thus, the part where the variable used is specified is called a dummy argument.int sum(int max);
Though it is necessary to do the prototype declaration similarly when the function is changed
[ Dummy argument ]
Type and name of argument written in function declaration
The variable specified for the argument can be used in the function just like a usual variable.#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; }
When the function with the argument is called, it is necessary to pass the numerical value.
Write the numerical value in () in it when calling it.
The following program is an example of passing the numerical value of 50 to the sum function.
The numerical value passed to the function might be called an actual argument.
[ Actual argument ]
Numerical value passed when function is called
The execution result of this program is as follows.#include <stdio.h> int sum(int); /* Prototype declaration */ int main(void) { sum(50); /* */ to which 50 has been passed Return 0; } int sum(int max) { printf("%d\n",(1 + max) * max / 2); return 0; }
The numerical value of 50 specified when the sum function is called : this.
1275
Pass the numerical value when you call the function with the argument.
For instance, if the function is called, it becomes an error following sum.
sum();
There is no argument because of being limited only to one.
To make the sum function a function from which the total value of min-max is requested
It is possible to achieve it by expanding the argument into two, and replacing the expression of contents.
The following function is an example of remodeling to request the total of min-max.
Switch off and specify of each in [de] district when you use two or more arguments.int sum(int min,int max) { printf("%d\n",(min + max) * (max - min + 1) / 2); return 0; }
Switch off and specify the numerical value in [de] district when you call this function.
The actual argument and the dummy argument correspond by couple 1 as it is the array order.
The following program is an example of calling the improved sum function.
The execution result of this program is as follows.#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 prototype declaration is corrected.
3825