0% found this document useful (0 votes)
5 views26 pages

Class 3

The document provides an overview of functions in PHP, including their definition, types, and usage. It covers key concepts such as function arguments, return values, recursion, and anonymous functions. The document also includes examples to illustrate how to create and call functions, as well as different argument types like default and variadic arguments.

Uploaded by

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

Class 3

The document provides an overview of functions in PHP, including their definition, types, and usage. It covers key concepts such as function arguments, return values, recursion, and anonymous functions. The document also includes examples to illustrate how to create and call functions, as well as different argument types like default and variadic arguments.

Uploaded by

karthiknrg
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

ASIRIYAR ACADEMY- PGTRB

PHP & MYSQL


Date: 11-Aug-2025

Class 3 - Functions & its Types


Day 3 - Topics

❏ Defining a Function

❏ Calling a Function

❏ Default Arguments

❏ Variadic arguments

❏ Named Arguments

❏ Call by Value

❏ Call by Reference

❏ Return Values

❏ Recursive Functions
Functions in PHP

● A function is a block of statements that can be used repeatedly in

a program.

● A function will not execute automatically when a page loads.

● A function will be executed by a call to the function.

Why use Functions?

● Reusable code is defined as code that can be written once and used repeatedly.

● Breaking code into smaller parts makes it easier to maintain.

● Functions clarify and arrange the code.


Creating a Function
To create a new function,

❏ use the function keyword,

❏ followed by the name of the function you may want to use.

❏ In front of the name, put a parenthesis, which may or may not contain arguments.

❏ It is followed by a block of statements delimited by curly brackets.

❏ This function block contains the statements to be executed every time the function is

called.

function myMessage() {

echo "Hello world!";

}
Function Call
Once a function is defined, it can be called any number of times, from anywhere in the PHP
code. Note that a function will not be called automatically.

To call the function, use its name in a statement; the name of the function followed by a
semicolon.

To call the function, just write its name followed by parentheses ():

function myMessage() {

echo "Hello world!";

myMessage();
Types of Functions

Built-in functions − PHP's standard library contains a large number of built-in


functions for string processing, file IO, mathematical computations and more.

User-defined functions − You can create user-defined functions too, specific to


the requirements of the programming logic.
Function Arguments
Information can be passed to functions through arguments.

An argument is just like a variable.

Arguments are specified after the function name, inside the parentheses. You can
add as many arguments as you want, just separate them with a comma.

Example:

<?php

function familyName($fname) {

echo "Hello , $fname \n";

familyName("Jani");

familyName("Hege");
Function arguments
function familyName($fname, $year) {

echo "Hello $fname. Born in $year <br>";

familyName("Hege", "1975");

familyName("Stale", "1978");

familyName("Kai Jim", "1983");


Default Argument
function setHeight($minheight = 50) {

echo "The height is : $minheight <br>";

setHeight(350);

setHeight(); // will use the default value of 50

setHeight(135);

setHeight(80);
Guess the output
<?php
function greet($name = "Guest") {
echo "Hello, $name!";
}
greet();
echo "\n";
greet("Rashmi");
?>
Variadic Arguments

By using the ... operator in front of the function parameter, the


function accepts an unknown number of arguments. This is also called
a variadic function.

<?php

function sumAll(...$numbers) {

$s=0;

for($i=0;i<count($numbers);$i++)

$s=$s+$numbers[$i];

return $s;
Guess the output ?
<?php
function sumAll($name,...$numbers) {
$s=0;
for($i=0;$i<count($numbers);$i++)
$s=$s+$numbers[$i];
echo $name;
return $s;
}
echo sumAll("ramla",1, 2, 3, 4);
?>
Formal & Actual Arguments
❏ A parameter refers to the variable used in function's definition, whereas an argument

refers to the value passed to the function while calling.

❏ An argument may be a literal, a variable or an expression

❏ The parameters in a function definition are also often called as formal arguments,

and what is passed is called actual arguments.

❏ The names of formal arguments and actual arguments need not be same. The value of

the actual argument is assigned to the corresponding formal argument, from left to

right order.

❏ The number of formal arguments defined in the function and the number of actual

arguments passed should be same.


Example:

<?php
# Actual arguments more than
function addition($first, formal arguments
$second) {
addition(10, 20, 30);
$result = $first+$second;
# Actual arguments fewer than
echo "First number: $first \n"; formal arguments
echo "Second number: $x=10;
$second \n";
$y=20;
echo "Addition: $result \n";
addition($x);
}
?>
Actual Vs Formal arguments
Actual Arguments − The arguments that are passed in a function call.
Formal Arguments − The arguments that are declared in a function definition.

<?php
// $name is a formal argument
function greet($name) {
echo "Hello, $name!";
}
// Rahul is an actual argument
greet("Rahul");
?>
Call by Value
<?php
function addFunction($num1, $num2) {
$sum = $num1 + $num2;
return $sum;
}
$x = 10;
$y = 20;
$num = addFunction($x, $y);
echo "Sum of the two numbers is : $num";
?>
Guess the output
<?php
function appendString($str) {
$str .= " World!";
echo "Inside function: $str\n";
}
$text = "Hello";
appendString($text);

// Original string remains unchanged


echo "Outside function: $text";
Call by Reference
<?php

function swap(&$num1, &$num2) {

$temp=$num1;

$num1=$num2;

$num2=$temp;

$x = 10;

$y = 20;

echo "Before Swapping","\n";

echo $x,"\n",$y;

swap($x, $y);

echo "\After Swapping","\n";

echo $x,"\n",$y;
Named Arguments

<?php
function myfunction($x, $y) {
echo "x = $x y = $y";
}
myfunction(x:10, y:20);
?>
Named Arguments
<?php
function f1($a,$b,$c)
{
echo $a."\n";
echo $b."\n";
echo $c."\n";
}
f1(3,$b=5,$a=4,3);
?>
Guess the output

<?php
function myfunction($x, $y, $z) {
echo "x = $x y = $y z = $z";
}
myfunction(10, z:20, y:30);
?>
O/p: 10 30 20
Note: Named arguments can be combined with positional arguments, with the condition that,
the named arguments must come after the positional arguments.
Return Values
1. A function in PHP may have any number of arguments, but can return only
one value.
2. The function goes back to the calling environment as soon as it comes
across a return statement for the first time, abandoning the rest of
statements in the function body.
<?php
function raiseto($x) {
$sqr = $x**2;
$cub = $x**3;
return $sqr, $cub;
}
$a = 5;
$val = raiseto($a); //error
Conditional Return
<?php
function raiseto($x, $i) {
if ($i == 2) {
return $x**2;
} elseif ($i==3) {
return $x**3;
}
}
$a = 5;
$b = 2;
$val = raiseto($a, $b);
echo "$a raised to $b = $val" . PHP_EOL;
Recursion

❏ A recursive function is such a function that calls itself until a certain

condition is satisfied. In PHP, it is possible to defines a recursive function.

❏ Recursion is used when a certain problem is defined in terms of itself.

❏ Sometimes, it can be tedious to solve a problem using iterative approach.

Recursive approach provides a very concise solution to seemingly complex

problems.
Recursion : Factorial
<?php
function factorial ($n) {
if ($n == 1) {
return 1;
} else {
return $n*factorial($n-1);
}
}
echo "Factorial of 5 = " . factorial(5);
?>
Anonymous Functions - Closure
In PHP, an anonymous function, also known as a closure, is a function that does not have a
specified name.
These functions can be assigned to variables, passed as arguments to other functions, or
returned from functions.
<?php
// Assigning an anonymous function to a variable
$greet = function($name) {
return "Hello, " . $name . "!";
};
// Calling the anonymous function through the variable
echo $greet("Alice"); // Output: Hello, Alice!
echo "<br>";
echo $greet("Bob"); // Output: Hello, Bob!

You might also like