C++ Functions Introduction
In this page:
Defining and Calling Functions
A function bundles a block of code under a name so it only runs when explicitly called, rather than executing automatically top to bottom like the rest of your file. Defining a function like void greet() { cout << "Hi"; } and then calling greet(); inside main() is how you trigger that code.
Example: Defining and Calling Functions
#include <iostream>
void greet() {
std::cout << "Hello from a function!" << std::endl;
}
int main() {
greet();
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Declaration vs Definition
Because C++ reads a file from top to bottom, calling a function before its full definition appears further down would normally fail — a function prototype (declaration) up top tells the compiler the function exists and what it looks like, letting you define the actual body later in the file.
Example: Declaration vs Definition
#include <iostream>
void greet();
int main() {
greet();
return 0;
}
void greet() {
std::cout << "Called before its full definition" << std::endl;
}
Login to try C/C++/Java/PHP code in the editor
Functions with No Arguments
Functions don't need any parameters at all if they don't require outside data to do their job — a function like void showMenu() that just prints a fixed set of options is a common example. These parameterless functions are especially handy for repeated, self-contained actions like logging or displaying static text.
Example: Functions with No Arguments
#include <iostream>
void showMenu() {
std::cout << "1. Start" << std::endl;
std::cout << "2. Exit" << std::endl;
}
int main() {
showMenu();
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Multiple Functions
Breaking a program into many small functions, each handling one specific task, and having some functions call others, keeps individual pieces of logic focused and easier to test in isolation. This is the foundation of writing maintainable code rather than one long, unbroken block of instructions.
Example: Multiple Functions
#include <iostream>
void printHeader() {
std::cout << "=== Report ===" << std::endl;
}
void printBody() {
std::cout << "Report contents" << std::endl;
}
int main() {
printHeader();
printBody();
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Local vs Global Variables
A variable declared inside a function is local and only exists while that function is running, invisible to any other function — while a variable declared outside every function is global and can be read (and modified) from anywhere in the file. Relying heavily on global variables tends to make bugs harder to trace, since any function could be the one that changed a shared value.
Example: Local vs Global Variables
#include <iostream>
int globalCount = 100;
void showLocal() {
int localCount = 5;
std::cout << "Local: " << localCount << std::endl;
}
int main() {
showLocal();
std::cout << "Global: " << globalCount << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first:
- C++ Functions Introduction
- C++ Function Parameters
- C++ Multiple Function Parameters
- C++ Passing by Reference
- C++ Passing Structures to Functions
- C++ Return Values
- C++ Function Overloading
- C++ Default Arguments
- C++ Recursion
- C++ Inline Functions
- C++ Lambda Functions
- C++ Scope & Lifetime
- C++ Math Functions