Skip to main content

Section 5.8 Declaring vs Defining Functions

Up until now, we have been declaring functions at the same time we have defined them. But it is possible to declare a function without defining it right away.
A function declaration is a statement that tells the compiler about a function’s name, return type, and parameters. To write a function declaration with a definition, we write its prototype, but instead of providing the body, we just place a ; at the end:
int doubleValue(int num);
When we declare a function, we are telling the compiler that the function exists and that it will be defined later. The declaration lets the compiler know β€œthere will be a function called doubleValue that takes in an int and returns an int”. With that information, the compiler can look at a call to the function like int x = doubleValue(3); and decide if it makes sense or not. Later on, when the declaration is provided, the compiler can link that call to the code in the function.
A function definition is the actual implementation of the function. It is what we have seen up to now - the prototype plus the body of the function. (A definition always declares the function as well.)
int doubleValue(int num) {
    int result = 2 * num;
    return result;
}
This distinction allows us to write code where a function call comes before the definition of that function. This flexibility is useful when we have multiple functions that call each other, or when we want to separate the implementation of a function from its use. Say I want to put the main function in my program at the top of my code file. I can do so by first declaring my functions, then writing main, then defining my other functions.
Listing 5.8.1.
Try running the program above. Then comment out the declaration of doubleValue on line 5. That should produce a compile error on line 11. When the compiler gets to that line, it sees a call to doubleValue, but does not yet know about that function.
Think of a declaration as you making a promise to the compiler β€œthis function will exist”. When you define the function, you are fulfilling that promise. Here is a brief summary of when definitions and declarations must occur:
  • A function declaration must occur before the function is used. Although you would not want to on purpose, it is OK to declare a function multiple times. (You are just making the same promise multiple times.)
  • A function definition must occur once in the program, but can appear at any point after the declaration. If you try to define a function multiple times, it will be a compiler error.
  • Defining a function declares it. So you only need to write a separate declaration if you want to use the function before defining it.

Checkpoint 5.8.1.

Put these blocks of code into a valid order where the definition of main comes before any other function definition. There are many possible correct orderings.

Checkpoint 5.8.2.

If we wanted to create a function called isPrime, which takes an int input as a parameter and returns either true or false, which of the following would be the correct function declaration?
  • boolean isPrime(int input);
  • In C++, use the bool keyword for a boolean.
  • bool isPrime (input);
  • In a function header, the type of each variable must be specified in the parameter list.
  • int isPrime (input);
  • The function should return true or false, not an integer.
  • bool isPrime(int input);
  • This is the correct function header for the function.
  • int isPrime(bool input);
  • Take a closer look at what the return type is.
You have attempted of activities on this page.