user-defined functions - limited scope ?

Hi all,

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

I’d appreciate any enlightenment on the subject.

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 :frowning:

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)” ?

Anytime I check/compile/run/stow this program on my Windows Environment → crash. I’ll check in on Solaris when I’m back at office on Monday…

I use Natural 6.1.1 PL21 with a “real” licence.

BTW: http://natural.forums.softwareag.com/viewtopic.php?t=308

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).

On Solaris, Natural crashes too.

System Error Code 1
Registers
CCR  0x0000000000000000
PC   0xffffffff7e900
...
Thread (5) <<Init Thread>> - Core Request

Current work directory: ...
Segmentation Fault - core dumped

:shock: And I only did a check:shock:

Well - looks like I won’t be using user-defined functions extensively. :?
Interesting link though in your previous post.