Coding/Learn/cpp/functions basics
C++ Foundational Course
01.Why C++ Powers the Modern World
02.Anatomy of a C++ Program
03.Semicolons, Statements & Comments
04.Variables & Fundamental Data Types
05.Console I/O: std::cout and std::cin
06.Arithmetic, Modulo & Increment Operators
07.Branching Decisions: if, else if, and else
08.Logical Operators (&&, ||, !)
09.Switch Statements: Fast Multi-Way Selection
10.While & Do-While Loops
11.The Standard For Loop & Counting
12.Loop Control: break and continue
13.Functions: Modular, Reusable Code Blocks
14.Pass by Value vs Pass by Reference (&)
15.Fixed-Size Arrays & Contiguous Memory
16.Dynamic Arrays: std::vector and STL
17.Strings Deep Dive & Manipulation
18.Unmasking Memory: Pointers & Addresses (& and *)
19.Custom Data Types: Structs & OOP Basics
20.Capstone: Command-Line Bank & Account System
Course Certificate0%

Complete all lessons to earn

LearnFunctions: Modular, Reusable Code Blocks
13 / 20 0%
Lesson 13 of 20
5 mins read

Functions: Modular, Reusable Code Blocks

PreviousNext Lesson
In Plain English

A function is a reusable block of code that performs a specific task. You define it once, give it inputs (parameters), perform logic, and return a result. Functions keep code modular, testable, and maintainable.

The Mental Model

"A function is like a kitchen blender. You pour ingredients in (parameters), press the button (execute code), and pour out a delicious smoothie (return value). You can use the blender 50 times without having to reinvent it."

Deep Dive: How It Works

Return Type: The data type of the result (e.g. int, double, void if returning nothing).

Parameters: Inputs accepted by the function (e.g. int a, int b).

Function Prototypes: In C++, functions must be declared or defined BEFORE they are called. If a function is written below main(), declare its prototype above main(): int add(int a, int b);.

void Functions: Functions that perform actions (like printing) without returning a value.

Syntax Blueprint

// ReturnType FunctionName(Parameters) {
//   Body
//   return value;
// }
int multiply(int a, int b) {
  return a * b;
}

Define return type, name, input parameters, and return statement.

PointCore Rules to Remember

Point star
Reusability (DRY): Don't Repeat Yourself—package repeated logic into callable functions.
Point star
void for No Return: Use void when a function only performs side-effects like logging or printing.
Point star
Prototypes Above Main: Declare signatures at the top of the file so main() knows they exist.

Common Beginner Traps & How to Fix Them

Mistake:Forgetting to return a value from a non-void function

Why it happens: Forgetting return result; at the end of an int function.

How to fix: All non-void functions must return a value matching their declared type.

Pro Tip: Give functions verb-based descriptive names like calculateTax(), findMax(), or printReport()!

Live Interactive Example

Hit Run Code to see it live
Creating and Calling Functions
C++ 20
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Output Console
Click "Run Code" to view the rendered output.
How it works: calculateArea() is called with different inputs, returning 20 and 30.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Write a square function

  • PointWrite an int square(int x) function that returns x * x.
  • PointIn main(), print "Square of 6 is 36" by calling square(6).
1
2
3
4
5
6
7
8
9
10
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates