Tuesday, April 22, 2025

PHP Return Values

You are advised to check corresponding YouTube video at the end of this article.

A return value is the value that a function or method returns after performing its operations or calculations. When a function is called, it can execute a series of instructions and then return a value to the caller, which can then be used in the program.

Return values are important because they allow you to get output from a function or method and use it in your program. They also allow you to make decisions and perform operations based on the output of a function or method.

In most programming languages, you can use the "return" keyword to specify the return value of a function. 


<?php 

function addition($x, $y) {
	$result = $x + $y;
	
	return $result;	
}

//Function ok, but we are not using return at all
addition(5, 6);

//Let's use return
echo addition(5, 6);
echo "<br>";

echo addition(3, 6);
echo "<br>";

echo addition(1, 7);
echo "<br>";

?>

Tthe function addition accepts two parameters and calculates their sum.

Using the return statement to return a value from a function is important because it allows the value to be used later in the program. In this example, without the return statement, the calculated result would be lost and could not be used elsewhere in the program.

By using return, the value can be stored in a variable, printed to the screen, or used in any other way that is necessary.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...