You are on page 1of 5

Faculty of Computing

CS110: Fundamentals of Computer Programming

Class: BESE-14AB

Lab 07: User Defined Functions

CLO 2: Solve given real-world problem by applying appropriate programming


concepts and techniques.

CLO 3: Build a program and associated documentation using appropriate IDE


and supplementary tools.

Date: 31st October 2023

Time: 10am-1pm &2:00pm-5:00pm

Instructor: Dr. Momina Moetesum

Lab Engineer: Shakeela Bibi


Lab 07: User Defined Functions

Learning Objectives:

After completing this section, you will be able to


• Practice function prototyping, definition and call in C++ programming.
• Differentiate between calling a function by value and calling it by reference.
• Use functions with default parameters to solve a given problem.
• Practice using inline functions

Tools/Software Requirement:
Microsoft Visual Studio 2013 or above

Lab Tasks

Task 1:

Copy the code in the same order in your compiler. Call the given function in main and
display the output.

#include<iostream>
using namespace std;

//Function Definition
void area()
{ float area;
float radius;
cout<< “Enter the radius : ”;
cin>>radius;
area = 3.14 * radius * radius;
cout<<"Area of Circle = "<<area;
}

void main()
{
//call the above function in main
}

a) Rewrite the same program by moving the definition of the function area() after the
main program. What other change you have to provide in your code to give the same
output as before? Show the latest code of the program with changes highlighted by
comments.
b) Rewrite the program by taking the value of radius from user inside main() and passing
it by value into the function call. Similarly, the output should be returned to the main()
to be displayed. What changes you have to make to function prototype, definition, and
call in your code to give the same output as before? Show the latest code of the
program with changes highlighted by comments.
Task 2:

Pass-by-reference is a powerful feature in C++ that allows you to modify the original data
within a function rather than working with copies. Run the following code and display the
output. Now make necessary changes in the code so that variable number displays updated
value instead of the original value. (Hint: Use pass number by reference in the function)

#include <iostream>
// Function to calculate the square of a number using pass by reference
void calculateSquare(int);
int main() {
int number;
std::cout << "Enter a number: ";
std::cin >> number;
// Call the function with pass by value
calculateSquare(number);
std::cout << "The square of the number is: " << number << std::endl;
return 0;
}
void calculateSquare(int num) {
num = num * num;
}

Task 3:

A function with default parameters in C++ allows you to define default values for one or
more function parameters. This means that if a value is not provided for a parameter when
calling the function, it will use the default value defined in the function declaration. Write a
C++ program to compute the product of any number of integers between 2 and 5 using a
single function int multiply(int, int, int, int, int). The function should use default values as
parameters to allow function calls with different number of parameters. The program should
prompt the user to enter the number of integers to be multiplied and then use switch statement
to go to the respective call.

Task 4:

C++ provides inline functions to reduce the function call overhead. To verify this run the
following program in the compiler and note the execution time using <chrono>. Now make
the function cube() inline and record the execution time difference. Give output and execution
time for both scenarios. Also give code after including inlining.

#include <iostream>
#include <chrono>
void cube(int x){std::cout<<x*x*x<<std::endl;}
int main() {
// Start the clock
auto start = std::chrono::high_resolution_clock::now();

// Your program or code to be timed


cube(4);

// Stop the clock


auto stop = std::chrono::high_resolution_clock::now();

// Calculate the duration


auto duration = std::chrono::duration_cast<std::chrono::microseconds>(stop - start);

// Print the execution time in microseconds


std::cout << "Execution Time: " << duration.count() << " microseconds" << std::endl;

return 0;
}

Deliverables

Compile a single Word document with codes for each question and screenshots of the outputs
and submit this Word file on LMS.
Lab Rubrics

Your Lab 7 will be graded out of 5 for each rubric according to the following rubrics. Grades
for CLO3 will be shared at different intervals during the semester.

Lab Rubrics for Lab 5(Conditional Statements – Decision Making)

Sr. Assessment Unacceptable Does Not Meet Meets Expectations Exceeds Expectations
No. (0 Marks) Expectations (3/4 Marks) (5 Marks)
(1/2 Marks)
1 Application of The student is unable to The student requires some The student demonstrates a
Programming apply the appropriate guidance to apply the clear ability to apply the
Concepts programming concepts to appropriate programming appropriate
solve the given problem concepts to solve the given programming concepts to
(CLO2, PLO3) solve the given problem.
thus resulting in an problem.
incomplete or ineffective
The program flow requires The program flow is
solution. adequate.
minor improvements.
The program flow is
messy and Codes are semi-modular and Codes are modular,
semi-reusable. reusable, and easily
incomprehensible. readable.
Codes are non-modular
and cannot be reused.
2 Software Tool The student demonstrates The student demonstrates The student demonstrates a
Usage a lack of understanding of some understanding of tool good understanding of tool
The student did not tool usage. usage. usage.
(CLO3-PLO5) submit any work.
Implementation has The codes are correct in Furthermore, his/her
OR syntax/semantic/runtime terms of their syntax, coding is complete and
however, the program functional, and the program
errors, and the student is output is correct in all test
The student unable to debug and output is not always correct
in all test cases. cases.
plagiarized the correct the errors.
solution and/or used The code has limited The code has sufficient
unfair means. The code has inadequate comments and inconsistent comments and consistent
comments and variable variable names and may not variable names and
names and does not adhere to the coding reasonably adhere to the
adhere to the coding standards. coding standards.
standards.
Some Error handling has been Adequate Error handling has
No Error handling has performed. been performed.
been performed.
Documentation is adequately Documentation is well
Documentation is poorly structured. structured.
structured.

You might also like