Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

CMSC 126

PHP User-defined Functions

2nd semester, SY 2018-19


Wk 13

Forms of User-Defined Functions

1. A function with no parameters and no return value

• Syntax: function functionName(){

}
• Uses: Applicable for writing descriptions about a program

2. A function with parameters but does not return any value

• Syntax: function functionName($var1, $var2){

}
• Uses: This is useful when you want to pass data to the function and print the
values.

Forms of User-Defined Functions

3. A function with parameters and returns a value


Syntax: function funcName($var1, $var$){
var $temp;
// sum processes here
return $temp
}

Examples:
1. Accept two variables and return their sum
2. Accept two variables and determine the highest number

1
Forms of User-Defined Functions

3. A function that accepts arguments and returns a value

Example: Output:

Sum = 15
<?php
$result = addition(5, 10);
echo "Sum = $result";
function addition($val1, $val2) {
$sum = $val1 + $val2;
return $sum;
}
?>

Forms of User-Defined Functions


3. A function that accepts arguments and returns a value. The function
is an external file.
file1.php: Output (file7func.php):
<?
include “file2.php”; Sum = 15
$result = addition(5, 10);
echo "Sum = $result";
?>
The function is located in
another file.

file2.php The include “file2.php” means


<?php
function addition($val1, $val2) {
that this is where the addition
$sum = $val1 + $val2; function can be found.
return $sum;
}
?>
// this is the file where the function is called

Exam ple:

<?php
$num1 = 45;
$num2 = 146;
$num3 = 12;
progdetails();
showval($num1, $num2, $num3);
$hi = findlarge($num1, $num2, $num3);
$low = findsmall($num1, $num2, $num3);
echo "<br>The largest value = $hi <br><br>";
echo "The smallest value = $low <br>";

// This is an example of a function w/ no arguments & no return value


function progdetails(){
echo "This program illustrates the different forms of user-defined functions.<br>";
echo "Form 1 - a function with no parameter and no return value <br> ";
echo "Form 2 - a function with parameters but does not return a value <br>";
echo "Form 3 - a function with parameters and returns a value <br> <br>";
echo "This program will find the largest and smallest element of the three variables<br>";
}

2
Exam ple:

// This is an example of a function that has arguments but does not ret a value
function showval($a, $b, $c){
echo "<br>The 1st integer = $a <br>";
echo "The 2nd integer = $b <br>";
echo "The 3rd integer = $c <br>";
}
// This is an example of a function that has arguments & ret a value
function findlarge($a, $b, $c){
$l = $a;
if ($l < $b) $l=$b;
if ($l < $c) $l=$x;
return $l;
}
// This is an example of a function that has arguments & ret a value
function findsmall($a, $b, $c){
$s = $a;
if ($s > $b) $s = $b;
if ($s > $c) $s = $c;
return $s;
}
?>

Program Output:

This program illustrates the different forms of user-defined functions.


Form 1 - a function with no parameter and no return value
Form 2 - a function with parameters but does not return a value
Form 3 - a function with parameters and returns a value

This program will find the largest and smallest element of the three variables

The 1st integer = 45


The 2nd integer = 146
The 3rd integer = 12

The largest value = 146

The smallest value = 12

Forms of User-Defined Functions

4. A function with parameters and returns multiple values


Syntax: function funcName(&$var1, &$var$){
// sum processes here
// sum processes here
}

To change the value of the parameters, they should be preceded


with “&” symbol. This means that we are referring to the memory of
the variables.

This can be done by passing the parameters as reference.

3
Forms of User-Defined Functions

1. Pass by reference --- you pass a pointer to the value in memory. This means
that any change on the value of the variables within the function, this will be
brought back the calling program. This can be done by adding a prefix symbol
“&” to your variable.

Format: function funcName(&$var1, &$var$) {


// code here
}

2. Pass by value - you pass a copy of the value in memory. This means that any
changes on the variables within the function has no effect after using the
function.

Forms of User-Defined Functions

4. A function with parameters and returns multiple values


<?php
$num1 = 45;
$num2 = 146;
$num3 = 12;
progdetails();
showval($num1, $num2, $num3);
addval($num1, $num2, $num3);
showval($num1, $num2, $num3);

// This is an example of a function w/ no arguments & no return value


function progdetails(){
echo "This program demonstrates how to pass parameters to the function by value <br>";
echo "and by reference. Passing a parameter by value means that any change of the <br>";
echo "values inside the function, this will be sent back to the main program. <br>";
}

Forms of User-Defined Functions

4. A function with parameters and returns multiple values


function addval(&$a, &$b, &$c){ // pass by reference
$a++;
$b++;
$c++;
}

function showval($a, $b, $c){ // pass by value


echo "<br>The 1st integer = $a <br>";
echo "The 2nd integer = $b <br>";
echo "The 3rd integer = $c <br>";
}
?>

4
Forms of User-Defined Functions
Example: This program will pass an array to a function and add each element of the array with 1

<?php
$num = array(34, 32, 12, 41, 12);
progdetails();
echo "<br>Original array value: "; showval($num);

add1($num);
echo "<br>Add 1 to each array value: "; showval($num);

// This is an example of a function w/ no arguments & no return value


function progdetails(){
echo "This program demonstrates how to pass parameters to the function by value ";
echo "and by reference. Passing a parameter by value means that changes of the ";
echo "values inside the function will be sent back to the main program. <br>";
}

function add1(&$num ) { // pass by reference


for ($i=0; $i < sizeof($num)-1; $i++)
$num[$i]++;
}

Forms of User-Defined Functions

Example: This program will pass an array to a function and add each element of the array with 1

function showval($num ){ // pass by value


for ($i=0; $i < sizeof($num); $i++)
echo "$num[$i] ";
echo "<br>";
}

?>

Forms of User-Defined Functions


Example: This program will pass an array to a function and sort the array in ascending order.

<?php
$num = array(34, 32, 12, 41, 12);
progdetails();
echo "<br>Original array value: "; showval($num);
ascend_sort($num);
echo "<br>The array in ascending order: "; showval($num);

// This is an example of a function w/ no arguments & no return value


function progdetails(){
echo "This program demonstrates how to pass parameters to the function by value ";
echo "and by reference. Passing a parameter by value means that changes of the ";
echo "values inside the function will be sent back to the main program. <br>";
}
function ascend_sort( &$num ){ // pass by reference
for ($i=0; $i < sizeof($num)-1; $i++)
for ($j=$i+1; $j < sizeof($num); $j++)
if ($num[$i]>$num[$j]) swap($num[$i], $num[$j]);
}

5
Forms of User-Defined Functions

Example: This program will pass an array to a function and sort the array in ascending order.

function swap(&$a, &$b){ // pass by reference


$t = $a; $a = $b; $b = $t;
}

function showval($num ){ // pass by value


for ($i=0; $i < sizeof($num); $i++)
echo "$num[$i] ";
echo "<br>";
}
?>

Resources:

https://www.php.net/manual/en/intro-whatis.php
https://www.w3schools.com/php/default.asp
https://techterms.com/
https://www.tutorialspoint.com/php/php_functions.htm

Thank you!

You might also like