Welcome to our new forum
All users of the legacy CODESYS Forums, please create a new account at account.codesys.com. But make sure to use the same E-Mail address as in the old Forum. Then your posts will be matched. Close

how to define a function

keobe
2014-07-05
2014-07-09
  • keobe - 2014-07-05

    Hi
    I like to define a function named myFunction with input parameters a,b,and c

    I wrote the function like this

    FUNCTION myFunction : INT
    VAR_INPUT
    a:INT;
    b:INT;
    c:INT;
    END_VAR
    VAR
    x:INT;
    END_VAR

    in the function body
    I wrote
    x:=a+b+c;

    when i call the function, I got the output =0, can someone pls help me why I got the answer =0?

    my prg is

    PROGRAM PLC_PRG
    VAR
    mycallOutput:INT;
    x:INT;
    y:INT;
    z:INT;

    END_VAR

    prog:

    x:=4;
    y:=5;
    z:=6;
    mycallOutput:=myFunction(x,y,z);

    result is mycallOutput =0;

     
  • shooter - 2014-07-06

    as the function has no idea what the output is you will have to define it.
    so your x should be myfunction

     
  • liuyongzhi - 2014-07-09

    I suggest in PRG:

    VAR
    mycallOutput : INT;
    onefunction: myFunction; //POU type of myFunction
    END_VAR

    in code body:

    mycallOutput : = onefunction(x, y, z);

    The way you are doing is using a POU type for processing, in fact no function instance is created, so there is output is default 0. Hope Codesys can explain the compiling process. I think the program should not pass compilation.

    Yongzhi

     
  • shooter - 2014-07-09

    FUNCTION myFunction : INT
    VAR_INPUT
    a:INT;
    b:INT;
    c:INT;
    END_VAR
    VAR

    END_VAR

    in the function body
    I wrote
    myfunction :=a+b+c; <<<<<<<<<<

     

Log in to post a comment.