
Download this note as PDF at no cost
If any AD appears on download click please wait for 30sec till it gets completed and then close it, you will be redirected to pdf/ppt notes page.
void functionsA function is a block of code that performs a specific task. Instead of writing one long program, we divide it into smaller parts.
Modular programming means breaking a program into independent modules (functions) so that:
Real-life analogy: A big project is completed faster when divided into small tasks.
A function is usually handled in three steps:
Why prototype is useful:
sqrt() in <cmath>).maxOfTwo()).void functionsreturn.Example:
int add(int a, int b) returns an integer.void printLine() only prints.Example idea:
int add(int x, int y) → x and y are parametersadd(10, 20) → 10 and 20 are argumentsThis is a very common exam question.
A copy of actual argument is passed.
Example situation: swap(a, b) will not swap originals if passed by value.
We pass the address/reference of variable.
In C++, call by reference is commonly done using reference parameters like int &x.
A function parameter can have a default value. If the caller does not provide that argument, the default value is used.
Key rule: default arguments should generally be from the rightmost parameters.
Function overloading means using the same function name with different parameter lists. Example idea:
area(int side)area(int l, int b)The compiler decides which one to call based on arguments.
Scope means the region where a variable is accessible.
{ } (like inside if, loops).Good practice: prefer local variables to reduce bugs.
Storage class decides lifetime, scope, and sometimes memory location.
In Sem 1, you mainly need conceptual meaning + one example statement.
A static local variable is initialized only once and keeps its value between calls.
Why useful:
Example concept:
static int count = 0; count++; will continue increasing across calls.An inline function suggests the compiler to replace the function call with the function code (expansion at call site).
Advantages:
Limitations:
Important exam line: inline is a request, not a command.
Recursion is when a function calls itself.
Two must-have parts:
Stack idea (simple):
Use recursion carefully because deep recursion can cause stack overflow.
main() (compiler may not know signature).void function).Tip: Write small test cases and print intermediate values.
Prototype → Call from main() → Control goes to function → Execute body → return → back to caller
Get instant access to notes, practice questions, and more benefits with our mobile app.
void functionsA function is a block of code that performs a specific task. Instead of writing one long program, we divide it into smaller parts.
Modular programming means breaking a program into independent modules (functions) so that:
Real-life analogy: A big project is completed faster when divided into small tasks.
A function is usually handled in three steps:
Why prototype is useful:
sqrt() in <cmath>).maxOfTwo()).void functionsreturn.Example:
int add(int a, int b) returns an integer.void printLine() only prints.Example idea:
int add(int x, int y) → x and y are parametersadd(10, 20) → 10 and 20 are argumentsThis is a very common exam question.
A copy of actual argument is passed.
Example situation: swap(a, b) will not swap originals if passed by value.
We pass the address/reference of variable.
In C++, call by reference is commonly done using reference parameters like int &x.
A function parameter can have a default value. If the caller does not provide that argument, the default value is used.
Key rule: default arguments should generally be from the rightmost parameters.
Access the complete note and unlock all topic-wise content
It's free and takes just 5 seconds
From this topic
Functions support modular programming. Advantages:
Hence functions make large programs manageable.
Parameters are variables written in the function definition/prototype. Arguments are actual values passed in the function call.
Example: int add(int x, int y) → x,y are parameters; add(10,20) → 10,20 are arguments.
Call by value passes a copy of the argument to the function, so changes inside the function do not affect the original variable. Call by reference passes a reference/address, so changes inside the function reflect back in the caller.
Value: a → copy → function
Reference: a → same a → function
Conclusion: Use call by reference when the function must modify the caller’s variables, otherwise call by value is safer.