Title: functions ($$$)
Course: PHP/MySQL Course

Using built in functions

Read page 131 to 133 about calling a function.

Notes:
- You can recognize functions in code because after their name they will always have a parentheses like this: name(
- Function names do not start with $ like variables
- The arguments or parameters are supplied between the parentheses after the function name, each parameter can be anything that has a value, a number, or string, an expression, or another function call if it returns a value.
- To see a complete list of the hundreds of built in functions see this link: PHP Manual: function reference

Question?
What single character is used to separate each argument in the calling function?

- (,|comma)

Defining a function

Read pages 133 to 138 on defining a function.

Notes:
- always indent the 'body of the function', these are the lines between the braces { }.
- You should pick names for your functions that are descriptive of what the function does. examples: calculateTotalSales(), displayTable()
- functions that return a value can be called from anywhere that a value is expected; for example in an expression, on the right side of an assignment, even as a argument to another function call.

Question?
How many arguments are expected when you call this function definition?
function goodbye() {
echo "Good bye.";
}

- (0|zero|none)

Variable scope and globals

Read about variable scope and global variables on pages 137 to 140.

Notes:
- You should not generally use global variables. This is considered bad practice except in special circumstances. You should instead pass data and variable references into the function by way of its arguments, or we will lean that by wrapping both the functions and variables that need to be shared within an object you can accomplish what you need. (this will be in the next topic)
- variables defined inside of a function are called local variables because they can only be referred to by other lines of code from within that function.

Question?
What will the following code block print?
<?
function add($x,$y) {
return $x + $y;
}

echo add(1,3)+add(1,1);
?>

- 6

Functions Misc.

Read the rest of the chapter on functions (page 140 to 146)

Notes:
- static is usefull rarely, if you have a function that needs some 'memory' from last time it was called then static is the way to go. Example of a function that would keep a running total and return the new total every time you call it:

function runningTotal( $amount ) {
static $total = 0;
$total += $amount;
return $total;
}
- Default or optional arguments are very useful.
- Marking an argument as a reference by put int a & in front of it is very useful to be able to effectively return more than one value from a function.

Question?
What character do you put in front of a argument variable in the definition of a function to indicate to pass a reference to the original variables memory?

- $
- &
- %
- @