Learn with Yasir

Share Your Feedback

CS-303: Programming Fundamentals


CS-303: Programming Fundamentals

Lecture 3 – Function Prototypes, Variable Scope and Parameter Passing

Student Notes

Beginner to Intermediate

Theory + Practical

1. Learning Objectives

After completing this lecture, students will be able to:

  1. Understand function declaration (prototype).
  2. Differentiate between function declaration and definition.
  3. Understand the importance of function prototypes.
  4. Explain local and global variables.
  5. Understand variable scope and lifetime.
  6. Differentiate between pass by value and pass by reference.
  7. Write C++ programs using different parameter-passing techniques.

2. Recap of Previous Lecture

In Lecture 2, we learned:

  • Function parameters and arguments.
  • How to pass values to functions.
  • How to return values from functions.
  • The four types of functions in C++.
  • The difference between 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.

Part 1: Function Prototypes

3. What is a Function Prototype?

A function prototype is a declaration that informs the compiler about a function before it is called.

It tells the compiler:

  • Function name.
  • Return type.
  • Number of parameters.
  • Data types of parameters.

In simple words:

A function prototype tells the compiler that a function exists and describes how it can be called.

Example 1: Function without a prototype

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.

4. How to Declare 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);

5. Complete Program Using a Function Prototype

#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

How does it work?

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

6. Function Declaration vs Function Definition vs Function Call

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:

  • A prototype normally ends with a semicolon.
  • A function definition contains a body enclosed in braces.
  • A function call uses the function name with arguments.
  • A function prototype is not an executable function.

Part 2: Variable Scope

7. What is Variable Scope?

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.

8. Local Variables

A local variable is declared inside a function or block.

It can only be accessed within its scope.

Example 2: Local variable

#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().
  • It can be accessed inside display().
  • It cannot be accessed directly inside main().

Example 3: Accessing a local variable outside its scope

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

9. Global Variables

A global variable is declared outside all functions.

It can be accessed by functions in its scope, subject to normal declaration and visibility rules.

Example 4: Global variable

#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.
  • It is a global variable.
  • Both display() and main() can access it.

10. Difference Between Local and Global Variables

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

11. Understanding Variable Scope with an Example

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

12. Variable Lifetime

Scope and lifetime are related but different concepts.

  • Scope: Where a variable can be accessed.
  • Lifetime: How long a variable exists in memory during program execution.

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.

Part 3: Parameter Passing

13. What is Parameter Passing?

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:

  1. Pass by value.
  2. Pass by reference.

14. Pass by Value

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.

Example 5: Pass by value

#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

Explanation

  1. number is initialized with 10.
  2. changeValue(number) calls the function.
  3. A copy of number is passed to num.
  4. num is changed to 100.
  5. The original variable number remains 10.

Pass by value – Memory concept

main()

10

number

Original

changeValue()

100

num

Copy changed

The two variables are separate. Changing the copy does not change the original.

15. Pass by Reference

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.

Example 6: Pass by reference

#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

Explanation

  1. number is initialized with 10.
  2. changeValue(number) calls the function.
  3. The parameter num refers to the original variable number.
  4. num is changed to 100.
  5. The original variable number also becomes 100.

Pass by reference – Memory concept

100

Original variable: number

Same variable is accessed through another name.

Function parameter: num

Same variable

Both names refer to the same object in memory.

16. Difference Between Pass by Value and Pass by Reference

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

17. Practical Example: Swapping Two Numbers

Swapping means exchanging the values of two variables.

For example:

Before swapping:

a = 10
b = 20

After swapping:

a = 20
b = 10

Example 7: Swapping using pass by value

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

Example 8: Swapping using pass by reference

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

Part 4: Dry Run Practice

18. Dry Run: Pass by Value

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

19. Dry Run: Pass by Reference

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

Part 5: Programming Exercises

Try these exercises to strengthen your understanding.

Beginner Level

Problem Statement 1: Function Prototype

Write a C++ program that calculates the square of a number using a function prototype.

Tasks:

  1. Declare a function named square().
  2. Define the function after main().
  3. Accept an integer from the user.
  4. Return and display its square.

Requirements:

  • Use an integer return type.
  • Use one integer parameter.
  • Use a function prototype.

Problem Statement 2: Local and Global Variables

Write a C++ program that demonstrates the use of local and global variables.

Tasks:

  1. Declare a global variable named number.
  2. Create a function named display().
  3. Declare a local variable inside display().
  4. Display both variables where they are accessible.

Requirements:

  • Use one global variable.
  • Use one local variable.

Problem Statement 3: Pass by Value

Write a function named increase() that receives an integer and increases its value by 10.

Tasks:

  1. Declare an integer variable in main().
  2. Pass it to increase().
  3. Modify the parameter inside the function.
  4. Display the original variable after the function call.

Requirements:

  • Use pass by value.
  • Use void return type.

Intermediate Level

Problem Statement 4: Pass by Reference

Write a function named increase() that receives an integer by reference and increases its value by 10.

Tasks:

  1. Declare an integer variable in main().
  2. Pass it to increase().
  3. Modify its value inside the function.
  4. Display the updated variable.

Requirements:

  • Use pass by reference.
  • Use the reference operator &.

Problem Statement 5: Swap Two Numbers

Write a function named swapNumbers() that exchanges the values of two integer variables.

Tasks:

  1. Input two integers.
  2. Pass both variables to the function.
  3. Exchange their values.
  4. Display the updated values in main().

Requirements:

  • Use pass by reference.
  • Use a temporary variable.

Problem Statement 6: Calculate Total Marks

Write a function named calculateTotal() that receives marks of three subjects and returns their total.

Tasks:

  1. Input marks of three subjects.
  2. Pass them to the function.
  3. Calculate the total.
  4. Display the returned result.

Requirements:

  • Use three integer parameters.
  • Use an integer return type.
  • Declare a function prototype.

Advanced Level

Problem Statement 7: Update Student Marks

Write a function named updateMarks() that receives marks by reference and increases them by 5.

Tasks:

  1. Input student marks.
  2. Pass the marks to the function.
  3. Update the marks inside the function.
  4. Display the updated marks in main().

Requirements:

  • Use pass by reference.
  • Use a void function.

Problem Statement 8: Compare Pass by Value and Pass by Reference

Develop a program with two separate functions:

  • changeByValue()
  • changeByReference()

Tasks:

  1. Declare an integer variable in main().
  2. Pass it to both functions.
  3. Modify the parameter in each function.
  4. Display the variable after each function call.
  5. Compare the results.

Requirements:

  • Use both parameter-passing techniques.
  • Explain why the results are different.

Part 6: Common Mistakes

20. Errors to Avoid

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

Part 7: Assessment

21. Short Questions

  1. What is a function prototype?
  2. Why do we use function prototypes?
  3. Differentiate between declaration and definition.
  4. What is variable scope?
  5. What is a local variable?
  6. What is a global variable?
  7. Differentiate between scope and lifetime.
  8. What is parameter passing?
  9. Explain pass by value.
  10. Explain pass by reference.
  11. What is the purpose of the & operator in a reference parameter?
  12. Why does pass by value not modify the original variable?
  13. What is the difference between pass by value and pass by reference?

22. Interactive Quiz

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

23. Lecture Summary

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

24. Preparation for Lecture 4

Next Lecture: Advanced Functions in C++

Topics:

  1. Default arguments.
  2. Function overloading.
  3. Function overloading with different data types.
  4. Function overloading with different numbers of parameters.
  5. Introduction to recursion.
  6. Recursive function execution.
  7. Factorial using recursion.
  8. Practical programming problems.

Important for students

Before moving to the next lecture, make sure you can:

  • Write a function prototype.
  • Define and call a function.
  • Identify local and global variables.
  • Explain pass by value.
  • Explain pass by reference.
  • Write a program to swap two numbers using pass by reference.

Practice is essential: Run all examples in a C++ compiler and observe how the output changes when you modify the parameter-passing method.