3.8k 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 | 3.8k 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
+2

Hint: Keep track of the stack and you'll reach fun(fun(111))

And fun(111) returns 101.

Substitute this, and we get fun(101). Now, IF will be true, not ELSE (up to this point, each time ELSE was true => recursive call. But now ELSE isn't true anymore)

Which makes fun(101) set the value as 91.

There are no more recursive calls, the execution halts here. 91 is the correct answer.

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.$

Correct Answer: $C$

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
0
fun = x – 10

how can u say that this is calling fun(x-10).........is it pascal syntax?

0
There is no termination condition.
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.
by
0
how can we understand this is stop codition.
0
Because when value is 111, there are no further function call. Just the return.
Value returned by
fun(95) = fun(fun(106))
= fun(96)
= fun(fun(107))
= fun(97)
= fun(fun(108))
= fun(98)
= fun(fun(109))
= fun(99)
= fun(110)
= fun(100)
= fun(fun(111))
= fun(101)
= 91
by