Information about cookies on this page

Our website uses cookies to ensure the functionality of the website, to collect and analyze information about the performance and usage of the website. Privacy Policy

  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in
  
  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in

Calling Functions

To make a function run, you have to call it: write its name, then parentheses and a semicolon, because a function call is a statement. You call the function from the body of main:

#include <iostream>

void sayHello() {
    std::cout << "Hello!" << std::endl;
    std::cout << "Have a nice day!" << std::endl;
}

int main() {
    sayHello();
}

This program prints:

Hello!
Have a nice day!

When the program, while running main, reaches a function call, it jumps into the function, runs its body from top to bottom and then continues with the statement after the call. You can call a function as many times as you like:

#include <iostream>

void sayHello() {
    std::cout << "Hello!" << std::endl;
}

int main() {
    std::cout << "Start" << std::endl;
    sayHello();
    sayHello();
    std::cout << "End" << std::endl;
}

This program prints:

Start
Hello!
Hello!
End

The program starts in main: it prints Start, runs the body of sayHello twice and finally prints End. A function that nobody calls never runs.

The compiler reads the program from top to bottom, and it must already know the function you call. That's why we define functions above main. This program has the function below main:

#include <iostream>

int main() {
    sayHello();
}

void sayHello() {
    std::cout << "Hello!" << std::endl;
}

The program doesn't compile, and you see this error in the output:

main.cpp:4:5: error: use of undeclared identifier 'sayHello'
    4 |     sayHello();
      |     ^~~~~~~~
1 error generated.

The error message says that line 4 uses the name sayHello, which hasn't been declared (announced) yet. When the compiler got to line 4, it didn't know about the function below main. Moving the whole function above main fixes it.

In programs on the internet, you will sometimes see only the first line of a function, ending with a semicolon, above main, for example void sayHello();, and the whole function below main. Such a line is called a declaration: it tells the compiler that the function exists. That works too, but in this course, we write whole functions above main.

Watch out for two more common mistakes:

  • Missing parentheses. sayHello; without parentheses doesn't call the function. The program compiles, but the function doesn't run and nothing is printed.
  • A different name. The name in the call must be exactly the same as in the definition, including upper and lower case. SayHello(); ends with the error error: use of undeclared identifier 'SayHello'; did you mean 'sayHello'?

Instructions

  1. 1

    In the body of main, call the function firstFunction three times.

  2. 2

    Add a second statement to the body of firstFunction that prints Functions are great!. Run the program: each of the three calls now prints both lines. You changed the code in one place, and all three calls do something new. That's exactly what functions are good for.

Back

2/10

  1. 1. Defining Functions
  2. 2. Calling Functions
  3. 3. Parameters and Arguments
  4. 4. Default Parameter Values
  5. 5. Returning Values
  6. 6. Print or Return?
  7. 7. return Ends the Function
  8. 8. Local Variables
  9. 9. Functions and Vectors
  10. 10. Functions Summary
Next

Try it yourself

Do this exercise right here in your browser. No account needed.