Thread: All combinations

  1. #16
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    There's another difference between returns and goto's.

    Goto jumps to a specific point. Return jumps back to the instruction following the one
    that called the function or subroutine. Return can return to various places, depending
    on where it was called from.

  2. #17
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    If order matters then you're after permutations, not combinations.

    Quote Originally Posted by quzah View Post
    The only difference is that returns can only jump backwards.
    I strongly disagree here. You can't even use the word backwards really. I mean the function it returns to could be further on down in the file. But no, you can't call it forwards either. At best you can call it is a jump out. A goto can never do the job of a return as it can with a break or continue. In fact if you go back to the days of basic to a certain pont in history where there were no loop constructs at all, you had goto, gosub and return. Surprise surprise, return is actually the same as the old return.
    Not only that, but where it was in this program (at the end) means that it does not affect control flow at all, and is in fact only used for the part of its job that has nothing to do with control flow at all and that's to provide the result to its caller. Calling the return in this program a goto, was a ridiculous stretch of the imagination by anyone's standards. And if that's a goto then there's one in every C program.
    Last edited by iMalc; 10-14-2011 at 08:53 PM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #18
    Registered User
    Join Date
    Sep 2011
    Posts
    117
    look liek teh formula I need to be using is: n!/(n-r)!

    That look about right? Now to the coding!

  4. #19
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by iMalc View Post
    And if that's a goto then there's one in every C program.
    That was my entire point. Every programmer that complains about gotos making code hard to read is simply glossing over the fact that they use things that do the exact same thing all the time.

    As far as your disagreement to it being a jump backwards, that's fine. You're wrong, but it's fine for you to think that way. Consider the stack. Everything is on the stack (we're ignoring dynamic memory allocations, and are just talking about instructions). The stack is basically your C file flipped upside down.

    So what does return do? It drops back down the stack to wherever your function call came from. What's that mean? It means you jumped backwards, tossing away everything above you, and overwriting the stack with new instructions. (Yeah yeah, you can shuffle things around and ruin my upside-down-source-code analogy if you really want to, but the concept holds true.)

    So whenever you hit a return, you're jumping backwards to the point where the function was called. A label is in effect a function name, and a goto is in effect a return.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #20
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    I don't program assembly on petiums, but I believe the stack only stores the return address
    for the function. Not the executable code.

    The calling code pushes the return address onto the stack, the address of the next instruction
    following the JSR (jump to subroutine). The subroutine, or function pulls the return address
    off the stack and then jumps (returns) there.

  6. #21
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by megafiddle View Post
    I don't program assembly on petiums, but I believe the stack only stores the return address
    for the function. Not the executable code.

    The calling code pushes the return address onto the stack, the address of the next instruction
    following the JSR (jump to subroutine). The subroutine, or function pulls the return address
    off the stack and then jumps (returns) there.
    If you must know...
    Code:
    return address
    parameter1
    parameter2
    etc.
    local 1
    local 2
    etc.
    push1 
    push2
    etc.
    ... at the end of the function it merely resets sp to the address on entry and there's the return address. The rest of it is abandoned to be overwritten by the next function call. The return value is actually in a CPU register.

    There are different protocals STDCALL, CDECL, FASTCALL etc... you can look these up for exact detail....

  7. #22
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    Yes, the stack stores more than just the return address.

    I meant that the function itself is not stored on the stack. The function could conceivably
    be anywhere in memory, and the calling code code also be anywhere. The "jump" that
    is involved in the return is different from a goto (or JMP) statement. A JMP is to a specific
    address or indirect address.

    You could easily implement a 'for' or 'while' loop using goto's. Quite a bit different though
    to implement a subroutine type return with a goto.
    Last edited by megafiddle; 10-14-2011 at 11:57 PM.

  8. #23
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by megafiddle View Post
    Quite a bit different though to implement a subroutine type return with a goto.
    Well then I guess it's a good thing no one was comparing returning a value to a goto then, huh?
    Quote Originally Posted by me
    Conceptually they are identical, except for the part where return can also bring along a value with it.

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #24
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by megafiddle View Post
    Yes, the stack stores more than just the return address.

    I meant that the function itself is not stored on the stack. The function could conceivably
    be anywhere in memory, and the calling code code also be anywhere. The "jump" that
    is involved in the return is different from a goto (or JMP) statement. A JMP is to a specific
    address or indirect address.

    You could easily implement a 'for' or 'while' loop using goto's. Quite a bit different though
    to implement a subroutine type return with a goto.
    Correct... the code is not on the stack... only the function's values end up there.

    JMP and it's cousins... do not store a return address... CALL and it's cousins do...

    Thus... subroutine != jump.

  10. #25
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by quzah View Post
    Well then I guess it's a good thing no one was comparing returning a value to a goto then, huh?
    Quzah.
    Actually... at the asm level, it is possible to load up a few registers then do a JMP instruction to code that operates on the values in the registers, just like a function call would. However; this functionality is not implemented in C ... (perhaps because it didn't exist back in the 70's when C was hatched...)

  11. #26
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    Quote Originally Posted by quzah View Post
    Well then I guess it's a good thing no one was comparing returning a value to a goto then, huh?
    Quzah.
    But what about functions that don't return anything?

    How would you implement that with a goto instead of a return?

  12. #27
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by megafiddle View Post
    But what about functions that don't return anything?

    How would you implement that with a goto instead of a return?
    I wasn't trying to implement a function as a goto. But you could. Stick everything in main, then when you want to get where you're going, just stick a goto and get there.


    Quzah.
    Hope is the first step on the road to disappointment.

  13. #28
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by quzah View Post
    I wasn't trying to implement a function as a goto. But you could. Stick everything in main, then when you want to get where you're going, just stick a goto and get there.
    Quzah.
    Right back to good old GWBasic... 1000 Goto 900....

  14. #29
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    How do you get back, though, If you jumped there from different places?

    edited: (double post) see next post
    Last edited by megafiddle; 10-15-2011 at 12:58 AM.

  15. #30
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    But then how do you get back? How do you know where that code was goto'ed from?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with combinations !!!!
    By ayan_2587 in forum C Programming
    Replies: 2
    Last Post: 12-12-2009, 12:45 PM
  2. Combinations
    By Cmuppet in forum C Programming
    Replies: 6
    Last Post: 10-19-2004, 07:39 AM
  3. Combinations, lotto, 6/49
    By Robert in forum C++ Programming
    Replies: 8
    Last Post: 12-06-2002, 07:27 PM
  4. key press combinations
    By Trauts in forum C++ Programming
    Replies: 2
    Last Post: 10-01-2002, 12:15 PM
  5. Combinations
    By GaPe in forum C Programming
    Replies: 16
    Last Post: 01-09-2002, 05:38 AM