HP Forums
Using function in program - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: HP Prime (/forum-5.html)
+--- Thread: Using function in program (/thread-531.html)



Using function in program - Graan - 01-26-2014 06:56 PM

Im trying to use a function in a program. Always getting some syntax error. Just trying to send 2 variables to a function and add them , then sending the result back . The function only needs to be accessible from the main program. Probably some error with declarations or something, but I don't get it.

Code:

Func3();

EXPORT Test()
BEGIN
LOCAL a;

a := Func3(3,4);

END;

Func3(a,b);
LOCAL a,b,c;
c := a + b;
RESULT(c);
END;



RE: Using function in program - eried - 01-26-2014 07:25 PM

Remove the ; from the func declaration, and add BEGIN


RE: Using function in program - Dougggg - 01-26-2014 07:27 PM

I think you need a BEGIN and END in the function and I dont think you need the a,b variables in Local and I believe result should be RETURN (c)


RE: Using function in program - Tim Wessman - 01-26-2014 07:36 PM

I will show 3 versions. All of them will behave identically.

Code:
Func3(a,b)
begin
local c;
c := a + b;
return c;
end;

Code:
Func3(a,b)
begin
local c;
c := a + b;
end;


Code:
Func3(a,b)
begin
a + b;
end;

Note that the reason these all behave the same is that there is *always* an implicit return on the last item in the runstream of the program. A function must return a single result. That result might not be used for anything, but it must return at least one thing.


RE: Using function in program - Graan - 01-26-2014 09:27 PM

Ok Func3(a,b); was wrong (no semicolon) and I needed a BEGIN. RESULT is pascal syntax should be: return. Seems as return c; and return(c); both works.

Thank you for the replies.


RE: Using function in program - Han - 01-26-2014 09:28 PM

When creating a function with input variables, those variables are implicitly declared as local. Therefore, you would not use a LOCAL statement with them. For example,

Code:

EXPORT MYFUNC(a,b)
BEGIN
  RETURN(a+b);
END;

The variables a and b do not need a LOCAL a,b statement inside the BEGIN END pair.