1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

issue to have "if" inside function

Discussion in 'Support' started by justsoso8, Oct 25, 2011.

  1. justsoso8

    Joined:
    Oct 25, 2011
    Messages:
    2
    Likes Received:
    0
    Dear sir,

    I'm now using your latest TCMD 13.00.27 x64 for evaluation.

    The following command runs well, function is defined, and the function is executed
    >>function test=`echo done` %+ function test %+ %@test[]
    echo done
    done

    also following conditional command executed well
    >>if 1==1 echo done
    done

    but if i combine them into one function:
    >> function test=`if 1==1 echo done` %+ function test %+ %@test[]
    if 1==1 echo done

    It seems the function is defined as expected, but doesn't have expected result.

    Any help from you?
     
  2. Charles Dye

    Charles Dye Super Moderator
    Staff Member

    Joined:
    May 20, 2008
    Messages:
    3,307
    Likes Received:
    39
    I'm not sure what you're trying to accomplish with that. The FUNCTION command is used to define, well... functions. Functions return values; they are generally used as arguments to commands. It's pretty rare to have a function return a command as a value.

    If you want to create a new command, use ALIAS. If, on the other hand, you want the function to return different values depending on a condition, that's what @IF[] is for.

    What are you trying to do?
     
  3. justsoso8

    Joined:
    Oct 25, 2011
    Messages:
    2
    Likes Received:
    0
    What I want do is to deine a function do sth depends on the condition, and finally return a result.
    My issue is: if there's an "if" inside function defination, the function can not execuate any futher. here, "echo" is just an example, since it's easy to get the feedback.

    I'm not sure if i expressed my idea clearly.
     
  4. vefatica

    Joined:
    May 20, 2008
    Messages:
    7,954
    Likes Received:
    30
    On Tue, 25 Oct 2011 22:56:45 -0400, justsoso8 <> wrote:

    |What I want do is to deine a function do sth and finally return a result.
    |My issue is: if there's an "if" inside function defination, the function can not execuate any futher.

    It is very difficult and often impossible to execute commands in a function.
    Functions should, in general, test/evaluate things and create a string (not echo
    it); the function will be replaced by this string when you use it. The best way
    to make decisions in a function if with @IF[]. Here's an example.

    Code:
    v:\> function parity `%@if[%@eval[%1 MOD 2] == 1,odd,even]`
    
    v:\> echo %@parity[3]
    odd
    
    v:\> echo %@parity[4]
    even
    Here's one a little like your original example

    Code:
    v:\> function test `%@if[%1 == 1,done,not done]`
    
    v:\> echo %@test[1]
    done
    
    v:\> echo %@test[2]
    not done
     

Share This Page