+ Reply to Thread
Results 1 to 4 of 4

Thread: coding is fine,but i did not understood how it works

  1. #1
    Join Date
    Nov 2008
    Location
    Hyderabad
    Age
    26
    Posts
    56
    Rep Power
    9

    Default coding is fine,but i did not understood how it works

    reversing of string, output is comming correct.

    FUNCTION rev RETURNS CHAR(INPUT ll AS CHAR).
    IF LENGTH(ll) = 1 THEN
    RETURN ll.
    ELSE
    DO:

    RETURN rev(SUBSTRING(ll,2)) + SUBSTRING(ll,1,1).
    END.
    END.
    DISP rev("shireesh").


    can any one explain me how it works.
    Last edited by shireeshn; 2 Oct 2009 at 10:50 PM.

  2. #2
    Casper is offline ProgressTalk.com Moderator Casper is a jewel in the rough Casper is a jewel in the rough Casper is a jewel in the rough
    Join Date
    Feb 2004
    Age
    40
    Posts
    1,654
    Rep Power
    132

    Default Re: coding is fine,but i did not understood how it works

    ah good old recursive function :-)

    Maybe you understand it better if you run this. You get a message at each stage the return value is formed.

    Code:
    DEFINE VARIABLE cTmp AS CHARACTER   NO-UNDO.
    FUNCTION rev RETURNS CHAR(INPUT ll AS CHAR).
        MESSAGE cTmp
            VIEW-AS ALERT-BOX INFO BUTTONS OK.
        IF LENGTH(ll) = 1
        THEN DO:
            ASSIGN cTmp  = ll + cTmp.
            MESSAGE 'finally: ' + cTmp
                VIEW-AS ALERT-BOX INFO BUTTONS OK.
            RETURN ll.
        END.
        ELSE do:
            ASSIGN cTmp = SUBSTRING(ll,1,1) + cTmp.
            RETURN rev(SUBSTRING(ll,2)) + SUBSTRING(ll,1,1).
        END.
    END.
    DISP rev("shireesh") cTmp.
    In essence the function is called until the length of the input value is 1.
    To show that, I introduced a variable cTmp which shows how the final return value is formed.
    As you can see the function calls itself until the length of the input value is 1 and then it returns to the caller.

    I hope this clarifies the function a bit more.

    Casper.

  3. #3
    Join Date
    Apr 2008
    Posts
    10
    Rep Power
    7

    Default Re: coding is fine,but i did not understood how it works

    It is using a recursive method to concatenate the string
    The function is calling itself repeatedly until it is finished.

    There are two cases: It has finished (the return ll) or it is not finished.

    In the case it is not yet finished doing the job, it is calling itself each time with one less beginning character (the rev(SUBSTRING(ll,2)) part) and returning this to itself plus the current first character. (the SUBSTRING(ll,1,1) part). The parenthesis placement are important since we only want to pass the remaining portion of the string back to itself until it is finished.

    Do notice that substring is called two different ways, once to get exactly one character. (the current first character) and the second way with only one numeric argument to get the string from position two and on (substring( ll,2))

    Better explanation of recursion:
    http://en.wikipedia.org/wiki/Recursi...mputer_science

  4. #4
    Join Date
    Nov 2008
    Location
    Hyderabad
    Age
    26
    Posts
    56
    Rep Power
    9

    Default Re: coding is fine,but i did not understood how it works

    casper it not clear fully, ur program is clear but in this statment

    RETURN rev(SUBSTRING(ll,2)) + SUBSTRING(ll,1,1).

    1) in every statment is it returns some value or calling function again or both ?

    2) where does this string get added ?

+ Reply to Thread

Similar Threads

  1. BEGINS how does it works??
    By remcoCaesar in forum Development
    Replies: 2
    Last Post: 2 Sep 2009, 12:49 AM
  2. X-coding in UTF-8
    By piotrk in forum Roundtable TSMS
    Replies: 0
    Last Post: 7 Apr 2004, 12:50 AM
  3. ODBC ASP and the works!!! HELP!!! PLEASE!!!
    By murphsl in forum SQL-92
    Replies: 5
    Last Post: 1 Jul 2002, 04:11 PM
  4. How this relationship works?
    By J V in forum QAD's MFG/PRO
    Replies: 2
    Last Post: 1 Nov 2001, 04:07 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts