Semester
Subject
Year
Tribhuwan University
Model
Bachelor Level / Third Year / Fifth Semester / Science
(Web Technology II)
Full Marks: 60
Pass Marks: 24
Time: 3 Hours
Candidates are required to give their answers in their own words as for as practicable.
The figures in the margin indicate full marks.
Long Answers Questions
A function is a reusable block of code that performs a specific task, can accept parameters as input, and can return a value as output.
A function in PHP is a self-contained module of code that:
strlen(), array_push())Syntax:
function functionName($parameter1, $parameter2) {
// body of the function
return $value;
}
Rules for defining a function:
function()return statement sends a value back to the callerA function is called by writing its name followed by parentheses with arguments (if any):
$result = functionName($arg1, $arg2);
<?php
// Function definition with parameters and return value
function calculateArea($length, $width) {
$area = $length * $width;
return $area;
}
// Function call with arguments
$result = calculateArea(10, 5);
echo "Area of rectangle = " . $result;
// Another function with parameters and return value
function addNumbers($a, $b) {
$sum = $a + $b;
return $sum;
}
$total = addNumbers(25, 35);
echo "<br>Sum = " . $total;
?>
Output:
Area of rectangle = 50
Sum = 60
calculateArea($length, $width) — A user-defined function that takes two parameters (length and width)return to send the result backcalculateArea(10, 5) — The function is called with arguments 10 and 5$result and displayed using echoFunctions are fundamental building blocks in PHP that promote code reusability and modular programming. By using parameters, we pass data into functions, and by using return values, we get processed results back — making our code clean, organized, and efficient.
Short Answers Questions