Lab-08 ME-A
Lab-08 ME-A
LAB MANUAL – 08
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:
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.
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.
Syntax:
4
Example No. 01:
Code
Output
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.