← Back to C++ Course | Chapter 5: Functions | Lesson 1 of 13

C++ Functions Introduction

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

cpp
#include <iostream>

void greet() {
	std::cout << "Hello from a function!" << std::endl;
}

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

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

cpp
#include <iostream>

void greet();

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

void greet() {
	std::cout << "Called before its full definition" << std::endl;
}

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

cpp
#include <iostream>

void showMenu() {
	std::cout << "1. Start" << std::endl;
	std::cout << "2. Exit" << std::endl;
}

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

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

cpp
#include <iostream>

void printHeader() {
	std::cout << "=== Report ===" << std::endl;
}

void printBody() {
	std::cout << "Report contents" << std::endl;
}

int main() {
	printHeader();
	printBody();
	return 0;
}

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

cpp
#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 run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.