Laboratory6 - Functions
Laboratory6 - Functions
EDP 101
Computer Programming Fundamentals
Laboratory
Programmer-Defined Function
Return type
Student Name
(LN, FN MI)
Subject Teacher
Date Submitted
Score
Laboratory Exercise # 6
PROGRAM-DEFINED FUNCTIONS
Objective:
At the conclusion of this laboratory exercise, the student should be able to:
Materials:
1 computer set
C ++ IDE
Introduction:
A function is a block of code that performs a specific task. Every C++ program
contains at least one function, which is named main. However, most C++ programs
contain many functions. Some of this functions are built into the C++ language. If
for example the function pow (base, exp) and sqrt() are to be used, you must
include the header file that contains the function’s specification via the include
statement, which is in this case #include<cmath>.
In the previous exercises, you use only the function main(); the programming
instructions are packed into one function. This technique, however, is good only for
short programs. For large programs, it is not practical to put the entire
programming instructions into one function. You must learn to break the problem
into manageable pieces.
Other program functions, like main(), are created by the programmer. These
functions are often referred to as program-defined functions because the function
definitions are contained in the program itself rather than in a different file. But
why would a programmer need more than the main function? One reason is to
avoid duplication of code. If the same task needs to be performed in more than one
section of a program, it is more efficient for the programmer to enter the code
once, in a function. Any section in the program can then call the function to perform
the required task.
It allows you to focus on just that part of the program and construct it.
Different people can work on different functions simultaneously.
If a function is needed in more than one place in a program or in different
programs, you can write it once and use it many times.
Using functions greatly enhances the program’s readability because it
reduces the complexity of the function main.
if (x>=y)
Function max = x;
body
else
max = y;
return max; Function return value
double a = 13.00;
double b = 36.53;
cout <<” The larger of “ << a << “ and “ << b
<<” is “ << larger(a, b) << endl;
The expression larger(a, b) is a function call. Here, a and b are the actual
parameters. When the expression larger(a, b) executes, the value of a is copied
into x, and the value of b is copied into y. Therefore the statement larger(a, b)
outputs the larger of a and b.
Encode the program below and observe how the function larger works.
#include <iostream>
using namespace std;
double larger(double x, double y); // function declaration
int main()
{
double num; //variable to hold the current number
double max; //variable to hold the larger number
int count; // loop control variable
cout << “Enter 10 numbers.” << endl;
cin >> num;
max = num;
return 0;
} // end main
statements;
When you attach & after the datatype in the formal parameter list of a
function, the variable following that datatype becomes a reference parameter.
When a function is called, the value of the actual parameter is copied to the formal
parameter. If the formal parameter is a value parameter, then after copying the
value of the actual to the formal parameter, there is no connection between the
formal and actual parameters because the formal parameter has now its own copy
of the data. Therefore, at run time, the formal parameter manipulates the data
stored in its own memory space.
Encode the following program below and observe how a value parameter works
(pass by value).
#include<iostream>
using namespace std;
int main()
{
int number = 6;
cout << “Before calling the function fValueParameter, number = “
<< number << endl;
fValueParameter(int num);
cout << “ After calling the function fValueParameter, number = “
<< number << endl;
return 0;
}
Encode the program below and observe how passing variables by reference works
#include<iostream>
using namespace std;
void getAge(int &inYears);
void displayAge(int years);
int main()
{
int age = 0;
getAge(age);
displayAge(age);
return 0;
}
Remember that when you pass a variable by a value, the computer uses the
data type and name of its corresponding formal parameter to create a separate
memory location in which to store the value. When you pass a variable by
reference, on the other hand, the computer locates the variable in memory and
then assigns the name of its corresponding formal parameter to the memory
location. When you pass a variable by reference, the variable will have two names:
one assigned by the calling function and the other assigned by the receiving
function. Value-returning functions, on the other hand, send information back to
the calling function through their return value.
Code the IPO chart shown below. Display the Celsius temperature in fixed-point
notation with no decimal places.
Algorithm:
1. Call getFahrenheit
function to get the
Fahrenheit temperature
2. Call the calcCelsius to
calculate temperature;
pass the Fahrenheit
temperature
3. Display the Celsius
temperature
getFahrenheit function
Algorithm:
1. Enter the Fahrenheit
temperature
2. Return the Fahrenheit
temperature
calcCelsius function
Algorithm:
1. Celsius temperature =
5.0 / 9.0 * (Fahrenheit
temperature – 32.0)
2. Return the Celsius
temperature
Modify the code from Laboratory Task 6-A. Change the getFahrenheit and
calcCelsius functions to void functions.
Create a program that calculates the average of three test scores. The
program should contain two value-returning programs (main and calcAverage) and
two void functions (getTestScores and displayAverage). The main function should
call the void getTestScores function to get three scores. The test scores may
contain a decimal place. The main function then should call the value-returning
calcAverage function to calculate and return the average of the three test scores
on the screen. Display the average with one decimal place. Use a sentinel value to
end the program.
Create IPO charts for the problem. List the input, processing and output
items, as well as the algorithm, in a chart similar to the one shown in Laboratory
Task A. Code the algorithm into a program. Use file format <lastname_Lab6.cpp>
and upload to specified link at schoology.com.
Screen Output Source code is well Source code allows Source code does
(10%) organized and easy to the required screen not meet the
understand. output to be required screen
displayed correctly output to be
with 1-3 errors displayed correctly.
found.
(10) (6) (3)