Junior
How to calculate the result of a function if it performs many different actions inside?
sobes.tech AI
Answer from AI
Use the function's return value (return).
<?php
// Function that performs various actions and returns a result
function calculateComplexResult(int $a, int $b): int
{
$intermediate_result = $a * $b; // First action
$final_result = $intermediate_result + 10; // Second action
return $final_result; // Return the final result
}
$x = 5;
$y = 3;
$result = calculateComplexResult($x, $y); // Call the function and store the return value
// Now the variable $result contains the result of the function execution
echo "Calculation result: " . $result;