# Functions 1: Predefined and Value-Returning Functions

What are Functions?

In general, we use (call) functions (aka: modules, methods, procedures, subprocedures, or subprograms) to perform a specific (atomic) task. In algebra, a function is defined as a rule or correspondence between values, called the function's arguments, and the unique value of the function associated with the arguments. For example:

```If f(x) = 2x + 5, then f(1) =  7, f(2) =  9, and f(3) = 11

You would say the call f(1) returns the value 7

1, 2, and 3 are arguments
7, 9, and 11 are the resulting values (or corresponding values)

```
Bjarne Stroustrup's C++ Glossary
• C++ functions work in largely the same way. Format of a C++ function call:
• `  functionName(argumentList)`
where the argumentList is a comma-separated list of arguments (data being sent into the function).
In general, function arguments may be constants, variables or more complex expressions.
• Functions are building blocks
• Also called modules, methods, procedures, or sub-procedures
• Like miniature programs
• Can be put together to form larger program
• One of two principle means of modularization in C++ (other is classes)
Why use Functions?
• Divide and Conquer
• Allow complicated programs to be divided into manageable components
• Programmer can focus on just the function: develop it, debug it, and test it
• Various developers can work on different functions simultaneously
• Reusability:
• Can be used in more than one place in a program--or in different programs
• Avoids repetition of code, thus simplifying code maintenance
• Can be called multiple times from anywhere in the program
• Components:
• Custom functions and classes that you write
• Prepackaged functions and classes available in the C++ Standard Library

# Predefined Functions

Using predefined functions:

• C++ Standard Library contains many predefined functions to perform various operations
• Predefined functions are organized into separate libraries
• I/O functions are in iostream header
• Math functions are in cmath header
• Some predefined C++ mathematical functions:
• pow(x,y)
• sqrt(x)
• floor(x)
• Power Function - pow(x,y):
• Power function pow(x,y) has two parameters
• pow(x,y) returns value of type double
• pow(x,y) calculates x to the power of y: pow(2,3) = 8.0
• x and y called parameters (or arguments) of function pow
• Square Root Function - sqrt(x):
• Square root function sqrt(x) has only one parameter
• sqrt(x) returns value of type double
• sqrt(x) calculates non-negative square root of x, for x >= 0.0: sqrt(2.25) = 1.5
• Floor Function - floor(x):
• Floor function floor(x) has only one parameter
• floor(x) returns value of type double
• floor(x) calculates largest whole number not greater than x: floor(48.79) = 48.0

Example:
• In general, function arguments may be constants, variables or more complex expressions
• The pre-defined math function sqrt listed above takes one input value (of type double) and returns its square root. Sample calls:
```  double x = 9.0, y = 16.0, z;

z = sqrt(36.0); // sqrt returns 6.0 (gets stored in z)
z = sqrt(x);// sqrt returns 3.0 (gets stored in z)
z = sqrt(x + y);// sqrt returns 5.0 (gets stored in z)

cout << sqrt(100.0);// sqrt returns 10.0, which gets printed

cout << sqrt(49);     // because of automatic type conversion rules
//  we can send an int where a double is expected
//  this call returns 7.0

// in this last one, sqrt(625.0) returns 25.0, which gets sent as the
//  argument to the outer sqrt call.  This one returns 5.0, which gets
//  printed

cout << sqrt(sqrt(625.0));

```

# User-Defined Functions

Using User-Defined functions:

• Two types:
1. Void functions (nonvalue-returning): no return type, do not return a value
2. Value-returning functions: have a data type, return only one value to caller
• When utilizing functions:
• Know function name
• Know number of parameters, if any
• Know data type of parameters
• Know data type of value computed by function
• Value-returning function used 3 ways:
1. Assignment statement
2. Output statement
3. Argument (actual parameter) in another function call
• Creating functions:

• Mnemonic: "ProDeCall":
1. Prototype
2. Definition
3. Call
Function prototype includes:
• Function prototype: function heading without body of function
• Tells the compiler the following:
• Name of the function
• Type of data it receives
• Number of paramaters it expects
• Type of paramters
• Order of parameters
• While you don't HAVE to prototype, you SHOULD!
• Why? Reduce program errors!
1. Compiler correctly handles function return value
2. Compiler checks you used correct number of function arguments
3. Compiler checks you used correct type of arguments, if not, automatically type casts (numeric data types only)
4. NOTE: Some compilers do these automatically, BUT it's best to be safe--prototype your functions!
• Easiest way to prototype?
Copy function header and add a semi-colon (;)--however, don't need parameter names, only data types
• If prototypes not used, then function must be defined before it is called, that is above main()--not good to do with large programs
• If functions are defined before they are invoked, the definition serves as the prototype
```//function prototype syntax: only data types (not names) of parameters must be specified
functionType functionName(formal parameter list);
```
• Function signature:
• The name of the function along with the types of its arguments is called the signature
• Functions in the same scope must have unique signatures
Function definition includes:
1. Function name
2. Number of parameters (if any)
3. Data type of each parameter
4. Type of function (data type or void)
2. Body includes:
2. ***Any variables (if any) declared in body of function are local to function
3. All enclosed in braces {}
• Formal parameter (or parameter): variable declared in heading
• Actual parameter (or argument): expressions, variables, or constant values listed in function call
• ```//Value-returning function definition syntax: including header and body
functionType functionName(formal parameter list) //function header
{
//function body
statements...

//value-returning function return statement
return expression;
}
```
• Formal parameter list can be empty: parentheses still required
Function call includes:
• Name with actual parameters (if any) in parentheses
• In call statement, specify only arguments (actual parameters), not data types
• One-to-one correspondence between actual and formal parameters (signature)

• Mnemonic: "TON" - (must agree in Type, Order, and Number)
• Value-returning function called in an expression (only 3 ways):
1. Assignment statement
2. Output statement
3. Argument (actual parameter) in another function call
• Function call results in execution of body of called function
• ```//function call syntax:
x = functionName(actual parameter list); //Assignment, Output, Argument in another function call
```
• Call to value-returning function with empty formal parameter list:
• ```x = functionName(); //Assignment, Output, Argument in another function call
```
• After value-returning function is invoked, function returns value via return statement
• ```//return statement syntax:
return expression;
```
• When return statement executes:
1. Function terminates
2. Control goes back to caller
• ***When return statement executes in function main(), program terminates

# Flow of Execution

Program Execution:

• Begins with first statement in function main()
• Additional functions invoked when called
• Function prototypes appear before function definitions--that is, before function main()
• Compiler translates prototypes first
• Function call invocation:
1. Transfer of control to first statement in body of called function
2. After function body executed: control passed back to point immediately following function call
3. Value-returning function returns value
4. After function execution: value that function returns replaces function call statement
Function Example:
```
```