Parameters and Arguments
The functions you have written so far do exactly the same thing every time. print_banner always prints the same three lines. But often you want a function to do almost the same thing, only a little differently each time: to print a line of 5 dashes, and then a line of 20. A function gets such values through parameters:
#include <stdio.h>
void print_line(int length) {
for (int i = 0; i < length; i++) {
printf("-");
}
printf("\n");
}
int main(void) {
print_line(5);
print_line(20);
return 0;
}This program prints:
-----
--------------------In the definition, int length in the parentheses is a parameter: a variable that the function can use in its body. Like any variable, it has a data type and a name, only without a semicolon. It takes the place of the word void, which meant that the function takes nothing. In the call, the value in the parentheses, 5, is an argument. When you call the function, the argument is stored in the parameter, and then the body runs. On the first call, length is 5; on the second call, it is 20.
These two words are easy to mix up, so remember: a parameter is a variable in the definition, an argument is a value in the call. An argument can be a number, a variable or a whole expression, such as print_line(width * 2). And now you know what "Hello!\n" in printf("Hello!\n") is: an argument of the function printf.
A function can have several parameters, separated by commas. Each parameter has its own data type, even when the types are the same: int home, int away, not int home, away. The arguments go into the parameters in order: the first argument into the first parameter, the second into the second, and so on:
#include <stdio.h>
void print_score(int home, int away) {
printf("Score: %d:%d\n", home, away);
}
int main(void) {
print_score(2, 1);
print_score(0, 3);
return 0;
}This program prints:
Score: 2:1
Score: 0:3C checks that the arguments fit the parameters. Each parameter needs its argument. If you write only print_score(2); in the first call, the program doesn't compile:
main.c:8:18: error: too few arguments to function call, expected 2, have 1
8 | print_score(2);
| ~~~~~~~~~~~ ^
main.c:3:6: note: 'print_score' declared here
3 | void print_score(int home, int away) {
| ^ ~~~~~~~~~~~~~~~~~~
1 error generated.The first line says that the call has too few arguments: the function expects 2, but the call has 1. The note below shows where the function is defined. With too many arguments, the message says too many arguments to function call.
The type of each argument should fit its parameter too. Text where a number belongs, such as print_score("two", 1), doesn't compile. But a decimal number for an int parameter is quietly cut to a whole number: print_score(2.7, 1) prints Score: 2:1. And when you swap two arguments of the same type, the compiler can't notice at all: print_score(1, 2) simply prints a different score. So always check the order of your arguments.
Now you can also see why we write (void) for a function without parameters. With void greet(void), the call greet(5) is an error: too many arguments to function call, expected 0, have 1. With empty parentheses, void greet(), C would let the call through. You will see empty parentheses in other people's programs, but (void) is safer.
Instructions
Above main, write a function print_row with two parameters: symbol of type char and count of type int. It returns nothing. It prints symbol as many times as count says, all on one line: in a for loop, print only the character (with %c), and end the line after the loop.
In main, call print_row('*', 5) and then print_row('#', 3). The program prints ***** and ###.
An argument can also be a variable. Below the two calls, write a for loop in which i goes from 1 to 4 and which calls print_row('*', i). In each pass of the loop, the function gets a different argument, so the program draws a triangle:
*
**
***
****Start programming for free
4/12