All lessonsOpen Open Open Open Open
5. Functions
Parameters and return values
0 of 5 activities0%
Reading 1
Inputs and outputs of a function
Parameters are the variables in the function header. Arguments are the values you pass when you call it.
The return type says what comes back. return expression; ends the function and provides that value.
A function can take multiple parameters: int max2(int a, int b).
int max2(int a, int b) {
if (a > b) return a;
return b;
}
int main() {
cout << max2(3, 8) << endl; // 8
return 0;
}Check 2
Argument vs parameter
In max2(3, 8), what are 3 and 8?
Fill in 3
Sending a result
Try it 4
max2
Call max2 with other pairs.
main.cpp
Loading editor…
Output will appear here.
Assignment 5
Write triple
Write int triple(int x) that returns 3 * x. In main, read one int and print triple(n).
main.cpp
Loading editor…