100% found this document useful (1 vote)
149 views

6.1 Built in Function: Lab Manual # 6 Functions

The document discusses functions in C++. It provides examples of different types of functions including those with and without arguments and return values. It also provides the syntax for defining functions. The remainder of the document consists of coding questions that involve writing functions to perform various tasks like calculating squares, averages, the largest of two numbers, checking for leap years, and more. Functions overloading and recursion are also briefly discussed.

Uploaded by

Videos4u iK
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
149 views

6.1 Built in Function: Lab Manual # 6 Functions

The document discusses functions in C++. It provides examples of different types of functions including those with and without arguments and return values. It also provides the syntax for defining functions. The remainder of the document consists of coding questions that involve writing functions to perform various tasks like calculating squares, averages, the largest of two numbers, checking for leap years, and more. Functions overloading and recursion are also briefly discussed.

Uploaded by

Videos4u iK
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Lab Manual # 6 Functions

6.1 Built In Function


#include <cmath> // defines the sqrt()
function #include <iostream.h>

int main()
{ // tests the sqrt() function: for
(int x=0; x <=9; x++)
cout << "\t" << x << "\t" << sqrt(x) << endl;
}

6.1.1 Out Put

6.2 Write types of Function


Function with no arguments and no return value

Function with no argument but return value

Function with argument but no return value

Function with argument with return value

6.3 Write Syntax Of function (Prototype, call & body of function)


Prototype:- returntype FunctionName(arguments(s));

Body:- returntype FunctionName(arguments(s))


{ statement;
}
Call:- FunctionName (arguments(s));
6.4 Que No 1 square () functioin
Write a program in C++ that has a function of square (). The user should
enter the number and the code should display the square of the given
number.

Code

6.5 Que No 2 Finding average using function


Get three integers numbers from user, Pass them to function, Add them and find
the average in function body, Display the Average in function body

6.5.1 Code

6.6 Que No 3
Write a code that take numbers from user and displays its cube. The
Code should reads integers and prints their cubes until the user inputs the
sentinel value 0. Each integer read should be passed to the cube()
function by the call cube(n). The value returned by the function should
replaces the expression cube(n) and then should be passed to the output
object cout.

6.6.1 Code
6.7 Que No 4 finding area of rectangle
Write a function that finds the area of the rectangle on providing length and
width. Get Length & width from user in main() Call the function area()
Calculate the length and return the area Display the result in main()

6.7.1 Code

6.8 Que No 5 Check date program


Get month ,day & year from user in main() Call the function printDate(int, int, int),
Put a check in printDate() using if statement (month < 1 || month > 12 || day < 1 ||
day > 31 || year < 0) & if it violates the rule display ―Must Enter a Valid Date”,
Using switch Statement get the month, Day and year are displayed normally,
Termination should be on entering 0 in months
Code

6.9 Que No 6 Leap Year program


A leap year is a year in which one extra day (February 29) is added to the regular
calendar. Most of us know that the leap years are the years that are divisible by 4. For
example, 1992 and 1996 are leap years. Most people, however, do not know that there
is an exception to this rule: centennial years are not leap years. For example, 1800
and 1900 are not leap years. Furthermore, there is an exception to the exception:
centennial years which are divisible by 400 are leap years. Thus, the year 2000 is a
leap year.
  
Make a program that full fills the above criteria using functions
 
The program should terminate on entering 0

The return type should be Boolean i.e bool isLeapYear(int);
6.9.1 code
6.10 Que No 7 Finding largest using if else in function
Write a program in C++ that take two numbers from user and find the
largest among two using function.

The Conditions are if
(a==b)
"A, B are the same“ a = b, (values)
else if (a < b)
"A & B are not same “ a != b (values)
" A is less than B “ a < b (values)
else
"A is Greater than B “ a > b (values)

6.10.1Code

6.11 QueNo 8
Develop a program in C++ that has function printTempOpinion() which prints "Cold"
on if the temperature is below 10, "OK" if the temperature is in the range 20 ->
30,"Hot" if the temperature is above 30.

Code
6.13 Que No 9 Swap by using function
Develop a program that swap the two values using function by reference
i.e After swap
a = 22.2 a = 44.4
b = 44.4 b = 22.2

6.13.1 Code

6.14 Provide an Example of Functions Overloading


6.15 Que 10 Find Factorial from -1 to 10 using function

You might also like