Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Declaration vs Definition | Prototype | Functions
C++ Intermediate | Mobile-Friendly

bookDeclaration vs Definition | Prototype

Functions in C++ consist of 2 elements:

  • Declaration: the function's name with parameters (if they exist).
  • Definition: the function’s body.
void myFunc() { // Declaration
    // Definition
}

The function declaration is also called the function’s prototype. The code body (the definition) can be defined separately. It’s a good rule to write before the main function prototypes of other functions to understand how many functions we have and which arguments it passes. After the main function, you should specify the function’s definitions:

// Function prototype
void myFunc(string name);

int main() {
    myFunc("Sheldon");
    return 0;
}
// Function definition
void myFunc(string name) {
    cout << "Hi " << name;
}

It doesn’t affect your code if you specify prototypes or not. We won’t do this in further chapters since our code is still pretty simple and doesn’t need strict structurization.

question mark

Choose correct statements:

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 4. Kapitel 10

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

Awesome!

Completion rate improved to 2.94

bookDeclaration vs Definition | Prototype

Svep för att visa menyn

Functions in C++ consist of 2 elements:

  • Declaration: the function's name with parameters (if they exist).
  • Definition: the function’s body.
void myFunc() { // Declaration
    // Definition
}

The function declaration is also called the function’s prototype. The code body (the definition) can be defined separately. It’s a good rule to write before the main function prototypes of other functions to understand how many functions we have and which arguments it passes. After the main function, you should specify the function’s definitions:

// Function prototype
void myFunc(string name);

int main() {
    myFunc("Sheldon");
    return 0;
}
// Function definition
void myFunc(string name) {
    cout << "Hi " << name;
}

It doesn’t affect your code if you specify prototypes or not. We won’t do this in further chapters since our code is still pretty simple and doesn’t need strict structurization.

question mark

Choose correct statements:

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 4. Kapitel 10
some-alt