Website Development Prices

Search Blog

Sunday, April 17, 2016

Vracanje vrednosti iz funkcija (Returning values from functions)

Funkcije su napravljene da bi vracale vrednosti. Do sada smo koristili ugradjene PHP funkcije koje su vracale vrednosti. 

Ako zelite da iz funkcije vratite vrednost, mozete da koristite iskaz return.

Primer:

return (vrednost);

Objasnjenje: zagrade koje cine da return izgleda kao funkcija, iako je rec o iskazu, nisu obavezne. 

Primer 2: PHP nema funkciju za dizanje na kvadrat, ali mozete sami da je napravite.


<?php

function na_kvadrat($vrednost)
{
return $vrednost * $vrednost;

}

?>

Nastavak primera 2: pozivanje funkcije.

<?php

function na_kvadrat($vrednost)
{
return $vrednost * $vrednost;
}


echo "Koliko je 15 x 15? Rezultat je ", na_kvadrat(15) , ".";

?>

Rezultat:

Koliko je 15 x 15? Rezultat je 225.

Takodje, mozete da vracate vrednost tipa boolean.

Primer 3: proveravamo cenu motora, pomocu funkcije proveri_cenu.

<?php

$cena_motora = 2700;

if(proveri_cenu($cena_motora)) {
echo "Cena motora nije velika.";
}

else {
echo "Cena motora je velika.";
}

function proveri_cenu($cena) 
{
$vrati_cenu = false;
if($cena > 2500 && $cena < 3500){
$vrati_cenu = true;
}
return $vrati_cenu;
}

?>

Rezultat:

Cena motora nije velika.

U funkciji moze da postoji vise iskaza return. Vodite racuna da, kada se izvrsi iskaz return, dolazi do napustanja funkcije i vracanja u kod koji je pozvao funkciju. 

Primer 4: primer 3 sa dva iskaza return.

<?php

$cena_motora = 2700;

if(proveri_cenu($cena_motora)) {
echo "Cena motora nije velika.";
}

else {
echo "Cena motora je velika.";
}

function proveri_cenu($cena) {
if($cena > 2500 && $cena < 3500){
return true;
}
return false;

}

?>

Rezultat:

Cena motora nije velika.

Pomocu iskaza return, mozete funkcije koristiti za obradu podataka i slanje rezultata nazad do koda koji je pozvao. Ovo je korisno kada koristite funkcije za podelu koda i kada zelite da svaka funkcija vrati rezultat onog sto je interno uradila. 

The functions are designed to return value. So far we have used the built-in PHP functions that are returning values.

If you want to return the value from the function, you can use the return statement.

Example:

return (value);

Explanation: brackets that make return look like a function, although it is about the statement, are not required.

Example 2: PHP doesn't have square number, but you can make yourself.

<?php

function square($value)
{
return $value * $value;

}

?>

Continued example 2: calling function.

<?php

/* english 2
function square($value)
{
return $value * $value;
}


echo "How much is 15 x 15? Result is ", square(15), ".";

?>

Result:

How much is 15 x 15? Result is 225.

You can also return the value type boolean.

Example 3: checking the cost of the motorcycle with the function check_price.

<?php

$motorcycle_price = 2700;

if(check_price($motorcycle_price)) {
echo "Motorcycle price is not big.";
}

else {
echo "Motorcycle price is big.";
}

function check_price($price) 
{
$return_price = false;
if($price > 2500 && $price < 3500){
$return_price = true;
}
return $return_price;

}

?>

Result:

Motorcycle price is not big.

In the function may be more than one return statement. Beware that when you execute a return statement, comes to leaving the function and return to the code that called the function.

Example 4: example 3 with two return statements.

<?php

$motorcycle_price = 2700;

if(check_price($motorcycle_price)) {
echo "Motorcycle price is not big.";
}

else {
echo "Motorcycle price is big.";
}

function check_price($price) {
if($price > 2500 && $price < 3500){
return true;
}
return false;

}

?>

Result:

Motorcycle price is not big.


Using the return statement, the functions can be used to process the data and send the results back to the code that is called. This is useful when you are using the function for dividing code and when you want that each function returns the result of what was done internally.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.