C++ Functions Introduction
In this page:
return_type function_name(); // declaration
return_type function_name() { // definition
// body
}
function_name(); // call
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 std::cout and std::cin
#include <iostream>
void greet() {
// Print to the console with cout
std::cout << "Hello from a function!" << std::endl;
}
// Program execution starts in main()
int main() {
greet();
// Return 0 to signal that the program finished successfully
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 std::cout and std::cin
#include <iostream>
void greet();
// Program execution starts in main()
int main() {
greet();
// Return 0 to signal that the program finished successfully
return 0;
}
void greet() {
// Print to the console with cout
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 std::cout and std::cin
#include <iostream>
void showMenu() {
// Print to the console with cout
std::cout << "1. Start" << std::endl;
// Print to the console with cout
std::cout << "2. Exit" << std::endl;
}
// Program execution starts in main()
int main() {
showMenu();
// Return 0 to signal that the program finished successfully
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 std::cout and std::cin
#include <iostream>
void printHeader() {
// Print to the console with cout
std::cout << "=== Report ===" << std::endl;
}
void printBody() {
// Print to the console with cout
std::cout << "Report contents" << std::endl;
}
// Program execution starts in main()
int main() {
printHeader();
printBody();
// Return 0 to signal that the program finished successfully
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 std::cout and std::cin
#include <iostream>
// Declare globalCount and set it to 100
int globalCount = 100;
void showLocal() {
// Declare localCount and set it to 5
int localCount = 5;
// Print to the console with cout
std::cout << "Local: " << localCount << std::endl;
}
// Program execution starts in main()
int main() {
showLocal();
// Print to the console with cout
std::cout << "Global: " << globalCount << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Calling a function before it is declared, which fails unless you add a prototype.
- Forgetting
returnin a non-void function, which is undefined behavior. - Writing
void foo();when calling and forgetting the parentheses, as infoo;, which does nothing.
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