As a programmer, you will find yourself reusing the same blocks of code over and over throughout your program. In times like these, you can turn to functions.

Also known as a method or procedure, a function is a named group of code statements that accomplish something together, a bit like a factory machine.

make_sandwich() adds ingredients together to make a sandwich

There are some great reasons to use functions in your code:

  • A single line can make all that code fire off instead of a whole bunch of lines.
  • You can build DRY (Don’t Repeat Yourself) code, reusing the code you already wrote.
  • Functions help make your code flexible and modular, meaning you can group your code more easily by task.

In fact, every C++ program has at least one function. “Hold on,” you may be thinking, “I’ve written some C++ programs, but I haven’t written any functions yet!”

Well, as it happens, main() is a function that you’ve already used! And you’ll understand it a bit more as you learn how functions work.



Take a look at the IT support conversations in main.cpp. There’s a lot of repeating code.

Compile and execute the code. It works, but we could make this code much shorter and cleaner with the help of a function…

IT Crowd - Have you tried turning it off and on again?

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?