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

C++ Functions Introduction

A function is a named set of steps you can use over and over, like a recipe. You write it once and call it whenever you need it.
Syntax
cpp
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

cpp
// 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;
}

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 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;
}

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 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;
}

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 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;
}

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 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Calling a function before it is declared, which fails unless you add a prototype.
  2. Forgetting return in a non-void function, which is undefined behavior.
  3. Writing void foo(); when calling and forgetting the parentheses, as in foo;, which does nothing.

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.