lab 6: functions

advertisement
LAB 6: FUNCTIONS
A function is a basic programming unit of a C++ programming. Every C++ program
should have at least one function called main. Student will get to know about function
declarations (prototype), function definitions and function calls. They will also take a
look at scope of variables.
LAB SHEET 6.1: Predefine Function
ESTIMATED TIME
: 1 hour
OBJECTIVE
: Identify Predefine Function
SOFTWARE
: Internet Access and Microsoft Visual Studio 6 ( C++)
PROCEDURE
CONCLUSION
:
:
Read all the questions and write the program. Then run the
program and get the correct output.
Upon completing the task, student should understand the
functions for each header file.
DISCUSSION / RESULT / EXERCISE
a) Research four (4) C++ header files. Give three (3) functions that defined
under each header. Complete the following tables:-
Header name 1:___________________
Header name 2:___________________
Functions
Functions
1. _________________________
1. _________________________
2. _________________________
2. _________________________
3. _________________________
3. _________________________
1
Header name 3:___________________
Header name 4:____________________
Functions
Functions
1. _________________________
1. _________________________
2. _________________________
2. _________________________
3. _________________________
3. _________________________
2
LAB SHEET 6.2: User Define Function
ESTIMATED TIME
:
5 hours
OBJECTIVE
:
Able to develop and apply user defines function. Differentiate
local and global variables.
SOFTWARE
:
Microsoft Visual Studio 6 ( C++)
PROCEDURE
:
Read all the questions and write the program. Then run the
program and get the correct output.
CONCLUSION
:
At the end of this exercise, student should be able to develop a
user define function in the program. They also able to
differentiate between local and global variables.
DISCUSSION / RESULT / EXERCISE
a) Write a program that inputs a series of integers and passes them one at a
time to function called even, which uses the modulus operator to determine
whether an integer is even. The function should take an integer argument and
return true if the integer is even and false otherwise.
Sample Output
Enter an integer: 4
4 is an even integer
Enter an integer: 3
3 is an odd integer
Enter an integer: 2
2 is an even integer
3
b) Write another function called odd that returns true if an integer is odd and
false otherwise.
c) Write a function divides that takes two integer parameters and returns true if
the first integer divides evenly into the second one (i.e., the second divided by
the first yields the remainder 0).
4
d) Write a function minimumValue that prints and returns the minimum of its five
integer parameters.
e) Write a function integerPower( base, exponent ) that returns the value of base
exponent For example, integerPower( 3, 4 ) = 3 * 3 * 3 * 3. Assume that
exponent is a positive, nonzero integer and that base is an integer. The
function integerPower should use for or while loop to control the calculation.
Do not use any math library functions.
Hints:
• Use a for loop to loop X times where X is the value of exponent.
• During each iteration of the loop, multiply variable product by the value of the base parameter.
• Return the value of product after X iterations of the for loop.
• Sample output:
Enter base and exponent: 5 2
5 to the power 2 is: 25
5
Download