Results 1 to 16 of 16

Thread: Function "Result:=" Help

  1. #1
    Join Date
    Apr 2009
    Posts
    6
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default Function "Result:=" Help

    Well, here's what I wanted to know:
    Is it possible to have...Actually I'll post the code first so you can see an example:
    Code:
    function ValidateText(ValidType: Integer): Boolean;
    begin
      Result := False;
      case ValidType of
        0:  if (ValidateText1 and ValidateText2) then
              Result := True;
        1:  if ValidateText1 then
              Result := True;
        2:  if ValidateText2 then
              Result := True;
      end;
    end;
    I was wondering if it was possible that I could possibly have the "Result" variable already declared in the function but yet still change it through the function without the script assuming that I've already told it the value of "Result" and then basically ignoring all other statements in the actual function and go on to another procedure/function.

  2. #2
    Join Date
    Mar 2008
    Location
    New Jersey
    Posts
    1,673
    Mentioned
    1 Post(s)
    Quoted
    9 Post(s)

    Default

    yea you can, but to change it to true then go on to the next procedure/function, you pretty much have it, except you should add an exit; after each Result:= True;. You also need begins and ends after each case if there is more then one line in it, like this:

    SCAR Code:
    function ValidateText(ValidType: Integer): Boolean;
    begin
      Result := False;
      case ValidType of

        0: begin
              if (ValidateText1 and ValidateText2) then
              begin
                Result := True;
                exit;
              end;    
            end;

        1: begin
              if ValidateText1 then
              begin
                Result := True;
                exit;
              end;  
            end;

        2: begin
              if ValidateText2 then
              begin
                Result := True;
                exit;
              end;  
            end;

      end;
    end;


    same with the ifs/thens since I added exit;
    Last edited by Baked0420; 04-08-2009 at 09:33 PM.

  3. #3
    Join Date
    Apr 2009
    Posts
    6
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by Baked0420 View Post
    yea you can, but to change it to true then go on to the next procedure/function, you pretty much have it, except you should add an exit; after each Result:= True;. You also need begins and ends after each case if there is more then one line in it, like this:

    Thanks but I really don't think I need to add an Exit; considering that only the integer can trigger the statements and the case will ultimately use only one out of the list.

  4. #4
    Join Date
    Mar 2008
    Location
    New Jersey
    Posts
    1,673
    Mentioned
    1 Post(s)
    Quoted
    9 Post(s)

    Default

    that's true TooMuch, you don't really need it, but I like to have it anyway as a just incase, I don't know, I just prefer using exit;'s when dealing with functions, my preference. But anyway yes you can change the result in the middle of the function if that's what you were asking. And you still need to add begins and ends to after the 0:, 1:, and 2: because you have more than one line after each.

  5. #5
    Join Date
    Apr 2009
    Posts
    6
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by Baked0420 View Post
    that's true TooMuch, you don't really need it, but I like to have it anyway as a just incase, I don't know, I just prefer using exit;'s when dealing with functions, my preference. But anyway yes you can change the result in the middle of the function if that's what you were asking. And you still need to add begins and ends to after the 0:, 1:, and 2: because you have more than one line after each.
    I just checked, it compiles perfectly without error, an If Statement is counted as one "line" or statement rather than 2 (the if statement and the other statement that follows).
    Last edited by TooMuch; 04-08-2009 at 10:10 PM.

  6. #6
    Join Date
    Jan 2007
    Location
    Kansas
    Posts
    3,760
    Mentioned
    1 Post(s)
    Quoted
    3 Post(s)

    Default

    You can speed up your code with Exits because it doesn't have to check more conditions after it's found the one it needs.


  7. #7
    Join Date
    Jun 2006
    Posts
    3,861
    Mentioned
    3 Post(s)
    Quoted
    1 Post(s)

    Default

    Quote Originally Posted by Bobarkinator View Post
    You can speed up your code with Exits because it doesn't have to check more conditions after it's found the one it needs.
    Once an item on the list is true, it automatically exits the case statement.

  8. #8
    Join Date
    Jan 2007
    Location
    Kansas
    Posts
    3,760
    Mentioned
    1 Post(s)
    Quoted
    3 Post(s)

    Default

    Quote Originally Posted by bullzeye95 View Post
    Once an item on the list is true, it automatically exits the case statement.
    Since when? What if I want my value to match 2 of my case statements


  9. #9
    Join Date
    Jun 2006
    Posts
    3,861
    Mentioned
    3 Post(s)
    Quoted
    1 Post(s)

    Default

    Quote Originally Posted by Bobarkinator View Post
    Since when? What if I want my value to match 2 of my case statements
    Then use switch. On another language.

  10. #10
    Join Date
    Mar 2008
    Location
    New Jersey
    Posts
    1,673
    Mentioned
    1 Post(s)
    Quoted
    9 Post(s)

    Default

    bullzeye I dont think it automatically exits when you call Result:= True, and TooMuch, you are correct, you dont need a begin or end where I said like after the 0, 1, or 2, I'm sorry, don't know why I thought that, but it wouldn't give an error if you did put a begin end where I said.

    EDIT: Nevermind bullzeye, you might be right, I just noticed you said it exits the case statement, that might be true, but I thought you meant it exits the whole function once Result = True.
    Last edited by Baked0420; 04-09-2009 at 03:09 AM.

  11. #11
    Join Date
    Mar 2006
    Location
    United States, -7:00 GMT
    Posts
    1,790
    Mentioned
    2 Post(s)
    Quoted
    2 Post(s)

    Default

    Isn't there a xor function, that can be Test1 xor Test2, therefore you would just need that? (and/or)

    Maybe I'm off, I have no idea

    EDIT: Woops. I misread and didn't see some stuff. Forget that.

    I think Bullzeye is wrong too! haha
    Last edited by Ejjman; 04-09-2009 at 03:17 AM.

    hakuna matata ;)

  12. #12
    Join Date
    Mar 2008
    Location
    New Jersey
    Posts
    1,673
    Mentioned
    1 Post(s)
    Quoted
    9 Post(s)

    Default

    xor means only one is true and not the other, like if(2+2=5 xor 2+2=4) then would let you do the next thing in the if/then, but an if(2+2=4 xor 2+3=5) then would not let you do the next thing in the if/then, because they both are true. Same if they both are false, then it won't do the next thing in the if/then either. It has to be one true and one false. And like you already know, and means both are true, so for his case 0: using xor is not the same thing as and.

  13. #13
    Join Date
    Apr 2009
    Posts
    6
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by Baked0420 View Post
    xor means only one is true and not the other, like if(2+2=5 xor 2+2=4) then would let you do the next thing in the if/then, but an if(2+2=4 xor 2+3=5) then would not let you do the next thing in the if/then, because they both are true. Same if they both are false, then it won't do the next thing in the if/then either. It has to be one true and one false. And like you already know, and means both are true, so for his case 0: using xor is not the same thing as and.
    Thanks for telling me about xor although it doesn't work due to the fact the function has to be very specific on which one of the ValidateTexts is true.

    In case you were wondering what it's for:
    In another procedure it will detect if both ValidateText1 and ValidateText2 are true if not it would move on to check which one of the variables are actually true.
    Last edited by TooMuch; 04-09-2009 at 04:35 AM.

  14. #14
    Join Date
    Jun 2006
    Posts
    3,861
    Mentioned
    3 Post(s)
    Quoted
    1 Post(s)

    Default

    @ejjman and baked, I meant that if you have a case statement, and an item on its list is true, it will exit when it is done with that item. Example:
    SCAR Code:
    case True of
      x > 1: writeln('1');
      x > 2: writeln('2');
      x > 3: writeln('3');
    end;
    If x is 5, all of those are true. However, it will exit the case statement after the first one (x > 1).

  15. #15
    Join Date
    Jun 2007
    Location
    #SRL
    Posts
    200
    Mentioned
    2 Post(s)
    Quoted
    1 Post(s)

    Default

    you don't really even need those if..then statements, just setting Result to the result values of the other functions will be fine:

    SCAR Code:
    function ValidateText(ValidType: Integer): Boolean;
    begin //Result defaults at False, so there's no need to set it
      case ValidType of
        0:  Result:= ValidateText1 and ValidateText2; //since doing "if (ValidateText1 and ValidateText2) then" is really saying "if((ValidateText1 and ValidateText2) = True) then", you might as well just set that True/False straight to Result, instead of calling a whole other line
        1:  Result:= ValidateText1; //see above
        2:  Result:= ValidateText2;
      end;
    end;
    Quote Originally Posted by IRC
    <Wizzup> 5
    <Wizzup> 4
    <Wizzup> 43
    <Wizzup> shit
    <Wizzup> 5
    <Wizzup> 4
    <Wizzup> 3
    <Wizzup> 1
    <Wizzup> offblast

  16. #16
    Join Date
    Apr 2009
    Posts
    6
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Okay Tootoot I never knew that, some of my other functions have that, I'll change that now

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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