C++ Parameters
The functions page ended with forty-three lines on the screen, and the last one was call, the text printed by announce(). This page starts from that screen, in the same file types.cpp, and the first box still holds the word pine. The empty announce function stays above main. This page continues the Functions module. The new work adds a second function that takes a value in its parentheses. That value is a parameter. The call sends an argument into it, and the body prints what arrived. The screen grows to forty-four lines, and the last one is tag.
An empty function always does the same work. A function with a parameter can reuse the same body with a different value each time you call it. You write the type and a name inside the parentheses of the definition. At the call, you put a matching value inside the parentheses. Inside the body, you use the parameter name as if it were a local box that already holds that value. The workshop file still ends in main with the same loops and the same announce call, so you can see the new parameter next to the empty case.
Programs pass values into functions constantly. A label is printed with different text. A count is checked with different numbers. This page keeps the value as a std::string, so the screen shows a word you recognize, without teaching return values yet. The next tool in this module will send a value back out; here the function only receives.
The new definition sits above main, next to announce. The line void show_label(std::string label) begins it. The word void still means nothing is sent back. The name show_label is the label you call. Inside the parentheses, std::string label declares the parameter: a string box named label that the caller fills. The braces hold the body. The body prints that box with std::cout << label << std::endl;.
The call sits inside main after announce(); and before return 0;. The call looks like show_label("tag");. The text "tag" is the argument. When main reaches that line, the program copies "tag" into label, runs the body, prints tag, then returns to main. The earlier announce(); still runs first, so call still appears on the screen above tag.
Walk the new ending. announce(); prints call. Then show_label("tag"); fills label with tag and prints it. One new line appears. The first forty-three lines match the functions page. The forty-fourth line is tag. If you call show_label again with another string, that second call gets its own fill of label for that pass only, then the previous argument is gone.
The argument at the call must match the parameter type. A string literal such as "tag" fits std::string label. Passing a bare number into a string parameter without a conversion is the wrong shape for this page. The parameter name is local to the function. main does not have a box named label unless you create one there separately. Only the body of show_label uses this label.
A common mistake is to leave the parameter in the definition but call show_label(); with empty parentheses. The build fails, because the call must supply the argument. Another common mistake is to print "label" in quotes inside the body, so the screen shows the word label instead of the value that arrived. The print line uses the name label with no quotes around it.
announce stays as the empty case from the previous page, so you can compare both shapes in one file. Empty parentheses mean no parameter. A type and a name inside the parentheses mean one parameter. This page stops before the next tool that sends a value back out of the function. The next page names that tool once at the end.
Type the file in this order, or add only the new lines if types.cpp is already on disk. Above int main(), keep announce, and add show_label with its std::string label parameter and the print of label. Near the end of main, after announce(); and before return 0;, add show_label("tag");. The file name on disk stays types.cpp. The checked file is below, and the new lines are the show_label definition and its call.
#include <iostream>
#include <string>
void announce()
{
std::cout << "call" << std::endl;
}
void show_label(std::string label)
{
std::cout << label << std::endl;
}
int main()
{
// the word stays pine
std::string word = "pine";
int count = 2;
double ratio = 0.5;
bool ready = true;
double price = 7.9;
const int limit = 10;
int scores[3] = {4, 5, 6};
std::string other = "cone";
std::string joined = word + " " + other;
int total = 17;
int step = 3;
step += 4;
int score = 72;
int goal = 50;
bool wet = false;
std::string result;
int day = 3;
std::string name;
std::cout << word << std::endl;
std::cout << count << std::endl;
std::cout << ratio << std::endl;
std::cout << ready << std::endl;
std::cout << word.size() << std::endl;
std::cout << joined << std::endl;
std::cout << word[0] << std::endl;
std::cout << joined.find("cone") << std::endl;
std::cout << joined.substr(5, 4) << std::endl;
std::cout << count * 4 << std::endl;
std::cout << 7 / 2 << std::endl;
std::cout << 7.0 / 2 << std::endl;
std::cout << (count > 1) << std::endl;
std::cout << (count == 3) << std::endl;
std::cout << !ready << std::endl;
std::cout << static_cast<int>(price) << std::endl;
std::cout << static_cast<double>(count) / 4 << std::endl;
std::cout << static_cast<int>(ratio * 3) << std::endl;
std::cout << limit << std::endl;
std::cout << limit * count << std::endl;
std::cout << scores[0] << std::endl;
std::cout << scores[2] << std::endl;
std::cout << total % 5 << std::endl;
std::cout << step << std::endl;
std::cout << (score >= goal) << std::endl;
std::cout << (score < goal) << std::endl;
std::cout << ((score >= goal) && (count == 2)) << std::endl;
std::cout << ((score < goal) || wet) << std::endl;
if (score >= goal) {
result = "pass";
} else {
result = "retry";
}
std::cout << result << std::endl;
if (wet) {
std::cout << "wet" << std::endl;
}
switch (day) {
case 1:
name = "mon";
break;
case 2:
name = "tue";
break;
case 3:
name = "wed";
break;
default:
name = "other";
break;
}
std::cout << name << std::endl;
for (int i = 0; i < 3; i++) {
std::cout << i << std::endl;
}
int n = 3;
while (n > 0) {
std::cout << n << std::endl;
n--;
}
for (int k = 0; k < 5; k++) {
if (k == 2) {
continue;
}
if (k == 4) {
break;
}
std::cout << k << std::endl;
}
for (int value : scores) {
std::cout << value << std::endl;
}
announce();
show_label("tag");
return 0;
}
Rebuild with g++ -std=c++17 types.cpp -o types, then run ./types. If g++ is missing and clang++ is the compiler you installed, swap only the tool name: clang++ -std=c++17 types.cpp -o types. The screen shows forty-four lines. The first forty-three are the same as on the functions page. The last line is tag, the argument that filled label.
Check the last two lines first. They should be call, then tag. The table gives each row its piece, so compare your run with it line by line. If the screen still ends with call, the show_label call is missing or sits after return 0;. If the build fails about the number of arguments, the call has empty parentheses. If the last line is the word label, the print inside the body quoted the name instead of using the parameter.
📊 The show_label parameter and what it shows
| Piece | What the screen shows |
|---|---|
| First | call, because announce still runs before show_label |
| Last | tag, because show_label("tag") fills label and prints it |
| Rule | a parameter receives the argument from the call and the body uses that name |
| Screen | pine, 2, 0.5, 1, 4, pine cone, p, 5, cone, 8, 3, 3.5, 1, 0, 0, 7, 0.5, 1, 10, 20, 4, 6, 2, 7, 1, 0, 1, 0, pass, wed, 0, 1, 2, 3, 2, 1, 0, 1, 3, 4, 5, 6, call, tag |
Try one change at a time. Change show_label("tag") to show_label("note"), leave the definition as it is, and rebuild. The last line becomes note, because a different argument filled label. Put "tag" back. Next, add a second call show_label("tip"); above return 0;, leave the first call as it is, and rebuild. The last three lines become call, then tag, then tip, because each call fills label once. Remove the second call. Each experiment changes one thing, so the screen shows exactly which argument caused the difference. Undo each change before the next one starts, so the file always returns to the checked version.
This page ends the parameter tool and stops before the next one. The next page is return, which sends a value back out of a function to the caller. The show_label you have just written only receives a value; it does not send one back. You leave with forty-four lines on the screen, and the last one is tag. Count the lines before you leave. There are forty-four, and the last one is tag.
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 Instructions
- Read each question carefully
- Select the best answer for each question
- You can retake the quiz as many times as you want
- Your progress will be shown at the top