Changes made - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

Changes made

Description:

Some functions are built in to PHP, some are added from code libraries, some you ... Parameter 2: max (int) Do lab exercise 2. ITM 352. 8. Passing Variables ... – PowerPoint PPT presentation

Number of Views:17
Avg rating:3.0/5.0
Slides: 22
Provided by: portk
Category:
Tags: changes | made | max | php

less

Transcript and Presenter's Notes

Title: Changes made


1
Changes made
  • Imported all the slides from the creating
    functions ppt
  • Deleted lets create some functions slide with
    array_key_exists, etc. on it
  • COUNT 18 slides not including titles

2
ITM 352Functions
3
Having Trouble???
  • If you are having trouble then please seek help
    as soon as possible!
  • Im not a mind reader!!!!
  • If you are silent, I will assume that everything
    is going fine.
  • The longer you wait, the more difficult it is to
    catch up.

4
Functions
  • A function is a named block of code (i.e. within
    s) that performs a specific set of statements
  • Possibly act on a set of values given to it
    (parameters)
  • Possibly returns a single value (return-value)
  • Functions are very useful for
  • Repeated tasks in multiple locations
  • Saving execution time
  • Modularization

5
Functions
  • Weve already made use of several functions!
  • Can you think of some?
  • Some functions are built in to PHP, some are
    added from code libraries, some you will define

Do lab exercise 1
6
Calling a Function
  • All PHP functions are designated by
    ltidentifiergt(ltparametersgt)
  • Ex. phpinfo(), rand(1,10)
  • Not case sensitive
  • Functions are used by calling them
  • lt? phpinfo() ?gt
  • Full interface for a function
  • ltreturn typegt ltidentifiergt ( ltparametersgt )

7
Passing Values into a Function Parameters
  • Some Functions can be more flexible (therefore
    useful) if we pass them input values to use in
    their operations
  • Input values for functions are called passed
    values or parameters
  • Parameters must be specified inside the
    parentheses () of the heading in the function, be
    of the expected data type, in the expected order
    as defined by the functions interface
  • rand(int min, int max)
  • rand(1,10)

Do lab exercise 2
8
Passing Variables
  • Any legal expression can be used as a parameter
    (recall that an expression returns a value)
  • rand(443, 2maxmax)
  • It is very common to use a variable for a
    parameter (remember that a variable by itself is
    a simple legal expression it returns the value
    of the variable)
  • rand(minGuess,maxGuess)
  • substr(name,1,i)

9
Return Values of Functions
  • Some Functions just perform an action (e.g. read
    in a value from the keyboard, switch to a web
    page) and do not return a value
  • Most functions perform an action and return a
    single value
  • Return types may be
  • a primitive data type, such as int, float,
    boolean, etc.
  • a compound type such as array, object, etc.
  • void if no value is returned
  • Return values are how a function passes
    information back after it is called and after it
    performs its operations.

10
Return Values of Functions
  • You can use a function with a return type
    anyplace where it is legal to use an expression,
  • guess rand(1,10)
  • 3.14159rand(1,10)
  • echo phpinfo()
  • yummy piece of . substr(piece,0,3)
  • if( is_int(guess) )
  • It is common for a function to return a boolean
    value
  • true is the function operations were successful
    with no problems
  • false if the function failed to perform its task
  • if( !writeFile() ) echo file not written

Do lab exercise 3
11
Function Documentation
  • Functions are documented not only with a
    description of the function, but also with a
    interface that shows the return value type, the
    name of the function, and the required and
    optional arguments
  • type function_name (type arg1, type arg2 ,type
    optional_arg)
  • PHP functions often document functions this way
  • printf
  • (PHP 3, PHP 4 )
  • printf -- Output a formatted string
  • Description
  • void printf ( string format , mixed
    args)Outputs a string by using format and the
    given arguments
  • You can look up functions using the book index,
    Appendix A function categories, or PHPeds help

function name
function signature
function description
Do lab exercise 4
12
ITM 352Creating Functions
13
Defining Functions
  • A very important aspect of PHP is the ability to
    create your own functions. You will find out how
    useful this is later.
  • You declare a function with the function keyword
    in front of a identifier and function parameter
    set and a code-block
  • function ltidentifiergt ( ltparametersgt )
  • // function operations (code statements) here
  • return ltexpressiongt
  • The way you define a function defines the
    functions interface (or how it is expected to be
    used)

14
Function Syntax and Example
  • function square(num)
  • answer num num
  • return answer
  • To call the function
  • quantity 3
  • myvar square (quantity)
  • myvar will contain the value 9
  • function funcname(var1, var 2...)
  • statement 1
  • statement 2
  • statement 3
  • ...
  • return retval

NOTE that the variable names do not need to be
the same the order of variables passed is what
will matter inside the function!
15
Global vs. Local Variables
  • Variables defined inside a function (or passed
    in) are local variables and are only available
    within functions
  • after exiting the function the variable ceases to
    exist!!!
  • Variables defined outside of a function are
    generally not available inside a function (this
    avoids accidental conflicts so variables should
    be passed into a function as arguments)
  • Global variables are accessible both in and out
    of functions
  • Where the variable is active (alive) is known as
    its scope
  • use global and static to modify this
  • also passing by reference will affect scope

16
Scope of Variables
  • Inside a function is like an entirely new
    program.
  • Variables that you define within the function
    have local scope
  • They dont exist before the function starts
  • They dont exist once the function has completed

17
Scope of Variables 2
  • lt?php
  • function deposit(amount)
  • balance amount
  • echo New balance is balance ltBRgt"
  • balance 600
  • deposit(50)
  • echo "Balance is balance"
  • ?gt
  • What will this program print?
  • Why?

18
Defining Functions
  • function swapTwoValues(a, b)
  • tmp a // save old value temporarily
  • a b // copy second parameter into first
  • b tmp // copy original first value into
    second
  • var1 1 var2 2
  • echo \var1 is var1 and \var2 is var2ltBRgt
  • swapTwoValues(var1, var2)
  • echo \var1 is var1 and \var2 is var2
  • What would happen if we used a and b instead?

19
Function Naming Conventions
  • Good Programming Practice (we will look for
    this!)
  • Use verbs to name functions that do not return
    values or operate directly on variables
  • They usually perform an action e.g. sort(),
    print_table()
  • Use nouns to name functions that return a value
  • they create (return) a piece of data, a thing
    e.g. date()
  • Start function names with a lower case letter
  • phpinfo()
  • Use _ to separate words
  • is_bool()
  • Use descriptive names
  • get_html_translation_table()

Do lab exercise 5
20
Pass-By-Value vs. Pass-By-Reference
  • When a function is called, the value of each
    argument is copied (assigned) and used locally
    within that function
  • Variables used as arguments cannot be changed by
    a function!!!!!!!!!
  • One way to change a value of a passed in variable
    is to make use of return value
  • function doubler(value)
  • return 2value
  • echo doubler(doubleUp)
  • doubleUp doubler(doubleUp)

21
Pass-By-Value vs. Pass-By-Reference
  • Sometimes its more convenient to directly
    operate on a variable, so you pass in its
    reference (address)
  • function swapTwoValues(a, b)
  • tmp a // save old value temporarily
  • a b // copy second parameter into first
  • b tmp // copy original first value into
    second
  • var1 1 var2 2
  • echo \var1 is var1 and \var2 is var2ltBRgt
  • swapTwoValues(var1, var2)
  • echo \var1 is var1 and \var2 is var2
Write a Comment
User Comments (0)
About PowerShow.com