0% found this document useful (0 votes)
6 views9 pages

Introduction To PHP Funct.8776715.powerpoint

The document provides an introduction to functions in PHP, explaining their purpose in organizing code into manageable sections. It covers various aspects such as passing parameters by value and reference, default argument values, optional arguments, returning values, nesting functions, and recursive functions. Examples are provided to illustrate each concept effectively.

Uploaded by

anitarooge
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views9 pages

Introduction To PHP Funct.8776715.powerpoint

The document provides an introduction to functions in PHP, explaining their purpose in organizing code into manageable sections. It covers various aspects such as passing parameters by value and reference, default argument values, optional arguments, returning values, nesting functions, and recursive functions. Examples are provided to illustrate each concept effectively.

Uploaded by

anitarooge
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Introduction to Functions

PHP Fundamentals: Functions


 A set of constructs which allow the programmer
to break up their code into smaller, more
manageable, chunks which can be called by
name and which may or may not return a value.

 function function_name (parameters) {


function-body
}

 function helloWorld(){
echo "HelloWorld";
//No out put is shown
}
Functions: Passing Parameters
 Passing parameters by value
– function salestax($price,$tax) {
$total = $price + ($price * $tax
echo "Total cost: $total";
}

salestax(15.00,.075);

$pricetag = 15.00;
$salestax = .075;

salestax($pricetag, $salestax);
Functions: Passing Parameters
 Passing parameters by reference
$cost = 20.00;
$tax = 0.05;
function calculate_cost(&$cost, $tax)
{
// Modify the $cost variable
$cost = $cost + ($cost * $tax);
// Perform some random change to the $tax
variable.
$tax += 4;
}
calculate_cost($cost,$tax);
echo "Tax is: ". $tax*100."<br />";
echo "Cost is: $". $cost."<br />";
Functions: Default Argument Values
 Default values are automatically assigned to the
argument if no other value is provided

function salestax($price, $tax=.0575) {


$total = $price + ($price * $tax);
echo "Total cost: $total";
}

$price = 15.47;
salestax($price);
Functions: Optional Arguments
 Certain arguments can be designated as
optional by placing them at the end of the list
and assigning them a default value of nothing .

function salestax($price, $tax="") {


$total = $price + ($price * $tax);
echo "Total cost: $total";
}

salestax(42.00);
function calculate($price,$price2="",$price3="")
{
echo $price + $price2 + $price3;
Functions: Returning Values
 You can pass data back to the caller by way of the
return keyword.
 function salestax($price,$tax=.0575) {

$total = $price + ($price * $tax);


return $total;
}
$total = salestax(6.50);
 Returning Multiple Values

function retrieve_user_profile() {
$user[] = "Jason";
$user[] = "[email protected]";
return $user; }
list($name,$email) = retrieve_user_profile();
echo "Name: $name, email: $email ";
Functions: Nesting Functions
 defining and invoking functions within functions

function salestax($price,$tax)
function convert_pound($dollars,
$conversion=1.6) {
return $dollars *
$conversion;
}
$total = $price + ($price * $tax);
echo "Total cost in dollars: $total. Cost in
British pounds: "
. convert_pound($total); }
salestax(15.00,.075);
echo convert_pound(15);
Functions: Recursive Functions
functions that call themselves

function nfact($n) {
if ($n == 0) {
return 1;
}else {
return $n * nfact($n - 1);
}
}
//call to function
nfact($num) ;

You might also like