Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Take A QuizChallenge yourself and boost your learning! Start the quiz now to earn credits.
Take A QuizUnlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
Take A Quiz
🔹 1. Introduction
As your C++ programs grow, you’ll need a way to reuse
logic, organize code, and reduce repetition — that's where functions
come in.
Functions help divide complex problems into manageable
blocks, making code:
In this chapter, we’ll also explore scope —
understanding where variables can be accessed and how they behave.
🔹 2. What is a Function?
A function is a reusable block of code that performs
a specific task.
✅ General Syntax:
returnType
functionName(parameters) {
// code
return value;
}
🔹 3. Function Example
#include
<iostream>
using
namespace std;
int
add(int a, int b) {
return a + b;
}
int
main() {
cout << add(5, 3); // Output: 8
return 0;
}
🧠 Key Concepts:
🔹 4. Function Declaration
& Definition
int
add(int, int); // Just declares it
int
add(int x, int y) {
return x + y;
}
✅ You can declare functions at
the top and define them later in the file — useful for organizing large
projects.
🔹 5. Return Type &
Parameters
Concept |
Example |
No return |
void greet() |
No parameters |
int getYear() |
With both |
int sum(int
a, int b) |
🔹 6. Default Arguments
void
greet(string name = "Guest") {
cout << "Hello, " <<
name;
}
int
main() {
greet(); // Hello, Guest
greet("Alice"); //
Hello, Alice
}
✅ Parameters can have default
values, which are overridden when arguments are passed.
🔹 7. Function Overloading
C++ allows functions with same name but different
parameter types or counts.
int
add(int a, int b) { return a + b; }
float
add(float a, float b) { return a + b; }
cout
<< add(2, 3); // Calls int
version
cout
<< add(2.5f, 3.5f); // Calls float
version
✅ Compile-time polymorphism in
action.
🔹 8. Recursive Functions
A function that calls itself is called recursive.
int
factorial(int n) {
if (n <= 1) return 1;
return n * factorial(n - 1);
}
Use recursion carefully to avoid stack overflows!
🔹 9. Scope in C++
Scope defines where a variable can be accessed or
modified.
Type |
Defined In |
Available
Where |
Local |
Inside
function/block |
Only within
that function/block |
Global |
Outside all
functions |
Accessible
from all functions |
Static |
Inside
function/block |
Retains value
between function calls |
✅ Local Example:
void
greet() {
int age = 30; // Local
}
✅ Global Example:
int
x = 5;
void
show() {
cout << x; // Accessible
}
✅ Static Variable:
void
counter() {
static int count = 0;
count++;
cout << count << " ";
}
✅ Output on 3 calls: 1 2 3
🔹 10. Inline Functions
(Compiler Suggestion)
inline
int square(int x) {
return x * x;
}
✅ The compiler may replace the
function call with the actual code to optimize performance (used for
small functions).
🔹 Summary Table
Topic |
Key Syntax |
Function
Decl. |
int sum(int,
int); |
Function Def. |
int sum(int
a, int b) {} |
Return
Statement |
return a + b; |
Default
Argument |
void f(int x
= 0) |
Overloading |
int f(int);
float f(float); |
Recursion |
f() calls f() |
Scope |
local,
global, static |
C++ is used in game development, operating systems, compilers, IoT, robotics, and performance-critical applications
C is procedural; C++ supports both procedural and object-oriented programming
Using
pointers, references, and manual memory control with new and delete.
The Standard Template Library is a powerful collection of classes like vector, set, map, and queue.
✅ Absolutely. It's critical in industries like gaming, defense, automotive, and finance
Not commonly — C++ is not typically used for frontend or backend web apps. However, it powers backend engines in browsers.
Yes, many machine learning libraries (like TensorFlow) have C++ backends for performance.
Very easy — once you’ve mastered C++, most modern languages feel simpler.
Please log in to access this content. You will be redirected to the login page shortly.
LoginReady to take your education and career to the next level? Register today and join our growing community of learners and professionals.
Comments(0)