0% found this document useful (0 votes)
11 views6 pages

Lab-08 ME-A

The lab manual focuses on teaching students about functions in C++, emphasizing user-defined and built-in functions to enhance code structure and reusability. Students will engage in hands-on exercises to create and utilize functions for various tasks, including temperature conversion and mathematical operations. By the end of the lab, students will be equipped to develop structured C++ programs using both types of functions effectively.

Uploaded by

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

Lab-08 ME-A

The lab manual focuses on teaching students about functions in C++, emphasizing user-defined and built-in functions to enhance code structure and reusability. Students will engage in hands-on exercises to create and utilize functions for various tasks, including temperature conversion and mathematical operations. By the end of the lab, students will be equipped to develop structured C++ programs using both types of functions effectively.

Uploaded by

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

DEPARTMENT OF COMPUTER & SOFTWARE ENGINEERING

COLLEGE OF E&ME, NUST, RAWALPINDI

ME-237 Computer Systems & Programming

LAB MANUAL – 08

Course Instructor: Dr. Sagheer Khan

Lab Instructor: Engr. Eman Fatima

Student Name: _______________________________________________

Degree/ Syndicate: ____________________________________________

1
LAB # 08: Functions in C++

Lab Objective:
The main objective of this lab is to help students understand and apply the concept of functions
in C++, focusing on both user-defined and built-in functions. By the end of this lab, students
should be able to define their own functions to improve code structure, readability, and
reusability. Additionally, students will gain experience in using built-in functions provided by
the language to perform common tasks efficiently. The lab encourages modular programming
and aims to strengthen students' problem-solving abilities by breaking down complex problems
into manageable function-based components.

Hardware/Software Tools:
 Hardware: Desktop/Computer
 Software Tool: MS Visual Studio 2013

Lab Description:
This lab provides students with a hands-on introduction to working with functions in C++.
Students will start by creating user-defined functions, learning how to declare, define, and call
them effectively. They will explore different types of functions, including those that return
values and those that do not, as well as understand how to pass arguments and manage variable
scope. The lab also introduces built-in functions to demonstrate how predefined functionalities
can be utilized to perform routine operations. Through practical exercises such as simple
calculations, menu-driven programs, and problem-solving tasks, students will gain a solid
understanding of how to use functions to organize code logically and efficiently. By the end of
the lab, students will be able to integrate both user-defined and built-in functions to develop
structured and maintainable C++ programs.

Functions:

A function in C++ is a block of code that performs a specific task. Instead of writing the same
code again and again, we can put it inside a function and use it whenever needed. Functions help
make our programs organized, reusable, and easy to understand. For example, imagine you are
making tea. You follow the same steps every time: boil water, add tea leaves, pour milk, and
serve. Instead of explaining all these steps each time, you can simply say "Make tea", that’s what
a function does in programming.

A function in C++ is a reusable block of code designed to perform a specific task. Functions
allow programmers to break down large programs into smaller, manageable parts, making the
code more organized, easy to debug, and reusable. Instead of writing the same code multiple
times, we can define a function once and call it whenever needed.

2
Types of functions:

There are two main types of functions:

1. Built-in Functions
2. User-defined Functions.

Built-in Functions:

Built-in functions are functions already defined in C++ libraries. These functions perform
common operations like mathematical calculations, input/output operations, string
manipulations, etc. Instead of writing these functions ourselves, we can simply call them. To use
mathematical functions, we need to include the cmath library.

Function Description Example


sqrt(x) Square root of x sqrt(25) = 5
pow(x,y) x raised to the power y pow(2, 3) = 8
abs(x) Absolute value of x abs(-10) = 10
ceil(x) Rounds up x to the nearest integer ceil(4.3) = 5
floor(x) Rounds down x to the nearest integer floor(4.9) = 4
round(x) Rounds x to the nearest integer round(4.5) = 5
sin(x), cos(x), Trigonometric functions sin(90)
tan(x)

Example No. 01:

Code

3
Output

User-defined Functions:
User-defined functions are functions created by the programmer to perform specific tasks. These
functions help in code reusability, modularity, and better organization. They help in modular
programming, allowing you to divide a complex program into manageable pieces. They can be
used multiple times in the same program or in different programs. They can return values or
perform actions without returning anything (void functions). Parameters can be passed to
functions for dynamic input.

 Function Declaration (Prototype) – Optional but recommended before main().


 Function Definition – The actual code block that performs a task.
 Function Call – Invoking the function when needed in the program.

Syntax:

Let’s understand the syntax step by step:


 returnType: The type of value the function returns (e.g., int, float, void). If the function
doesn’t return a value, we use void.
 functionName: The name of the function, chosen by the programmer (e.g PrintMessage
etc).
 parameters: Optional input values that the function uses (inside parentheses).
 { } (Function Body): Contains the actual code that gets executed when the function is
called.
 return value: If a function performs a calculation, it returns the result using return. Void
functions don’t need this.

4
Example No. 01:

Code

Output

Example No. 02:

Code

5
Output

Lab Tasks:

1. Write a C++ program that allows users to convert temperatures between Celsius,
Fahrenheit, and Kelvin. Implement three user-defined functions:
 celsiusToFahrenheit(double c) – Converts Celsius to Fahrenheit
 fahrenheitToCelsius(double f) – Converts Fahrenheit to Celsius
 kelvinToCelsius(double k) – Converts Kelvin to Celsius

The program should allow the user to choose a conversion type, input a temperature, and
display the converted value with appropriate formatting. Ensure that invalid inputs (e.g.,
negative values for Kelvin) are handled properly.

2. Write a C++ program that takes two floating-point numbers as input and performs
various mathematical operations using <cmath>. Implement a user-defined function
performCalculations() to compute and display min(), max(), sqrt(), pow(), abs(),
floor(), ceil() and round(). Additionally, calculate the sine, cosine, and tangent of the
first number (in radians). Ensure input validation to prevent errors (e.g., no square root
or logarithm of negative numbers) and display results with proper formatting.

3. Write a C++ program that finds the largest of three numbers using a user-defined
function. The program should take three numbers as input from the user, pass them to a
function named findLargest(), and return the largest number. The result should be
displayed with a clear message indicating which number is the greatest among the three.

You might also like