What is the difference between these 2 codes | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What is the difference between these 2 codes

// code1 <?php function mult($num1, $num2) { $res = $num1 * $num2; echo $res; } mult(8, 3); ?> // code2 <?php function mult($num1, $num2) { $res = $num1 * $num2; return $res; } echo mult(8, 3); ?> what is the point of "return" ?!

1st Jan 2017, 6:18 PM
Rami Magdi
Rami Magdi - avatar
2 Answers
+ 2
returns gives you the value. The result of the function. So with the return mult, you can store it in other variable, or even do $res = mult(mult(2,2),2) so the inside mult will be 4, and because it was returned, the expresion goes to mult(4,2). so $res is 8.
1st Jan 2017, 6:39 PM
Nahuel
Nahuel - avatar
0
in code2 the function returns the result to the point of call. so you can print outside the function, or do other things with that result value.
1st Jan 2017, 8:35 PM
ifl
ifl - avatar