MMGameslogo  MMGames
TwitterSharebutton  FacebookSharebutton   
learn through sufferingC Language
learn through sufferingC Language

Passing a number to a function.

Functions with arguments
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.

Part 0: Program Overview
  1. What is a program?
Chapter 3: Displaying on the Screen
  1. String Display
  2. newline character
  3. Practice Problem 3
Chapter 4: Displaying and Calculating Numbers
  1. Display of numbers
  2. Basic calculations
  3. Numeric types
  4. Practice Problem 4
Chapter 6: Input from the Keyboard
  1. input function
  2. The fear of input
  3. Practice Problem 6
Chapter 9: Repeating a Fixed Number of Times
  1. Iterative sentence
  2. How Loops Work
  3. Practice Problem 9
Chapter 10: Repeating Without Knowing the Number of Times
  1. Unspecified loop
  2. Input validation
  3. Practice Problem 10
Chapter 13: Handling Multiple Variables at Once
  1. Handling multiple variables collectively.
  2. Arrays
  3. Practice Problem 13
Chapter 19: Dynamic Arrays
  1. Create arrays freely.
  2. Practice Problem 19