Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lesson 3 Functions. Lesson 3 Functions are declared with function. For example, to calculate the cube of a number function function name (parameters)

Similar presentations


Presentation on theme: "Lesson 3 Functions. Lesson 3 Functions are declared with function. For example, to calculate the cube of a number function function name (parameters)"— Presentation transcript:

1 Lesson 3 Functions

2 Lesson 3 Functions are declared with function. For example, to calculate the cube of a number function function name (parameters) { body of function }

3 Functions Lesson 3

4 Functions Lesson 3 Passing Arguments Arguments provide a way to pass input to the function. When we write the code cube(3), “3” is the argument, the argument is available within the function as the parameter $num. The parameter takes on the value of the corresponding argument that is passed to it in the Invocation of the function. The cube function then uses this value to compute the return value.

5 Functions Lesson 3

6 Functions Lesson 3 By default, arguments are passed by value. This means that the parameter variable within the function holds a copy of the value passed to it. If the parameter’s value changes, it will not change the value of a variable in the calling statement. Consider the following :

7 Functions Lesson 3

8 Functions Lesson 3 When an argument is passed by reference, changes to the parameter variable do Result in changes to the calling statement’s variable. An ampersand (&) is placed before the parameter’s name to indicate that the argument should passed by reference.

9 Functions Lesson 3

10 Functions Lesson 3 It is also possible to establish a default value for a parameter

11 Functions Lesson 3 Variable Scope and Lifetime The Scope of a variable determines which parts of the program have access to it Consider the following example:

12 Functions Lesson 3

13 Functions Lesson 3

14 Functions Lesson 3

15 Functions Lesson 3 Static variables retain their previous values each time a function is invoked

16 Functions Lesson 3 Static variables retain their previous values each time a function is invoked

17 Functions Lesson 3 Nested Function You may wish for a function to contain other functions on which it depends

18 Functions Lesson 3 Recursion Function When Function calls itself

19 Functions Lesson 3 Recursion Function When Function calls itself

20 Lesson 3 Arrays

21 Lesson 3 Initializing Arrays This code creates an array with three elements. Since we did not explicitly specify indices inside the square brackets, the elements have been given the default indices 0,1 and 2.

22 Arrays Lesson 3 It is usually practical to assign indices in sequential order, as we have done above; but if necessary, we can assign any integer index you please :

23 Arrays Lesson 3 If we need to know how many elements are in array, we can use the count() function

24 Arrays Lesson 3 Another way to initialize an array is with array() construct

25 Arrays Lesson 3 Looping Through an Array

26 Arrays Lesson 3 Looping Through an Array ( Non Sequentially Indexed Arrays) The two function each() and list() can be used together to loop through an array, even if The indices are non sequential (or even if they are not numbers at all). We can determine the value of the current element using the current() function, and the Current element’s index using the key() function.

27 Arrays Lesson 3

28 Arrays Lesson 3 While (list ($key,$value)= each($countries) ) For each element in the array, set $key equal to the element’s key (or index), and $value equal to the value of the element, reset() function sets the internal Pointer to the first element. The each() function moves the array pointer one element forward every time it is called

29 Arrays Lesson 3 String Indexed Arrays

30 Arrays Lesson 3 Sorting Functions Sort()

31 Arrays Lesson 3 Sorting Functions

32 Arrays Lesson 3 asort()

33 Arrays Lesson 3 The rsort() and arsort() functions are identical to sort() and asort() respectively, expect That they sort arrays in reverse order. The ksort(), krsort() function sorts arrays by key, rather Than by value : sort()

34 Arrays Lesson 3

35 Arrays Lesson 3 rsort()

36 Arrays Lesson 3 arsort()

37 Arrays Lesson 3 ksort()

38 Arrays Lesson 3 krsort()

39 Lesson 3 End of Lesson 3


Download ppt "Lesson 3 Functions. Lesson 3 Functions are declared with function. For example, to calculate the cube of a number function function name (parameters)"

Similar presentations


Ads by Google