Chapter 4
Chapter 4
Python provides some out-of-the-box built-in functions for performing some common programming tasks called.
Some common functions are :int(), float(), str(), type(), bool(), len(), chr(), min(), max(), range(), hex(), bin(),
abs(), ord(), pow(), raw_input(), sum(), format(), cmp(), dir(), oct(), round(), print()
Python also allows programmers to define user-defined functions.
perform a task or set of tasks
no return a value
has a function name and may have zero or more parameters
Python: Functions and Procedures
Built in Functions User Defined Functions
1 >>> len("Technology") 1 def print1to10():
2 10 2 for i in range(1,11):
3 >>> chr(65) 3 print i,
4 'A' 4
5 >>> ord('B') 5 # to call the function
6 66 6 print1to10()
7 >>> range(1,10) 7
8 [1, 2, 3, 4, 5, 6, 7, 8, 9] >>>
9 >>> range (1,20, 3) 8 1 2 3 4 5 6 7 8 9 10
10 [1, 4, 7, 10, 13, 16, 19]
11 >>> range (20,1,-5)
12 [20, 15, 10, 5]
13 >>> sum([3,2],0)
14 5
15 >>> sum(range(1,5), 0)
16 10
17 >>> round(0.89377,3)
18 0.894
Python: Parameter Passing
Functions can also accept values to be used as part as their execution, such values are stored in
variables. The variables are called parameters. Python has four mechanisms for passing parameters
Normal parameters: Functions can accept values of ay data type i.e. int, float, string, list, tuples etc.
as parameters.
There are special functions in Python called “lambda”. They are functions that have no name (i.e. anonymous),
contain only expressions and no statements and occupy only one line. They are convenient to use. A lambda can
take multiple arguments and can return (like a function) multiple values
Function names are case-insensitive; that is, you can call the sin() function as sin(1), SIN(1), SiN(1), and so on,
because all these names refer to the same function. By convention, built-in PHP functions are called with all
lowercase.
1 <?php
2 function findAverage(){
3 $average=0;
4 echo “average is “, $average;
5 }
6 ?>
PHP: Parameter Passing
In PHP, There are several kinds of parameters
Default parameters: To specify a default parameter, assign the parameter value in the function
declaration. The value assigned to a parameter as a default value cannot be a complex expression; it
can only be a scalar value.
1 <?php
2 function findAverage($num1, $num2, $num3=7){
3 $average=($num1 + $num2+$num3)/3;
4 return $average;
5 }
6 ?>
PHP: Parameter Passing
Variable Parameters: A function may require a variable number of arguments. To declare a function with a
variable number of arguments, leave out the parameter block entirely
1 <?php
2 function findAverage(){
3 if (func_num_args() == 0) {
4 return 0;
5 }
6 else {
$sum = 0;
for ($i = 0; $i<func_num_args(); $i++) {
$sum += func_get_arg($i);
}
$average=$sum/func_num_args();
return $average;
}
}
?>
PHP: Parameter Passing
PHP provides three functions you can use in the function to retrieve the parameters passed to it.
func_get_args() returns an array of all parameters provided to the function;
func_num_args() returns the number of parameters provided to the function;
func_get_arg() returns a specific argument from the parameters.
For example:
$array = func_get_args();
$count = func_num_args();
$value = func_get_arg(argument_number);
PHP: Parameter Passing
Missing Parameters: PHP allows the passing of any number of arguments to the function. Any parameters the
function expects that are not passed to it remain unset, and a warning is issued for each of them:
1 <?php
2 function findAverage($num1, $num2, $num3=7){
3 $average=($num1 + $num2+$num3)/3
4 return $average
5 }
6 ?>
PHP: Parameter Passing
Passing Parameters by Value
This means the value of the parameter is copied into the function i.e. the function has access to a copy of the value
not the original .The function is evaluated, and the resulting value is assigned to the appropriate variable in the
function. In all of the examples so far, we’ve been passing arguments by value.
1 <?php
2 function findAverage(&$num1, &$num2,){
3 $average=($num1 + $num2)/2
4 return $average
5 }
6 ?>
PHP: Parameter Passing
To return a value by reference, both declare the function with an & before its name and when assigning the
returned value to a variable:
1 <?php
2 function &findAverage($num1, $num2, $num3){
3 $average=($num1 + $num2+$num3)/3
4 return $average
5 }
6 ?>
PHP: Anonymous Functions
PHP allows the definition of localized and temporary functions. Such functions are called anonymous functions or
closure. It is defined using the normal function definition syntax, but assign it is then assigned to a variable or pass
it directly.
1 <?php
2 function findAverage($num1, $num2, $num3, function(){ return
3 $sum=$num1 + $num2+$num3)}){
4 $average=($num1 + $num2+$num3)/3
5 return $average
6 }
?>
Python and PHP
Python PHP
Function Keyword def function
Parameter Passing pass by reference pass by value, pass by reference