C++ Functions

A function is a block of code that only runs when you call it. Functions let you group related statements together, give them a name, and reuse them as many times as you like. Instead of writing the same instructions over and over, you write them once inside a function and then call that function whenever you need it.

Why use functions?

Functions are one of the most important building blocks in C++. They help you organize a program into smaller, understandable pieces. When code is split into functions, it is easier to read, easier to test, and much easier to fix when something goes wrong. This idea of writing code once and reusing it is often called DRY: Don't Repeat Yourself.

  • Reuse: write the logic once and call it many times.
  • Organization: give a meaningful name to a group of statements.
  • Readability: a well-named function tells the reader what the code does.
  • Testing: small functions are easier to check for correctness.

Creating and calling a function

C++ already provides predefined functions, such as main(), which is where every program starts running. To create your own function, you write a return type, a name, a pair of parentheses (), and a body inside curly braces {}. To run the function, you call it by writing its name followed by parentheses.

Define once, call it

#include <iostream>
using namespace std;

// Create a function called myFunction
void myFunction() {
  cout << "I just got executed!";
}

int main() {
  myFunction(); // Call the function
  return 0;
}

// Outputs: I just got executed!

The keyword void means the function does not return any value. The name myFunction is your own choice, but by convention function names start with a lowercase letter. Notice that defining a function does not run it; the code inside only runs when you call myFunction() from main().

Calling a function multiple times

#include <iostream>
using namespace std;

void greet() {
  cout << "Hello!\n";
}

int main() {
  greet();
  greet();
  greet();
  return 0;
}

// Outputs:
// Hello!
// Hello!
// Hello!
Note: A function must be declared before it is used. The easiest way is to define the whole function above main(). You can also write a declaration (also called a prototype) first and place the full definition later in the file.

Declaration and definition

A function has two parts. The declaration (the return type, name, and parameters) tells the compiler the function exists. The definition is the body with the actual code. You can split them so that main() can appear at the top of your file while the details live below it.

Declaration above, definition below

#include <iostream>
using namespace std;

// Declaration (prototype)
void myFunction();

int main() {
  myFunction();
  return 0;
}

// Definition
void myFunction() {
  cout << "I was defined after main()!";
}
PartExampleMeaning
Return typevoidThe type of value the function gives back (void = nothing)
NamemyFunctionThe identifier used to call the function
Parentheses()Holds parameters; empty means no parameters
Body{ ... }The statements that run when the function is called

Exercise: C++ Functions

What is function overloading in C++?