I am relatively new to NATURAL and usually work on the mainframe. Exploring the features of NAT 6.1.1 for Windows though, I detected user-defined functions. As the manual stated, you can avoid the hassle with temporary variables by using function-calls. So I tried to put the manual-example to some use and coded the following:
DEFINE DATA LOCAL
01 #ARRAY (I4/0:9) INIT < 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 >
01 #TEMP (I4)
END-DEFINE
*
INCLUDE C#ADD
WRITE #ADD(< 2,3 >) /* this works
*
#TEMP := #ADD(< 2,3 >)
WRITE #ARRAY(#TEMP) /* this works
*
WRITE 'array-element 2 + 3' #ARRAY(#ADD(< 2,3 >)) /* this doesn't
*
END
So in cases like that I cannot avoid the use of temporary variables?
Here’s the code of the NAT-Objects - exactly as in the manual:
/* Natural function definition
DEFINE FUNCTION #ADD
RETURNS (I4) BY VALUE
DEFINE DATA PARAMETER
1 #SUMMAND1 (I4) BY VALUE
1 #SUMMAND2 (I4) BY VALUE
END-DEFINE
#ADD := #SUMMAND1 + #SUMMAND2
END-FUNCTION
END
/* Natural copycode containing prototype
DEFINE PROTOTYPE #ADD
RETURNS (I4)
DEFINE DATA PARAMETER
1 #SUMMAND1 (I4) BY VALUE
1 #SUMMAND2 (I4) BY VALUE
END-DEFINE
END-PROTOTYPE
You’re trying to use results of a function as an array-position. This doesn’t work because such a “nesting” (like in Java or C) is not allowed in Natural.
But I also wish, that this would work:
define data local
01 #ARRAY (I4/0:9) INIT < 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 >
01 #a5 (A5)
01 #n5 (N5)
end-define
#a5 := '1'
#n5 := val(#a5)
write #array(#n5) /* this works
write #array(val(#a5)) /* this doesn't
end
… but it doesn’t
BTW: If I want to compile or run your program, I got no syntax or runtime-error. Natural simply crashes!!! That is ugly!
Yes, that happened to me too, but only sometimes - other times I get the array-index error message. If you comment out the “bad line”, the rest works fine. Really strange, but I thought it was because on PC, for personal explorations, I am using the free Personal Edition. That crashed on me at other and quite harmless opportunities as well. My colleagues use the SPoD - we’ll try on Monday it crashes the application there as well.
Too bad the “nestings” don’t work: Is it not so, that the expressions “val(a5)” or “add (<2,3>)” are solved first and the resulting integer is then given to the enclosing “array(solution-as-index)” ?
I cannot explain it in detail, because I never used Natural-functions in real life. But you will get used to this logic. Another examle is the substring option SUBSTR. It is not a function in terms of C-style languages. Is is a part of the MOVE-Statement (and others).