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
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
#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
Square of 5 is: 25
Square of 8 is: 646. 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
1. What is the entry point of every C++ program?
2. Which of the following is NOT a benefit of using functions?
3. What must a function have if it does not return any value?
4. Which part of a function documents the inputs it accepts?
5. How many times can a defined function be called in a program?
Was this page helpful?
You May Also Like
Function Declaration and Definition in C++
Understand the difference between a function declaration (prototype) and a function definition in C++, and when each is required.
Function Parameters and Arguments in C++
Learn the difference between function parameters and arguments in C++ and understand pass-by-value semantics with a hands-on example.
return Statement in C++
Understand how the return statement ends a function and optionally sends a value back to the caller, including void functions.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics