2.5k views

What value would the following function return for the input $x=95$?

Function fun (x:integer):integer;
Begin
If x > 100 then fun = x – 10
Else fun = fun(fun (x+11))
End;

1. $89$
2. $90$
3. $91$
4. $92$
edited | 2.5k views
+3
Unless you tell what is starting point , how can we tell where recursion stops ?
0
Yes elemenation condition is not there l. But we can take help of options

Value returned by $\text{fun}(95) = \text{fun}(\text{fun}(106))$
$\qquad\qquad= \text{fun}(96)$
$\qquad\qquad= \text{fun}(\text{fun}(107))$
$\qquad\qquad= \text{fun}(97)$
$\qquad\qquad= \text{fun}(\text{fun}(108))$
$\qquad\qquad= \text{fun}(98)$
$\qquad\qquad = \text{fun}(\text{fun}(109))$
$\qquad\qquad= \text{fun}(99)$
$\qquad\qquad= \text{fun}(\text{fun}(110))$
$\qquad\qquad= \text{fun}(100)$
$\qquad\qquad= \text{fun}(\text{fun}(111))$
$\qquad\qquad= \text{fun}(101) = 91.$
edited
+1

f(95)
= f(f(106) = f(96)
= f(f(107) = f(97)
= f(f(108) = f(108)
= f(f(109) = f(109)
= f(f(110) = f(100)
= f(f(111) = f(101) = 91

0
@Digvijay what is happening at fun : x-10.
0
thats a typo- corrected..
+2
How did we get when to stop the recursion ?
+1
There is no termination condition. Why are we stopping at 91
Yes 91 is correct.

step 1: when fun=111 -> fun(fun(111))

step 2:  fun=101 ->fun(101)

step 3:As 101>100 fun=fun -10 =101-10=91

step 4: as there is no fun function call remaining ,it will exit.
0
how can we understand this is stop codition.

1
2