Learn with Yasir

Share Your Feedback

CS-303: Programming Fundamentals


CS-303: Programming Fundamentals

Lecture 2 – Function Parameters and Return Values in C++

Beginner level

Theory + Practical

60–75 minutes

1. Learning objectives

After completing this lecture, students will be able to:

  1. Understand function parameters and arguments.
  2. Pass values to a function.
  3. Understand the purpose of the return statement.
  4. Return values from functions.
  5. Differentiate between parameters and arguments.
  6. Identify the four types of functions.
  7. Write C++ programs using parameters and return values.

2. Recap of Lecture 1

In the previous lecture, we learned:

  • A function is a reusable block of code.
  • A function performs a specific task.
  • Function definition contains the actual code.
  • Function call executes the function.
  • 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:

  • Welcome Ali!
  • Welcome Ahmed!
  • Welcome Sara!

Writing a separate function for every student is not a good approach.

We can solve this problem using function parameters.

Part 1: Function Parameters

3. What are 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.

Example 1: Function with one parameter

#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!

Understand the code

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

greet(“Ali”)

Argument: “Ali”

Function definition

greet(string name)

Parameter receives the value “Ali”

Function output
Welcome Ali!

4. Parameters vs Arguments

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:

  • Parameter = Variable in function definition.
  • Argument = Value in function call.

5. Function with multiple parameters

A function can receive more than one parameter.

Example 2: Add two numbers

#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

Understand the function call

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.

6. Taking input inside main() and passing it to a function

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:

  1. The user enters a number in main().
  2. The number is passed to displaySquare().
  3. The function calculates its square.
  4. The function displays the result.

Part 2: Return Values

7. What is a return value?

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().

8. Understanding the return statement

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

add(10, 20)

Function calculation

10 + 20 = 30

Return value

30

Store in main()
int result = 30;

9. Complete example: Function with parameters and return value

#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-by-step execution

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

10. Difference between cout and return

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.

Part 3: Four Types of Functions

11. Classification of functions

Functions can be classified into four types based on two characteristics:

  1. Whether a function receives parameters.
  2. Whether a function returns a value.

Four types of functions

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)

Type 1: No parameters and no return value

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:

  • No parameters are used.
  • void means no return value.
  • The function directly displays a message.

Type 2: Parameters and 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:

  • The function receives two integers.
  • It calculates their sum.
  • It displays the result using cout.
  • It does not return a value.

Type 3: No parameters but with return value

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:

  • No parameters are used.
  • The function returns the integer 100.
  • The returned value is stored in number.

Type 4: Parameters and return value

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:

  • The function receives two integers.
  • It multiplies them.
  • It returns the calculated result.
  • The result is displayed in main().

12. Comparison of four function types

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.

Part 4: Practical Examples

13. Example: Calculate the square of a number

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

14. Example: Check even or odd

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.

15. Example: Find the maximum of two numbers

#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

Part 5: Dry Run Practice

16. Dry run: Addition function

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

17. Modify the program

Ask students to make these modifications:

  1. Change the arguments from 5, 7 to 10, 20.
  2. Modify the function to subtract two numbers.
  3. Modify the function to multiply two numbers.
  4. Create a function that divides two numbers.
  5. Store and display the returned result.

Part 6: Programming Exercises

The following exercises are arranged from beginner to intermediate level.

18. Beginner exercises

Problem Statement 1: Display Student Name

Write a function named displayName() that receives a student’s name and displays it.

Tasks:

  1. Accept a student’s name in main().
  2. Pass the name to the function.
  3. Display the name inside the function.

Requirements:

  • Use void return type.
  • Use one string parameter.

Problem Statement 2: Calculate Cube

Write a function named cube() that receives an integer and returns its cube.

Tasks:

  1. Accept an integer from the user.
  2. Pass it to the function.
  3. Display the returned result.

Requirements:

  • Use one integer parameter.
  • Return an integer value.

Problem Statement 3: Calculate Average

Write a function named calculateAverage() that receives three numbers and returns their average.

Tasks:

  1. Input three numbers.
  2. Pass them to the function.
  3. Calculate their average.
  4. Display the returned result.

Requirements:

  • Use three parameters.
  • Use double as the return type.

19. Intermediate exercises

Problem Statement 4: Find Minimum

Write a function named findMinimum() that receives two integers and returns the smaller value.

Tasks:

  1. Input two integers.
  2. Pass both values to the function.
  3. Find the smaller number.
  4. Display the returned result.

Requirements:

  • Use two integer parameters.
  • Return an integer.

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:

  1. Input length and width.
  2. Pass both values to the function.
  3. Calculate the area.
  4. Display the returned result.

Requirements:

  • Use two parameters.
  • Return an integer or appropriate numeric type.

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:

  1. Input an integer.
  2. Pass it to the function.
  3. Check the number.
  4. Display the appropriate message.

Requirements:

  • Use void return type.
  • Use one integer parameter.

Part 7: Common Mistakes

20. Errors students should avoid

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

Part 8: Assessment

21. Short questions

  1. What are function parameters?
  2. What are function arguments?
  3. Differentiate between parameters and arguments.
  4. What is the purpose of the return statement?
  5. What is the difference between void and int return types?
  6. Explain the four types of functions.
  7. Can a function receive multiple parameters?
  8. Can a function return a Boolean value?
  9. What happens after a function returns a value?
  10. What is the difference between cout and return?

22. Interactive quiz

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

23. Lecture summary

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