Summary ICT3612 EXAM PACK
Selected topics for discussion • Functions • Classes and objects • Past exam: May/June 2019 exam • General discussion Syntax of a function (pages 382 to 383) 1. keyword function 2. function name 3. parameters 4. body of function 5. return statement Calling/invoking the function: function addHundred($num){ $result = $num + 100; return $result; } $number = 2; $value = addHundred($number); Exercise 1 Write a function that accepts three parameters (integers) and returns the sum of the three integers. function calculateSum($num1, $num2, $num3){ $result = $num1 + $num2 + $num3; return $result; } Pass arguments by value and by reference (pp 384 to 385) • Pass by value - A copy of the argument is passed to the function - Changes to the parameter in the function does not change the value of the original variable • Pass by reference - A pointer to the original variable is passed to the function - Changes to the parameter in the function changes the value of the original variable. Pass arguments by value (pp 384 to 385) - Example function addHundred($num){ $result = $num + 100; return $result; } Calling/invoking the function: $number = 2; $value = addHundred($number); The value stored in $number is 2 before and after addHundred() is invoked. Pass arguments by reference (pp 384 to 385) - Example function addHundred(&$num){ $num = $num + 100; } Calling/invoking the function: $number = 2; addHundred($number); The value stored in $number is 2 before addHundred()is invoked and the value stored in $number is 102 after addHundred() is invoked. Exercise 2 function calculate($arr, &$num){ $result = 0; foreach($arr as $value){ $result += $value; } $num = $result / 10; } $arr = array(0,2,4,6,8); $num = 0; calculate($arr,$num); echo $num; What is the output generated by the following code? Default value for function parameter (pp 388 to 389) • A default
Geschreven voor
- Instelling
- University of South Africa
- Vak
- ICT3612 - Advanced Internet Programming
Documentinformatie
- Geüpload op
- 6 oktober 2021
- Aantal pagina's
- 28
- Geschreven in
- 2021/2022
- Type
- Tentamen (uitwerkingen)
- Bevat
- Vragen en antwoorden
Onderwerpen
-
ict
-
3612