how do I write a recursive function if the recursive call is first?
Joseph
2013-10-19 08:30:35 UTC
I am in AP Computer Programming 2 and we are supposed to write a recursive function where the recursive call comes first. Please I really am confused and I want help. If you want the directions for what I'm supposed to do I'll put them in here.
Three answers:
Michael E
2013-10-19 09:26:54 UTC
I'm not sure what you mean by "first". If you are talking about the order of execution at run-time, the recursive calls MUST come before the non-recursive call. If the last call isn't non-recursive you don't have a function, you have an infinite loop.
If they are talking about "first" as in the order of lines in the code, do they mean something like
Function Factorial(N as Integer)
If N <> 0 Then
Factorial = Factorial(N - 1)
Else
Factorial = 1
End If
End Function
as opposed to
Function Factorial(N as Integer)
If N = 0 Then
Factorial = 1
Else
Factorial = Factorial(N - 1)
End If
End Function
James Bond
2013-10-19 09:27:53 UTC
It all depends on your problem.
First, you have to identify recursive relation in your problem based on that only recursive function will be written
modulo_function
2013-10-19 10:21:45 UTC
I, like the others, do not understand your question. I'm fairly good at recursion and can probably help you.
ⓘ
This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.