Chapter 8 Functions in Depth
Chapter 8 A programmer-defined function is a block of statements, or a subprogram, that is written to perform a specific task required by you, the programmer.
Chapter 8 Functions eliminate the need for duplicate statements within a program. Given a task to be performed more than once, the statements are written just once for the function. Then, the function is called each time the task must be performed. Functions make the program easier to design, since the program behaviors (algorithms) are modularized as individual functions, whose combined execution solves the problem at hand.
Chapter 8 Functions make the program easier to code and test, since you can concentrate on a single function at a time, rather than one big main() function. Functions allow for software reusability, where existing functions can be reused to create new programs. Functions make the program more clear and readable, thus making the program easier to maintain.
Chapter 8 Functions provide the basis for structured as well as object-oriented programming
Chapter 8 A non-void function is a function that, when called, returns a single value to the calling program.
Chapter 8 Non-void Function Format //FUNCTION HEADER return data type function name (parameter list) { //BEGIN FUNCTION STATEMENT BLOCK //LOCAL VARS local variables should go here //FUNCTION STATEMENTS or BODY statement #1; statement #2; statement #n; return value; } //END FUNCTION STATEMENT BLOCK
Chapter 8 The function header forms the interface between the calling program and the function.
Chapter 8 The function header consists of a return type, if any, the function name, and a parameter listing.
Chapter 8 The value returned by a non-void function replaces the function name wherever the name is used in the calling program. Examples: result = cube(a); cout << cube(a); solution = 2 cube(a) + 5;
Chapter 8 Arguments are values/variables used within the function call, and parameters are variables used within the function header that receive the argument values.
Chapter 8 A void function is a function that returns multiple values or performs some specific task, rather than returning a single value to the calling program.
Chapter 8 When calling a void function, simply list the function name and required arguments as a single statement within your program. Do not call a void function with an assignment operator or cout object as you do non-void functions. Example: displayHeader();
Chapter 8 A value parameter provides for one-way communication of data from the calling program to the function. A reference parameter provides two-way communication of data between the calling program and function.
Chapter 8 Locating Functions in a Program
Chapter 8 A function prototype is a model of the interface to the function. Simply copy/paste the function header and add a semicolon to code the prototype. Locate your function prototypes after the preprocessor directives and before main().
Chapter 8 A default parameter is a function parameter that is assigned a default value in either the function prototype or the function header, but not both. Example: int volume(int length, int width = 5, int height = 2);
Chapter 8 An overloaded function exhibits different behavior with a different number of arguments or argument data types. Example: i nt calculateArea(int); int calculateArea(int, int); double calculateArea(double);
Chapter 8 A local variable is a variable that is defined within a specific block of code, such as a function. The scope of a variable refers to the largest block in which a given variable is accessible. Always define your variables as locally as possible within a given function block. If variables need to be shared among functions, pass them.
Chapter 8 Recursion is a process whereby an operation calls and clones itself until a terminating condition is reached. Examples of recursive operations include compound interest calculation, summation, and factorial, just to mention a few.
Chapter 8 Calculating a Monthly Compounded Bank Balance using Recursion If n is 0 balance(n) = deposit Else balance(n) = (1+rate/12/100) balance(n–1) (Where n is the month for which the balance must be determined.)
Chapter 8 A recursive function must always reach a terminating condition. If it does not, the function will keep calling itself forever, resulting in a memory overflow run-time error.
Chapter 8 double balance(double deposit, int month, double rate) { if (month == 0) return deposit; else return (1 + rate / 12 / 100) * balance(deposit,month – 1,rate); } //END balance()
Chapter 8
If n is 1 sum(n) = 1 Else sum(n) = n + sum (n – 1)
Chapter 8 int sum(int n) { if (n == 1) return 1; else return n + sum(n 1); } //END sum()
Chapter 8 int sum(int n) { int subTotal = 0;//SUM SUBTOTAL for (int count = 1; count <= n; ++count) subTotal = subTotal + count; return subTotal; } //END sum()