Beginner level
Theory + Practical
60–75 minutes
After completing this lecture, students will be able to:
return statement.In the previous lecture, we learned:
void means that a function does not return a value.Example:
#include <iostream>
using namespace std;
void displayMessage()
{
cout << "Welcome to C++!";
}
int main()
{
displayMessage();
return 0;
}
Question: What if we want to display different messages using the same function?
For example:
Writing a separate function for every student is not a good approach.
We can solve this problem using function parameters.
Parameters are variables declared inside the parentheses of a function definition. They receive values when the function is called.
In simple words:
Parameters allow us to send information to a function.
#include <iostream>
using namespace std;
void greet(string name)
{
cout << "Welcome " << name << "!" << endl;
}
int main()
{
greet("Ali");
greet("Ahmed");
greet("Sara");
return 0;
}
Output:
Welcome Ali!
Welcome Ahmed!
Welcome Sara!
void greet(string name)
void is the return type.greet is the function name.string is the data type.name is the parameter.When we call:
greet("Ali");
The value "Ali" is passed to the function and stored in the parameter name.
How a parameter receives a value
Function call
Argument: “Ali”
Function definition
Parameter receives the value “Ali”
Function output
Welcome Ali!
These two terms are very important.
Consider:
void displayNumber(int num)
{
cout << num;
}
int main()
{
displayNumber(10);
return 0;
}
| Parameter | Argument |
|---|---|
| Variable declared in function definition | Value supplied during function call |
num |
10 |
| Receives the value | Sends the value |
Remember:
A function can receive more than one parameter.
#include <iostream>
using namespace std;
void add(int a, int b)
{
int sum = a + b;
cout << "Sum = " << sum;
}
int main()
{
add(10, 20);
return 0;
}
Output:
Sum = 30
add(10, 20);
| Parameter | Received value |
|---|---|
a |
10 |
b |
20 |
Calculation:
\[ sum = a + b \]
\[ sum = 10 + 20 = 30 \]
Important: The number and order of arguments should match the function parameters, and their values must be compatible with the parameter types.
This is a common programming approach.
#include <iostream>
using namespace std;
void displaySquare(int num)
{
cout << "Square = " << num * num;
}
int main()
{
int number;
cout << "Enter a number: ";
cin >> number;
displaySquare(number);
return 0;
}
Sample output:
Enter a number: 5
Square = 25
Notice that:
main().displaySquare().A return value is the result that a function sends back to the part of the program that called it.
In simple words:
A function can perform a calculation and send its result back to the calling function.
Consider the addition of two numbers.
We can calculate the sum inside a function and return it to main().
Example:
int add(int a, int b)
{
return a + b;
}
Explanation:
int specifies that the function returns an integer.a and b are parameters.return sends the calculated result back.a + b is the value being returned.Calling the function:
int result = add(10, 20);
The returned value is stored in result.
How a function returns a value
Function call
Function calculation
Return value
Store in main()
int result = 30;
#include <iostream>
using namespace std;
int add(int a, int b)
{
return a + b;
}
int main()
{
int result;
result = add(10, 20);
cout << "Sum = " << result;
return 0;
}
Output:
Sum = 30
| Step | Statement | Explanation |
|---|---|---|
| 1 | main() |
Program starts |
| 2 | int result; |
Declares result variable |
| 3 | add(10, 20) |
Calls the function |
| 4 | a = 10 |
First parameter receives 10 |
| 5 | b = 20 |
Second parameter receives 20 |
| 6 | return a + b |
Returns 30 |
| 7 | result = 30 |
Stores returned value |
| 8 | cout |
Displays the result |
Students often confuse these two concepts.
cout |
return |
|---|---|
| Displays information on the screen | Sends a value back to the caller |
| Used for output | Used to return a result |
| Does not automatically send a value to the caller | Transfers control back to the caller |
Can be used in a void function |
Used according to the function’s return type |
Compare:
Using cout:
void add(int a, int b)
{
cout << a + b;
}
Using return:
int add(int a, int b)
{
return a + b;
}
In the second example, we can use the returned result in another calculation:
int result = add(10, 20) * 2;
The value of result will be 60.
Functions can be classified into four types based on two characteristics:
Type 1
No parameters
No return value
void display()
Type 2
Parameters
No return value
void display(int a)
Type 3
No parameters
Return value
int getNumber()
Type 4
Parameters
Return value
int add(int a, int b)
The function neither receives information nor returns a result.
#include <iostream>
using namespace std;
void display()
{
cout << "Welcome to C++!";
}
int main()
{
display();
return 0;
}
Explanation:
void means no return value.The function receives information but does not return a result.
#include <iostream>
using namespace std;
void add(int a, int b)
{
cout << "Sum = " << a + b;
}
int main()
{
add(10, 20);
return 0;
}
Explanation:
cout.The function does not receive information but returns a result.
#include <iostream>
using namespace std;
int getNumber()
{
return 100;
}
int main()
{
int number = getNumber();
cout << number;
return 0;
}
Output:
100
Explanation:
100.number.The function receives information and returns a result.
#include <iostream>
using namespace std;
int multiply(int a, int b)
{
return a * b;
}
int main()
{
int result = multiply(5, 4);
cout << "Result = " << result;
return 0;
}
Output:
Result = 20
Explanation:
main().| Feature | Type 1 | Type 2 | Type 3 | Type 4 |
|---|---|---|---|---|
| Parameters | No | Yes | No | Yes |
| Return value | No | No | Yes | Yes |
| Return type | void | void | int (example) | int (example) |
| Example | display() |
add(a,b) |
getNumber() |
multiply(a,b) |
Teaching recommendation: Ask students to identify the type of function before writing its code. This helps them understand the concept instead of memorizing four examples.
Problem: Write a function that receives an integer and returns its square.
#include <iostream>
using namespace std;
int square(int num)
{
return num * num;
}
int main()
{
int number;
cout << "Enter a number: ";
cin >> number;
cout << "Square = " << square(number);
return 0;
}
Sample output:
Enter a number: 6
Square = 36
Problem: Write a function that receives an integer and returns true if it is even; otherwise, it returns false.
#include <iostream>
using namespace std;
bool isEven(int num)
{
return num % 2 == 0;
}
int main()
{
int number;
cout << "Enter a number: ";
cin >> number;
if (isEven(number))
cout << "Even Number";
else
cout << "Odd Number";
return 0;
}
Sample output:
Enter a number: 8
Even Number
Notice that the function return type is bool, because it returns either true or false.
#include <iostream>
using namespace std;
int findMaximum(int a, int b)
{
if (a > b)
return a;
else
return b;
}
int main()
{
int x, y;
cout << "Enter two numbers: ";
cin >> x >> y;
cout << "Maximum = " << findMaximum(x, y);
return 0;
}
Sample output:
Enter two numbers: 15 25
Maximum = 25
Consider:
int add(int a, int b)
{
return a + b;
}
int main()
{
int result = add(5, 7);
cout << result;
return 0;
}
Dry run table:
| Step | Operation | a | b | result |
|---|---|---|---|---|
| 1 | Function called | – | – | – |
| 2 | First argument assigned | 5 | – | – |
| 3 | Second argument assigned | 5 | 7 | – |
| 4 | Calculate sum | 5 | 7 | 12 |
| 5 | Return value | – | – | 12 |
| 6 | Store in result | – | – | 12 |
Output:
12
Ask students to make these modifications:
5, 7 to 10, 20.The following exercises are arranged from beginner to intermediate level.
Problem Statement 1: Display Student Name
Write a function named displayName() that receives a student’s name and displays it.
Tasks:
main().Requirements:
void return type.Problem Statement 2: Calculate Cube
Write a function named cube() that receives an integer and returns its cube.
Tasks:
Requirements:
Problem Statement 3: Calculate Average
Write a function named calculateAverage() that receives three numbers and returns their average.
Tasks:
Requirements:
double as the return type.Problem Statement 4: Find Minimum
Write a function named findMinimum() that receives two integers and returns the smaller value.
Tasks:
Requirements:
Problem Statement 5: Calculate Rectangle Area
Write a function named calculateArea() that receives the length and width of a rectangle and returns its area.
Tasks:
Requirements:
Problem Statement 6: Check Positive, Negative or Zero
Write a function named checkNumber() that receives an integer and displays whether it is positive, negative, or zero.
Tasks:
Requirements:
void return type.| Mistake | Explanation |
|---|---|
Using void when a value must be returned |
Use an appropriate return type |
Forgetting return |
A non-void function must return an appropriate value on every required execution path |
| Calling a function without required arguments | Supply the required arguments |
| Passing incompatible data types | Use compatible argument types |
| Confusing parameters with arguments | Parameters are declared in definitions; arguments are supplied in calls |
Assuming cout returns a value |
cout displays information; it does not replace return |
return statement?void and int return types?cout and return?Lecture 2 knowledge check
0/7 answered
Q1. In void display(int num), what is num?
A. Argument
B. Parameter
C. Return value
D. Function call
Q2. In add(10, 20), what are 10 and 20?
A. Parameters
B. Arguments
C. Return types
D. Function names
Q3. Which keyword sends a value back from a function?
A. cout
B. void
C. return
D. int
Q4. Which return type is appropriate for a function returning true or false?
A. int
B. void
C. bool
D. char
Q5. Which function type receives parameters and returns a value?
A. Type 1
B. Type 2
C. Type 3
D. Type 4
Q6. What is the output of return 5 + 3?
A. 5
B. 3
C. 8
D. 53
Q7. Which statement is correct about cout?
A. It returns a value to main
B. It displays information
C. It declares parameters
D. It defines a function
Submit answers
Students should remember:
| Concept | Key point |
|---|---|
| Parameter | Variable declared in function definition |
| Argument | Value supplied during function call |
| Return value | Result sent back to the caller |
return |
Sends the result back |
void |
Function does not return a value |
int |
Function returns an integer |
| Type 1 | No parameters, no return value |
| Type 2 | Parameters, no return value |
| Type 3 | No parameters, return value |
| Type 4 | Parameters and return value |