Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
34 views

Lecture 04

User-defined functions allow programmers to break large programs into smaller, more manageable pieces called functions. Functions can return values to the calling code using the return statement. Value-returning functions specify a return type and use return to pass a value back. Predefined and user-defined functions make programs more modular, reusable, and readable.

Uploaded by

aw.aw.free321
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Lecture 04

User-defined functions allow programmers to break large programs into smaller, more manageable pieces called functions. Functions can return values to the calling code using the return statement. Value-returning functions specify a return type and use return to pass a value back. Predefined and user-defined functions make programs more modular, reusable, and readable.

Uploaded by

aw.aw.free321
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

User Defined functions

a C++ program is a collection of functions. One such function is main. The programs in previous
lectures 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, as you will soon discover. You must
learn to break the problem into manageable pieces.

Functions are like building blocks. They let you divide complicated programs into manageable
pieces. They have other advantages, too:
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 indifferent programs, you can
write it once and use it many times.
4- Using functions greatly enhances the program’s readability because it reduces the complexity
of the function main.

Functions are often called modules. They are like miniature programs; you can put them together
to form a larger program.

Predefined Functions:
In C++, the concept of a function, either predefined or user-defined

Some of the predefined mathematical functions are pow(x, y), sqrt(x), and floor(x).
The power function, pow(x, y), calculates xy; that is, the value of pow(x, y)= xy. For example,
pow(2, 3)= 23 = 8.0 and pow(2.5, 3)= 2.53 = 15.625. Because the value of pow(x, y) is of type
double, we say that the function pow is of type double or that the function pow returns a value
of type double. Moreover, 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 nonnegative square root of x for x >= 0.0. For
example, sqrt(2.25) is 1.5. The function sqrt is of type double and has only one parameter.
The floor function, floor(x), calculates the largest whole number that is less than or equal to x. For
example, floor(48.79) is 48.0. The function floor is of type double and has only one parameter

In C++, predefined functions are organized into separate libraries. For example, the header file
iostream contains I/O functions, and the header file cmath contains math functions. Below lists
some of the predefined functions, the name of the header file
To use predefined functions in a program, you must include the header file that contains
the function’s specification via the include statement. For example, to use the function
pow, the program must include:
#include <cmath>
User-Defined Functions
Because C++ does not provide every function that you will ever need and designers cannot
possibly know a user’s specific needs, you must learn to write your own functions.

User-defined functions in C++ are classified into two categories:


• Value-returning functions—functions that have a return type. These functions return a value of
a specific data type using the return statement.
• Void functions—functions that do not have a return type. These functions do not use a return
statement to return a value

Value-Returning Functions
The predefined C++ functions such as pow, abs, islower, and toupper. These are examples of
value-returning functions. To use these functions in your programs, you must know the name of
the header file that contains the functions’ specification. You need to include this header file in
your program using the include statement and know the following items:
(In this syntax, expression can be a single constant value.) Thus, to call a value returning function, you use
its name, with the actual parameters (if any) in parentheses. A function’s formal parameter list can be
empty. However, if the formal parameter list is empty, the parentheses are still needed.
The function heading of the value-returning function thus takes, if the formal parameter
list is empty, the following form:

functionType functionName()

If the formal parameter list of a value-returning function is empty, the actual parameter is
also empty in a function call. In this case (that is, an empty formal parameter list), in a
function call, the empty parentheses are still needed. Thus, a call to a value-returning
function with an empty formal parameter list is:
functionName()
In a function call, the number of actual parameters, together with their data types, must
match with the formal parameters in the order given.
return Statement
Once a value-returning function computes the value, the function returns this value via
the return statement. In other words, it passes this value outside the function via the
return statement.
Syntax: return Statement
The return statement has the following syntax:
return expr;
in which expr is a variable, constant value, or expression. The expr is evaluated, and its
value is returned. The data type of the value that expr computes must match the
function type.
Note : In C++, return is a reserved word.
When a return statement executes in a function, the function immediately terminates
and the control goes back to the caller. Moreover, the function call statement is replaced
by the value returned by the return statement. When a return statement executes in
the function main, the program terminates.

To put the ideas in this discussion to work, let us write a function that determines the
larger of two numbers. Because the function compares two numbers, it follows that this
function has two parameters and that both parameters are numbers.
Suppose that num, num1, and num2 are double variables. Also suppose that num1 = 45.75 and num2 = 35.50.
we can calls the function larger in in any form of the following:

num =larger(45.75,35.50);
num= larger(num1,num2);
num=larger(num1,35.50);
You can also write the definition of the function larger as follows:
double larger(double x, double y)
{
if (x >= y)
return x;
else
return y;
}
Because the execution of a return statement in a function terminates the function, the
preceding function larger can also be written (without the word else) as:
double larger(double x, double y)
{
if (x >= y)
return x;
return y;
}
Note that these forms of the function larger do not require you to declare any
local variable.
Now that the function larger is written, the following C++ code illustrates how to use it.
double one = 13.00;
double two = 36.53;
double maxNum;
Consider the following statements:
cout << "The larger of 5 and 6 is " << larger(5, 6) << endl; //Line 1
cout << "The larger of " << one << " and " << two << " is " << larger(one, two) << endl; //Line 2
cout << "The larger of " << one << " and 29 is “ << larger(one, 29) << endl; //Line 3
maxNum = larger(38.45, 56.78); //Line 4

In a function call, you specify only the actual parameter, not its data type. For example, in Example above ,
the statements in Lines 1, 2, 3, and 4 show how to call the function larger with the actual parameters.
However, the following statements contain incorrect calls to the function larger and would result in syntax
errors. (Assume that all variables are properly declared.)

You might also like