Student Notes
Beginner to Intermediate
Theory + Practical
After completing this lecture, students will be able to:
In Lecture 2, we learned:
cout and return.Example:
int add(int a, int b)
{
return a + b;
}
Here:
a and b are parameters.10 and 20 would be arguments in add(10, 20).return sends the calculated result back to the calling function.In this lecture, we will learn how to organize functions, manage variables, and pass information between functions.
A function prototype is a declaration that informs the compiler about a function before it is called.
It tells the compiler:
In simple words:
A function prototype tells the compiler that a function exists and describes how it can be called.
Consider the following program:
#include <iostream>
using namespace std;
int main()
{
int result = add(10, 20);
cout << result;
return 0;
}
int add(int a, int b)
{
return a + b;
}
In this program, the function add() is called before the compiler has seen its declaration.
In modern C++, this causes a compilation error because the compiler does not know about add() at the point of the call.
We can solve this problem using a function prototype.
Syntax:
returnType functionName(parameterTypes);
Example:
int add(int, int);
Explanation:
| Component | Description |
|---|---|
int |
Return type |
add |
Function name |
int, int |
Parameter data types |
; |
Ends the declaration |
Notice that parameter names are optional in a prototype.
These declarations are also valid:
int add(int a, int b);
int add(int, int);
#include <iostream>
using namespace std;
// Function prototype
int add(int, int);
int main()
{
int result = add(10, 20);
cout << "Sum = " << result;
return 0;
}
// Function definition
int add(int a, int b)
{
return a + b;
}
Output:
Sum = 30
Program execution and compilation flow
1. Function Prototype
int add(int, int);
Compiler learns about the function.
2. main() Function
add(10, 20);
The function is called.
3. Function Definition
int add(int a, int b)
The function performs the calculation.
4. Return Result
30
These three concepts are important.
| Concept | Purpose | Example |
|---|---|---|
| Declaration | Informs compiler about the function | int add(int, int); |
| Definition | Contains the actual function code | int add(int a,int b){...} |
| Call | Executes the function | add(10,20); |
Important points:
Variable scope refers to the part of a program where a variable can be accessed.
In simple words:
Scope determines where a variable can be used in a program.
C++ variables are commonly discussed as local and global variables at this level.
A local variable is declared inside a function or block.
It can only be accessed within its scope.
#include <iostream>
using namespace std;
void display()
{
int number = 10;
cout << number;
}
int main()
{
display();
return 0;
}
Output:
10
Explanation:
number is declared inside display().display().main().#include <iostream>
using namespace std;
void display()
{
int number = 10;
}
int main()
{
display();
cout << number;
return 0;
}
This program produces a compilation error because number is not accessible inside main().
A global variable is declared outside all functions.
It can be accessed by functions in its scope, subject to normal declaration and visibility rules.
#include <iostream>
using namespace std;
int number = 100; // Global variable
void display()
{
cout << number << endl;
}
int main()
{
display();
cout << number;
return 0;
}
Output:
100
100
Explanation:
number is declared outside all functions.display() and main() can access it.| Local variable | Global variable |
|---|---|
| Declared inside a function or block | Declared outside all functions |
| Accessible within its scope | Accessible throughout its visibility scope |
| Normally exists while its block is active | Normally exists for the entire program execution |
| Usually stores function-specific data | Can hold data shared across functions |
Consider:
#include <iostream>
using namespace std;
int x = 100; // Global variable
void display()
{
int y = 50; // Local variable
cout << "x = " << x << endl;
cout << "y = " << y << endl;
}
int main()
{
display();
cout << "x = " << x << endl;
return 0;
}
Output:
x = 100
y = 50
x = 100
Notice:
x is accessible in both functions.y is accessible only inside display().Scope and lifetime are related but different concepts.
For example, an ordinary local variable is created when its block is entered and ceases to exist when that block exits.
A global variable exists for the duration of the program.
Parameter passing is the process of sending information from a function call to a function.
C++ provides different ways to pass information to functions.
In this lecture, we will study:
In pass by value, a copy of the original argument is passed to the function.
Any changes made to the parameter inside the function do not affect the original variable.
#include <iostream>
using namespace std;
void changeValue(int num)
{
num = 100;
}
int main()
{
int number = 10;
changeValue(number);
cout << "Number = " << number;
return 0;
}
Output:
Number = 10
number is initialized with 10.changeValue(number) calls the function.number is passed to num.num is changed to 100.number remains 10.Pass by value – Memory concept
main()
number
Original
changeValue()
num
Copy changed
The two variables are separate. Changing the copy does not change the original.
In pass by reference, the function parameter becomes another name (an alias) for the original variable.
Changes made to the parameter directly affect the original variable.
The reference operator & is used in the parameter declaration.
#include <iostream>
using namespace std;
void changeValue(int &num)
{
num = 100;
}
int main()
{
int number = 10;
changeValue(number);
cout << "Number = " << number;
return 0;
}
Output:
Number = 100
number is initialized with 10.changeValue(number) calls the function.num refers to the original variable number.num is changed to 100.number also becomes 100.Pass by reference – Memory concept
Original variable: number
Same variable is accessed through another name.
Function parameter: num
Same variable
Both names refer to the same object in memory.
| Pass by value | Pass by reference |
|---|---|
| Passes a copy of the argument | Parameter refers to the original variable |
| Uses a normal parameter | Uses a reference parameter with & |
| Changes do not affect the original | Changes affect the original |
| Useful when the original should remain unchanged | Useful when a function needs to modify the original |
Swapping means exchanging the values of two variables.
For example:
Before swapping:
a = 10
b = 20
After swapping:
a = 20
b = 10
#include <iostream>
using namespace std;
void swapNumbers(int a, int b)
{
int temp = a;
a = b;
b = temp;
}
int main()
{
int x = 10;
int y = 20;
swapNumbers(x, y);
cout << "x = " << x << endl;
cout << "y = " << y;
return 0;
}
Output:
x = 10
y = 20
Why?
Because only copies of x and y are swapped. The original variables remain unchanged.
#include <iostream>
using namespace std;
void swapNumbers(int &a, int &b)
{
int temp = a;
a = b;
b = temp;
}
int main()
{
int x = 10;
int y = 20;
swapNumbers(x, y);
cout << "x = " << x << endl;
cout << "y = " << y;
return 0;
}
Output:
x = 20
y = 10
Important: Pass by reference allows the function to modify the original variables.
void update(int x)
{
x = x + 5;
}
int main()
{
int a = 10;
update(a);
cout << a;
return 0;
}
| Step | Operation | a (main) | x (function) |
|---|---|---|---|
| 1 | Initialize a |
10 | – |
| 2 | Call update(a) |
10 | 10 |
| 3 | Execute x = x + 5 |
10 | 15 |
| 4 | Function ends | 10 | – |
| 5 | Display a |
10 | – |
Output:
10
void update(int &x)
{
x = x + 5;
}
int main()
{
int a = 10;
update(a);
cout << a;
return 0;
}
| Step | Operation | Value of a |
|---|---|---|
| 1 | Initialize a |
10 |
| 2 | Call update(a) |
10 |
| 3 | Execute x = x + 5 |
15 |
| 4 | Function ends | 15 |
| 5 | Display a |
15 |
Output:
15
Try these exercises to strengthen your understanding.
Problem Statement 1: Function Prototype
Write a C++ program that calculates the square of a number using a function prototype.
Tasks:
square().main().Requirements:
Problem Statement 2: Local and Global Variables
Write a C++ program that demonstrates the use of local and global variables.
Tasks:
number.display().display().Requirements:
Problem Statement 3: Pass by Value
Write a function named increase() that receives an integer and increases its value by 10.
Tasks:
main().increase().Requirements:
void return type.Problem Statement 4: Pass by Reference
Write a function named increase() that receives an integer by reference and increases its value by 10.
Tasks:
main().increase().Requirements:
&.Problem Statement 5: Swap Two Numbers
Write a function named swapNumbers() that exchanges the values of two integer variables.
Tasks:
main().Requirements:
Problem Statement 6: Calculate Total Marks
Write a function named calculateTotal() that receives marks of three subjects and returns their total.
Tasks:
Requirements:
Problem Statement 7: Update Student Marks
Write a function named updateMarks() that receives marks by reference and increases them by 5.
Tasks:
main().Requirements:
void function.Problem Statement 8: Compare Pass by Value and Pass by Reference
Develop a program with two separate functions:
changeByValue()changeByReference()Tasks:
main().Requirements:
| Mistake | Correct approach |
|---|---|
| Forgetting semicolon in prototype | End declaration with ; |
| Calling a function before declaration | Declare a prototype first |
| Accessing a local variable outside its scope | Access it only within its scope |
Using int num instead of int &num when modification by reference is required |
Use a reference parameter |
| Expecting pass by value to modify original variable | Understand that it modifies a copy |
| Using a variable before declaration | Declare it before use |
& operator in a reference parameter?Lecture 3 Knowledge Check
0/10 answered
Q1. What is the purpose of a function prototype?
A. To execute a function
B. To inform the compiler about a function
C. To declare a variable
D. To stop program execution
Q2. Which statement is a valid function prototype?
A. int add(int a, int b)
B. int add(int, int);
C. add(int, int);
D. void add(int, int)
Q3. Where is a local variable declared?
A. Outside all functions
B. Inside a function or block
C. Only in header files
D. Only in main()
Q4. What is a global variable?
A. A variable declared inside a loop
B. A variable declared inside a function
C. A variable declared outside all functions
D. A function parameter
Q5. What is passed in pass by value?
A. Original variable itself
B. A copy of the argument
C. A global variable
D. A function prototype
Q6. Which parameter uses pass by reference?
A. int x
B. float x
C. int &x
D. void x
Q7. What happens when a pass-by-value parameter is modified?
A. Original variable always changes
B. Only the parameter copy changes
C. Program stops
D. Function is deleted
Q8. What happens when a pass-by-reference parameter is modified?
A. Only a copy changes
B. Original variable changes
C. No variable changes
D. The function returns void
Q9. What is the output after calling update(a) if a is 10 and update takes int &x and adds 5?
A. 10
B. 5
C. 15
D. 0
Q10. Which concept is used to swap original variables inside a function?
A. Pass by value
B. Pass by reference
C. Function prototype only
D. Local variable only
Submit Answers
| Topic | Key concept |
|---|---|
| Function prototype | Declares a function before its use |
| Function definition | Contains actual function code |
| Local variable | Accessible within its scope |
| Global variable | Declared outside all functions |
| Variable scope | Determines where a variable can be accessed |
| Variable lifetime | Determines how long a variable exists |
| Pass by value | Passes a copy |
| Pass by reference | Parameter refers to the original variable |
& |
Reference declarator in a parameter |
Next Lecture: Advanced Functions in C++
Topics:
Important for students
Before moving to the next lecture, make sure you can:
Practice is essential: Run all examples in a C++ compiler and observe how the output changes when you modify the parameter-passing method.