Professional Documents
Culture Documents
USER-DEFINED FUNCTIONS I
In this chapter, you will:
• Learn about standard (predefined) functions and discover
how to use them in a program
• Learn about user-defined functions
• Examine value-returning functions, including actual and
formal parameters
• Explore how to construct and use a value-returning, user-
defined function in a program
Functions are like building blocks. They allow complicated programs
to be divided into manageable pieces.
Some of the advantages of functions are:
1. While working on one function, you can focus on just that part of
the program and construct it, debug it, and perfect it.
2. Different people can work on different functions simultaneously.
3. 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.
Function in C++
• Pre-defined (standard functions).
• User-defined functions.
Pre-defined Functions
Some of the pre-defined mathematical functions are abs(x),
sqrt(x), and pow(x,y).
The power function, pow(x,y), calculates xy; that is, the value of
pow(x,y) = xy.
pow(2,3) = 8.0 and pow(2.5,3) = 15.625.
The function pow is of the type double or that the function pow
returns a value of the type double.
x and y are called the parameters (or arguments) of the function
pow.
Function pow has two parameters.
The square root function, sqrt(x), calculates the non-negative
square root of x for x >= 0.0.
sqrt(2.25) is 1.5.
The function sqrt is of the type double and has only one
parameter.
The floor function, floor, calculates the largest whole number that
is not greater than x.
floor(48.79) is 48.0.
The function floor is of the type double and has only one
parameter.
In C++, pre-defined functions are organized into separate libraries.
I/O functions are contained in the header file iostream.
Math functions are contained in the header file cmath.
Example 6-1
// How to use predefined functions.
#include <iostream>
#include <cmath>
#include <cctype>
#include <cstdlib>
using namespace std;
int main()
{
int x;
double u,v;
cout<<"Line 1: Uppercase a is "
<<static_cast<char>(toupper('a'))
<<endl; //Line 1
u = 4.2; //Line 2
v = 3.0; //Line 3
cout<<"Line 4: "<<u<<" to the power of "
<<v<<" = "<<pow(u,v)<<endl; //Line 4
cout<<"Line 5: 5 to the power of 4 = "
<<pow(5,4)<<endl; //Line 5
u = u + pow(3,3); //Line 6
cout<<"Line 7: u = "<<u<<endl; //Line 7
x = -15; //Line 8
cout<<"Line 9: Absolute value of "<<x
<<" = "<<abs(x)<<endl; //Line 9
return 0;
}
Output:
Line 1: Uppercase a is A
Line 4: 4.2 to the power of 3 = 74.088
Line 5: 5 to the power of 4 = 625
Line 7: u = 31.2
Line 9: Absolute value of -15 = 15
USER-DEFINED FUNCTIONS
double u = 2.5;
double v = 3.0;
double x, y, w;
x = pow(u, v); //Line 1
y = pow(2.0,3.2); //Line 2
w = pow(u,7); //Line 3
• In Line 1, the function pow is called with the parameters u and v.
• The values of u and v are passed to the function pow.
• The value of u is copied into base and the value of v is copied into
exponent.
• The variables u and v that appear in the call to the function pow in
Line 1 are called actual parameters of that call.
• In Line 2, the function pow is called with parameters 2.0 and 3.2.
• The value 2.0 is copied into base and 3.2 is copied into
exponent. In this call of the function pow, the actual parameters
are 2.0 and 3.2, respectively.
• In Line 3, the actual parameters of the function pow are u and 7, the
value of u is copied into base, and 7.0 is copied into exponent.
• Formal Parameter: A variable declared in the function
heading.
• Actual Parameter: A variable or expression listed in a call
to a function.
Syntax: Value-Returning Function
functionType functionName()
or
functionType functionName(void)
• If the formal parameter list is empty, in a function call the actual
parameter is also empty.
• A call to a value-returning function with an empty formal parameter
list is
functionName()
To call a value-returning function:
1. Use its name, with the actual parameters (if any) in parentheses.
2. The number of actual parameters together with their data types
must match with the formal parameters in the order given. That
is, there is a one-to-one correspondence between actual and
formal parameters.
if(x >= y)
max = x;
else
max = y;
return max;
}
double larger(double x, double y)
{
if(x >= y)
return x;
else
return y;
}
The first form of the function larger requires that you use an
additional variable max (called a local declaration, where max is a
variable local to the function larger); the second form does not.
x and y are formal parameters.
int main()
{
double one, two, maxNum; //Line 1
return 0;
}
1. The expression larger(5,6), at Line 2, is a function call, and 5
and 6 are actual parameters.
2. The expression larger(one, two), at Line 6, is a function call.
Here, one and two are actual parameters.
3. The expression larger(one, 29), at Line 7, is also a function
call. Here, one and 29 are actual parameters.
4. The expression larger(38.45, 56.78); at Line 8 is a function
call. In this call, the actual parameters are 38.45 and 56.78. In this
statement, the value returned by the function larger is assigned to
the variable maxNum.
Once a function is written it can be used anywhere in the program.
Example:
For the function larger, the function prototype is:
int main()
{
double one, two; //Line 1
#include <iostream>
using namespace std;
int funcRet1();
int funcRet2();
int funcRet3();
int funcRet4(int z);
int main()
{
int num = 4;
cout<<"Line 1: Value returned by funcRet1: "
<<funcRet1()<<endl; // Line 1
cout<<"Line 2: Value returned by funcRet2: "
<<funcRet2()<<endl; // Line 2
cout<<"Line 3: Value returned by funcRet3: "
<<funcRet3()<<endl; // Line 3
cout<<"Line 4: Value returned by funcRet4: "
<<funcRet4(num)<<endl; // Line 4
return 0;
}
int funcRet1()
{
return 23, 45; //Only 45 is returned
}
int funcRet2()
{
int x = 5;
int y = 6;
int funcRet3()
{
int x = 5;
int y = 6;
Output:
Line 1: Value returned by funcRet1: 45
Line 2: Value returned by funcRet2: 6
Line 3: Value returned by funcRet3: 10
Line 4: Value returned by funcRet4: 7
Example 6-2: Palindrome Number
• In this example, a function, isNumPalindrome, is designed that
returns true if an integer is a palindrome and false otherwise.
• An integer is a palindrome if it reads forward and backward in the
same way.
• The integers 5, 44, 434, 1881, and 789656987 are all
palindromes.
• Suppose num is an integer. If num < 10, it is a palindrome.
• Suppose num >= 10.
• To determine whether num is a palindrome, first compare the first and
the last digits of num.
• If the first and the last digits of num are not the same, it is not a
palindrome.
• If the first and the last digits of num are the same, remove the first and
the last digits of num and repeat this process on the new number
which is obtained from num after removing the first and the last digits
of num.
• Repeat this process as long as the number is >= 10.
• Suppose the input is 18281.
• The first and last digits of 18281 are the same, remove the first and
last digits to get the number 828.
• Repeat this process of comparing the first and last digits on 828.
Once again, the first and last digits are the same.
• After removing the first and last digits of 828, the resulting number is
2, which is less than 10.Thus, 18281 is a palindrome.
• To remove the first and last digits of num, you first need to find the
highest power of 10 that divides num, call it pwr.
• The highest power of 10 that divides 18281 is 4, that is, pwr = 4.
• Now 18281 % 10pwr = 8281, and so the first digit is removed.
• Since 8281 / 10 = 828, the last digit is removed.
• Decrement pwr by 2 for the next iteration.
1. If num < 10, it is a palindrome and so the function should return
true.
2. Suppose num is an integer and num >= 10. To see if num is a
palindrome,
a. Find the highest power of 10 that divides num and call it pwr.
For example, the highest power of 10 that divides 434 is 2; the
highest power of 10 that divides 789656987 is 8.
b. While num is greater than or equal to 10, compare the first and
last digit of num.
b.1. If the first and last digits of num are not the same, num is not
a palindrome. Return false.
b.2. If the first and the last digits of num are the same,
b.2.1. Remove the first and last digits of num.
b.2.2. Decrement pwr by 2.
3. Return true.
bool isNumPalindrome(int num)
{
int pwr = 0;
}
}//end while
return true;
}//end else
Flow of Execution
When the program is executed (that is, run) execution always begins
at the first statement in the function main no matter where it is
placed in the program.
Other functions are executed only when they are called.
Function prototypes appear before any function definition, so the
compiler translates these first. The compiler can then correctly
translate a function call.
A function call statement results in the transfer of control to the first
statement in the body of the called function.
After the last statement of the called function is executed, the
control is passed back to the point immediately following the
function call.
A value-returning function returns a value. Therefore, for value-
returning functions, after executing the function when the control
goes back to the caller, the value that the function returns replaces
the function call statement.
PROGRAMMING EXAMPLE: LARGEST NUMBER
cout<<fixed<<showpoint; //Step 1
cout<<setprecision(2); //Step 2
switch(customerType) //Step 7
{
case 'r': //Step 7a
case 'R': amountDue = residential(); //Step 7a.i
cout<<"Account number = "
<<accountNumber<<endl; //Step 7a.ii
cout<<"Amount due = $"
<<amountDue<<endl; //Step 7a.ii
break;
case 'b': //Step 7b
case 'B': amountDue = business(); //Step 7b.i
cout<<"Account number = "
<<accountNumber<<endl; //Step 7b.ii
cout<<"Amount due = $"
<<amountDue<<endl; //Step 7b.ii
break;
default: cout<<"Invalid customer type."
<<endl; //Step 7c
}
return 0;
}
double residential ()
{
int noOfPChannels;
double bAmount;