Opening the reading…
Opening the reading…
PROGRAMMING FUNDAMENTALS › C++ FUNDAMENTALS
The expression square(5) is a function call. It says that main should use the function named square, passing 5 as its argument. The definition of square is a separate piece of program structure. Its return type, parameter, braces, and return statement describe how the calculation is performed.
int square(int x) {
return x * x;
}
int main() {
cout << square(5);
return 0;
}The braces after int main() mark the body and scope of main. They can contain statements such as cout << square(5), but they cannot contain an ordinary named function definition. The call crosses from main to the separate definition of square, which receives 5 and returns 25.
In the running example, which line is the function call rather than the function definition?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
This version places the complete definition of square between the braces that belong to main. C++ permits local variables in that scope, but an ordinary named function definition is not a statement that can be nested inside main.
int main() {
int square(int x) {
return x * x;
}
cout << square(5);
return 0;
}The compiler reports a structural error before the program can run. The problem is not the value 5, the multiplication, or the call syntax. The problem is that the braces following main contain a second ordinary function definition instead of only the statements that make up main.
Move the complete definition of square outside the braces of main and place it before main. The call itself stays at the same point inside main. This changes where the compiler finds the definition, not what the call asks square to calculate.
int square(int x) {
return x * x;
}
int main() {
cout << square(5);
return 0;
}When main evaluates square(5), the value 5 is passed into the parameter x. The local variable x exists during that call, and the return statement evaluates x * x as 5 * 5. The value 25 travels back to the call in main, so cout prints 25.
Type the corrected top-level header order, with square defined before main.
int main() {
cout << square(5);
return 0;
}
int square(int x) {
return x * x;
}Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
Java uses the same separation between a method call and a method definition. square(5) remains inside main, while square is a separate static method. The difference is the outer boundary: Java places both methods inside a class, whereas the C++ example places the two function definitions at file level.
class Main {
static int square(int x) {
return x * x;
}
public static void main(String[] args) {
System.out.println(square(5));
}
}The class braces surround both methods, but the braces after main still surround only main's body. Placing square between those braces would be the same structural mistake in Java: the method would be inside another method instead of beside it. In the valid version, square(5) calls the separate method and prints 25.