0
$\begingroup$

For this question, I thought function F called twice but it called three times. Are those three functions were called? F(N), F(K) and F(N-1)?

How many times in this pseudo-code is the function F called?

Main
    Declare K as Integer
    K = 3
    Set Result = F(K)
    Write Result
End Program

Function F(N) as Integer
        If N == 1 Then
Set F = 1
    Else
        Set F = N * F(N - 1)
        Set N = N - 1
    End If
End Function
$\endgroup$
  • $\begingroup$ The question most likely is not How many calls to F() can you count in the code presented?, but How many times is the function F called in an execution of this pseudocode? $\endgroup$ – greybeard Mar 27 at 17:38
0
$\begingroup$

The function is called 3 times. F(3) F(2) F(1)

| cite | improve this answer | |
$\endgroup$
  • $\begingroup$ Thank you for your reply. Are the F(N), F(K) and f(N-1) function called? $\endgroup$ – Kijimu7 Mar 27 at 16:18
0
$\begingroup$

The pseudocode calls the F 3 times

| cite | improve this answer | |
$\endgroup$
  • 2
    $\begingroup$ I don't understand what this adds over the existing answers. We prefer answers that come with explanations, rationale, justification, and/or proofs. $\endgroup$ – D.W. Apr 26 at 20:15
0
$\begingroup$

To understand what is happening you have to execute the code "on paper". One way of showing this is to number the lines of code so we can see what is happening:

01 Main
02    Declare K as Integer
03    K = 3
04    Set Result = F(K)
06    Write Result
07 End Program
08
09 Function F(N) as Integer
10    If N == 1 Then
11        Set F = 1
12    Else
13        Set F = N * F(N - 1)
14        Set N = N - 1
15    End If
16 End Function

Now if we work through the code one line at a time and record the values in the variables we can see what is happening:

Line Code                       K    N   Result
01   Main
02   Declare K as Integer       ?
03   K - 3                      3
04   Set Result = F(3)          3          ?
09   Function F(3) as Integer        3
10   If 3 == 1 Then                  3
13   Set F = 3 * F(2)                3
09   Function F(2) as Integer        2
10   If 2 == 1 Then                  2
13   Set F = 2 * F(1)                2
09   Function F(1) as Integer        1
10   If 1 == 1 Then                  1
13   Set F = 1                       1
15   End If                          1
16   End Function                    1
14   Set N = 2 - 1                   1
15   End If                          2
16   End Function                    2
14   Set N = 3 - 1                   2
15   End If                          2
16   End Function                    2
06   Write Result                3        6
07   End Program                 3        6

What we are seeing is recursion in action, with different scopes of the variable N.

You can see in the execution trace three calls of F with the values of 1,2,3. The first comes from Line 4 and the last two from line 9. So F(K) and F(N-1) are the only calls.

| cite | improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.