Small Steps I — Creating with Text and Numbers
Receiving a function's result
Receive and print the result of square(6).
What you will learn
A return value is a value sent back to the caller. return sends the value back and ends that function's execution.
Try it
Example · example.cpp
int square(int number)
{
return number * number;
}
void small_main()
{
int result = square(4);
print(result);
}
The int at the beginning means the result is an integer. The program stores the 16 returned by square(4) in result. The function handles calculation; small_main handles output.
input_int was also a function that returns a value: the integer it read.
Expected output:
16
Exercise
Receive and print the result of square(6).
Exercise starter · exercise_starter.cpp
int square(int number)
{
return number * number;
}
void small_main()
{
int result = square(4);
print(result);
}
Hint
Change the parts of the example that you need.
show solution
int square(int number)
{
return number * number;
}
void small_main()
{
int result = square(6);
print(result);
}