0% found this document useful (0 votes)
7 views5 pages

PF Functions Tasks

This file is a practice PF tasks for functions

Uploaded by

Mohsin Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views5 pages

PF Functions Tasks

This file is a practice PF tasks for functions

Uploaded by

Mohsin Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Lab 15: Advanced Function Concepts

Introduction
This lab focuses on expanding your understanding of functions in C++, introducing advanced
function concepts that enhance code modularity and flexibility. You'll explore various ways to
define and use functions, including function overloading, different parameter passing methods,
default arguments, and variable scoping.

In order to understand these concepts better, here is a simple example that implements each of
these concepts.

#include <iostream>
using namespace std;

// Function overloading example


int calculate(int a, int b) {
return a + b;
}

double calculate(double a, double b) {


return a * b;
}

// Pass by reference function


void swapValues(int &x, int &y) {
int temp = x;
x = y;
y = temp;
}

// Default argument function


void printMessage(string msg = "Hello World") {
cout << msg << endl;
}

int main() {
// Demonstrating different function concepts
cout << calculate(5, 3) << endl; // Integer addition
cout << calculate(5.5, 3.2) << endl; // Double multiplication

int a = 10, b = 20;


swapValues(a, b);
printMessage(); // Default message
printMessage("Custom message");

return 0;
}

Sample Output of the code:

Lab Tasks
Task 1: Function Overloading

Create a program that demonstrates function overloading for calculating the area of different
shapes.

Requirements:

● Implement area calculation functions:


○ Rectangles (length and width) - [formula: l * w]: calculateArea(double
length, double width)
○ Circles (radius) - [formula: pi * r * r]: calculateArea(double radius)
○ Triangles (base and height) - [formula: ½ * base * height]:
calculateArea(double base, double height, bool isTriangle) -
you can pass the boolean value as true always
● Use function overloading to handle different parameter types
● Display the calculated areas with appropriate labels

Sample Input/Output:

Rectangle Area (5.0, 3.0): 15.00


Circle Area (4.0): 50.27
Triangle Area (6.0, 4.0): 12.00

Task 2: Arrays in Functions

Write a program that demonstrates passing arrays to functions for basic statistical calculations.
Requirements:

● Create functions to:


○ Calculate the average of an integer array: calculateAverage(int arr[],
int size)
○ Find the maximum value in the array: findMaximum(int arr[], int size)
○ Count even numbers in the array: countEvenNumbers(int arr[], int
size)
● The main function should populate the array and call these functions
● Array size should be defined as a constant

Array: 10, 25, 30, 15, 40, 20


Average: 23.33
Maximum Value: 40
Even Number Count: 4

Task 3: Pass by Value vs Pass by Reference

Develop a program to illustrate the difference between pass by value and pass by reference.

Requirements:

● Create two functions that swap values:


○ swapByValue(int x, int y) using pass by value
○ swapByReference(int &x, int &y) using pass by reference
● Demonstrate the different behaviors in the main function
● Use integer variables

Original Values:
x = 5, y = 10

After Pass by Value Swap:


x = 5, y = 10

After Pass by Reference Swap:


x = 10, y = 5

Task 4: Default Arguments

Create a program that calculates the volume of different 3D shapes using default arguments.

Requirements:
● Implement a volume calculation function: calculateVolume(double side,
double width = -1, double height = -1)
○ Cube (side length)
○ Rectangular prism (length, width, height)
● Use default arguments for some parameters
● Demonstrate calling the function with different numbers of arguments

Cube Volume (5): 125.00


Rectangular Prism Volume (4, 3, 2): 24.00
Rectangular Prism Volume (4, 3): 12.00
Cube Volume (4): 64.00

Task 5: Variable Scope

Write a program demonstrating different variable scopes (local, static, and global) and const
variables.

Requirements:

Declare variables of different scopes:

● Declare a global variable


● Declare a global const variable
● Create function demonstrateScope() that:
○ Uses a local variable
○ Uses a local const variable
○ Demonstrates a static local variable
○ Modifies the global variable
● Show the behavior of these variables across multiple function calls

Demonstrate const behavior:

● Show that const variables cannot be modified (comment out attempt and mention the
error you receive in the comment)
● Demonstrate const with different scopes (global vs local)
● Include at least one function with const parameters
● Show proper use of const return values

Global Variable: 10
Global Const: 100

After First Call:


Local Variable: 5
Local Const: 55
Static Variable: 1
Global Variable: 15
Global Const: 100 (unchanged)

After Second Call:


Local Variable: 5
Local Const: 55
Static Variable: 2
Global Variable: 20
Global Const: 100 (unchanged)

Const Function Test:


Original Value: 42
Returned Const Value: 42

Note:
Your code should:

● Include proper input validation


● Use meaningful variable names
● Be properly indented
● Include necessary comments (including your name, roll number and section at the start
of each file)
● Handle invalid inputs gracefully
● Display clear prompts for all user inputs where applicable (e.g., "Enter the number of
dumplings Po wants to eat (or 0 to finish): ")
● Show appropriate messages for all operations (e.g., "Calculating total sales...")
● Present results clearly with proper labels and formatting
● Follow the given sample output

Submission instructions:

● Rename each .cpp file to i24xxxx_tx (e.g i24000_t1 for task 1)


● Upload .cpp files only
● Include your name, roll number and section as comments at the start of each task file

You might also like