3.6. Definitions and UsesΒΆ
Pulling together all the code fragments from the previous section, the whole program looks like this:
This program contains three function definitions: new_line
, three_line
,
and main
.
Inside the definition of main
, there is a statement that uses or calls
three_line
. Similarly, three_line
calls new_line
three times. Notice that
the definition of each function appears above the place where it is
used.
This is necessary in C++; the declaration or definition of a function must appear before (above) the first use of the function. It is possible for only the declarations for appear before a function is used and for the definition to appear later. These are called forward declarations.
Try This!
You should try compiling both programs with the functions in a different order and see what error messages you get.
Q-3: The function declaration or definition must be written the first use of the function.
void print_name()
-
This declaration is missing a
;
. total_cost_after_tax () {
-
This declaration is missing a return type.
void todays_weather ();
-
Correct!
void final_grade {
-
This declaration is missing parentheses. Even if a function does not take in any parameters, empty parentheses should be used.
Q-4: Which of the following is a correct function declaration?
Construct a block of code that correctly defines a the add_two function.
More to Explore
From cppreference.com