Well, in my humble opinion, the teacher has his/her terms confused.
Recursive functions are used when you need to loop thru items that need to call the same routine to solve the calculation. In the two calculations you need to do, they can be done by a simple loop without any need of true recursion.
But, with that said, you can use a recursive function if you really need to. You would set up the script to loop thru the process with a simple loop as I stated, but, use the function parts to call it based on the “n” number. In this case the calling line would just be function_name(10); to make the code run up to n=10. Hope that makes sense to you. So, you could do something like this for the first example:
<?php
// Recursive Function Example ( Set beginning total to 1 so no multiplying by zero! )
function recursive1($tot, $n) {
// If the current number is less the target, process it...
if($n != 0){
// Call the function again. decrement number by one.
return recursive1($tot * $n, $n-1);
} else {
return $tot;
}
}
//Call our recursive function.
echo "Final total for Recursion #1 : " . recursive1(1, 10);
?>
I think this will show you how to recurse back to the same function. But, remember, when you loop like this, it takes a lot of server time if the function is complicated. One common use for this type of code is to parse thru folder structures. You can look thru folders saved inside other folders and create tress of them for use in various displays or databases. For simple calculations like this a simple FOR clause would work faster. But, they are attempting to teach you about recursion, so there you go. Hope this helps!