User Defined Functions

Submitted by: Submitted by

Views: 106

Words: 809

Pages: 4

Category: Other Topics

Date Submitted: 02/27/2014 11:37 PM

Report This Essay

USER DEFINED FUNCTIONS

a C++ program is a collection of functions. Every C++ program has at least one function which is main ( ).

The programs in Chapters 1 through 5 uses only the function main; the programming instructions are packed into one function. This technique is good only for short programs. But for large programs, it is not practical (although possible) to put the entire programming instructions into one function. - You must learn how to break the problem into manageable pieces.

This chapter first discusses the functions previously defined and then discusses user-defined functions.

Functions are like building blocks. They let you divide complicated program s into manageable pieces. They have other advantages, too.

• While working on one function, you canfocus on just that part of the program and construct it, debug it, and perfect 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.

PREDEFINED FUNCTIONS

In algebra, a function can be considered a rule or correspondence between values, called the function’s arguments, and the unique values of function associated with the arguments. Thus, if f(x) = 2x+5, then f(1) =7, f(2) = 9, and f(3) =11, where 1,2 and 3 are e the arguments of f, and 7, 9 and 11 are the corresponding values of the functions.

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 type double, we say that the function pow is of type double or that the function pow returns a value of type double....