Click here to Skip to main content
15,887,477 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Can someone help? What doe this function return?
C#
function (return_number) {
!temp a,,i2
!temp b,,i2
!temp c,,i2
!temp i,,i2

    tmp.b=5
    tmp.a=2
    tmp.c = tmp.b - tmp.a
    if (tmp.c == 2) {
        tmp.c = 5
    } else {
        tmp.b = 7
    }

    for (tmp.i=0; tmp.i < tmp.c; tmp.i=tmp.i+1) {
        while (tmp.i>4) {
            if (tmp.i<7) {
                return tmp.i
            }
        }
    }
    return tmp.i
}
Posted
Updated 17-Apr-12 2:54am
v3
Comments
Sergey Alexandrovich Kryukov 17-Apr-12 0:38am    
First of all, what language is this? Why all those language tags? Is it because you don't know yourself? :-)
--SA
JF2015 17-Apr-12 2:06am    
This isn't even any type of C (C, C++, C#). So, please tag your question with the correct programming language.

Let's start by breaking this down. First of all, I assume that the following section is defining some integers:
C#
!temp a,,i2
!temp b,,i2
!temp c,,i2
!temp i,,i2
Next, you are assigning values to them (and I assume that your initial use of temp is a mistype, as you change this to tmp further on). So, you set b to 5 and a to 2; then you subtract a from b and store that in c, so c is 3. This means that your if test drops into the tmp.b = 7 section. Following the rest of the logic, tmp.b isn't used anywhere else, and leads to a real logic bomb for you if you expect your while condition to execute.

What is the bomb? Well, assuming your logic is entirely correct, your for test loops from 0 to 2, so you never execute the inner logic of the while loop because tmp.i never reaches the condition where it will trigger. This means that tmp.i stops at 2, and that leads to the value you are returning.

You could have figured all this out for yourself just by writing it out. It's not that hard to work through a problem as simple as this.
 
Share this answer
 
Comments
JackDingler 17-Apr-12 10:59am    
Let us know what grade you got for the answer.
Mohammad A Rahman 17-Apr-12 23:32pm    
5!
I'd say that function returns "one headache and years and years of trouble."

By which I mean: it will for ever be hard to determine whether the function is doing what it is intended to do because none of the variables have meaningful names.

Personally I would approach a mystery function like yours from the direction of "what is it supposed to do?" first, and only then would I seek to determine "does it do what is intended?".
 
Share this answer
 
Comments
Aescleal 17-Apr-12 12:59pm    
Dunno who gave you the 1, but it's pretty good advice in general. Have a 5 to make up for it.

I have a feeling this is a bit of homework or an interview test question so the context won't be there.

Cheers,

Ash
Easiest way to figure his out is to use a trace table or to implement the method and see what it returns.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900