100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
C++

Functions in C++

Learn what a function is in C++, why programs are broken into functions, and how reusability, modularity, and readability improve with them.

FunctionsBeginner6 min readJul 7, 2026
Analogies

1. Intro

A function in C++ is a named, reusable block of code that performs a specific task. Instead of writing the same logic again and again, you write it once inside a function and call it wherever needed. Every C++ program has at least one function — main() — which is the entry point where execution begins.

🏏

Cricket analogy: A team's warm-up routine of 'net practice' is drilled once and repeated every match day, just as a function is written once and called wherever needed instead of retyping the same logic.

2. Syntax

cpp
returnType functionName(parameterList) {
    // function body
    // statements
    return value; // optional, depends on returnType
}

3. Explanation

A function has four main parts: the return type (the data type of the value it sends back, or void if nothing is returned), the function name (an identifier used to call it), the parameter list (inputs the function accepts, which can be empty), and the function body (the statements that run when the function is called). Using functions brings three big benefits: reusability (write once, call many times), modularity (break a large problem into smaller, manageable pieces), and readability (each function name documents what a chunk of code does, making the program easier to follow and debug).

🏏

Cricket analogy: A bowling action has a run-up (parameter list), a delivery type (body), and a result like 'wicket' or 'dot ball' (return type) — and specializing lets a team have distinct death-over and powerplay bowlers, just as functions bring reusability and modularity.

4. Example

cpp
#include <iostream>
using namespace std;

// function definition
int square(int n) {
    return n * n;
}

int main() {
    int result = square(5);   // function call
    cout << "Square of 5 is: " << result << endl;
    cout << "Square of 8 is: " << square(8) << endl;
    return 0;
}

5. Output

text
Square of 5 is: 25
Square of 8 is: 64

6. Key Takeaways

  • A function is a named, reusable block of code that performs a specific task.
  • Every C++ program starts execution from the main() function.
  • Functions improve reusability, modularity, and readability of code.
  • A function is defined once but can be called as many times as needed.

Practice what you learned

Was this page helpful?

Topics covered

#CStudyNotes#Programming#FunctionsInC#Functions#Syntax#Explanation#Example#StudyNotes#SkillVeris#ExamPrep