# user-defined functions - limited scope ?

**URL:** <https://techcommunity.softwareag.com/t/user-defined-functions-limited-scope/61351>\
**Category:** Forum\
**Tags:** Adabas-Natural, Natural, Natural-on-Linux, Natural-on-Windows-Unix, Adabas-Natural-Cloud\
**Created:** [August 18, 2006, 5:22pm UTC](https://techcommunity.softwareag.com/t/user-defined-functions-limited-scope/61351 "2006-08-18T17:22:08Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![djh](https://avatars.discourse-cdn.com/v4/letter/d/e36b37/32.png) [@djh](https://techcommunity.softwareag.com/u/djh)\
**Post date:** [August 18, 2006, 5:22pm UTC](https://techcommunity.softwareag.com/t/user-defined-functions-limited-scope/61351/1 "2006-08-18T17:22:08Z")

</div>

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:

```auto

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:

```auto

/* 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.

---

<div class="post-metadata">

**Author:** ![Matthias1](https://avatars.discourse-cdn.com/v4/letter/m/9fc348/32.png) [@Matthias1](https://techcommunity.softwareag.com/u/Matthias1)\
**Post date:** [August 18, 2006, 7:26pm UTC](https://techcommunity.softwareag.com/t/user-defined-functions-limited-scope/61351/2 "2006-08-18T19:26:05Z")

</div>

> [@djh](#):
>
> ```auto
> WRITE 'array-element 2 + 3' #ARRAY(#ADD(< 2,3 >)) /* this doesn't
> ```

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:

```auto
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!

---

<div class="post-metadata">

**Author:** ![djh](https://avatars.discourse-cdn.com/v4/letter/d/e36b37/32.png) [@djh](https://techcommunity.softwareag.com/u/djh)\
**Post date:** [August 18, 2006, 11:38pm UTC](https://techcommunity.softwareag.com/t/user-defined-functions-limited-scope/61351/3 "2006-08-18T23:38:17Z")

</div>

> [@Matthias](#):
>
> 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)” ?

---

<div class="post-metadata">

**Author:** ![Matthias1](https://avatars.discourse-cdn.com/v4/letter/m/9fc348/32.png) [@Matthias1](https://techcommunity.softwareag.com/u/Matthias1)\
**Post date:** [August 19, 2006, 3:18am UTC](https://techcommunity.softwareag.com/t/user-defined-functions-limited-scope/61351/4 "2006-08-19T03:18:50Z")

</div>

> [@djh](#):
>
> Yes, that happened to me too, but only sometimes

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…

> [@](#):
>
> 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.

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

> [@](#):
>
> 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.

BTW: [http://natural.forums.softwareag.com/viewtopic.php?t=308](http://natural.forums.softwareag.com/viewtopic.php?t=308)

> [@](#):
>
> 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).

---

<div class="post-metadata">

**Author:** ![Matthias1](https://avatars.discourse-cdn.com/v4/letter/m/9fc348/32.png) [@Matthias1](https://techcommunity.softwareag.com/u/Matthias1)\
**Post date:** [August 21, 2006, 12:37pm UTC](https://techcommunity.softwareag.com/t/user-defined-functions-limited-scope/61351/5 "2006-08-21T12:37:02Z")

</div>

> [@Matthias](#):
>
> 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…

On Solaris, Natural crashes too.

```auto
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:

---

<div class="post-metadata">

**Author:** ![djh](https://avatars.discourse-cdn.com/v4/letter/d/e36b37/32.png) [@djh](https://techcommunity.softwareag.com/u/djh)\
**Post date:** [August 21, 2006, 2:21pm UTC](https://techcommunity.softwareag.com/t/user-defined-functions-limited-scope/61351/6 "2006-08-21T14:21:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/techcommunity/original/3X/c/d/cd19967bb605d28a72d77eb83a1cae22f3df2eb8.svg) [@system](https://techcommunity.softwareag.com/u/system)\
**Post date:** [April 2, 2021, 2:40pm UTC](https://techcommunity.softwareag.com/t/user-defined-functions-limited-scope/61351/7 "2021-04-02T14:40:47Z")

</div>


