[Scilab-users] Nested function definition

Samuel Gougeon sgougeon at free.fr
Wed Apr 10 17:10:54 CEST 2019


Izabela,

I have not clearly understood why you are speaking about "nested 
functions" in your example.
A nested function is a function that is /defined/  in another one.

About the example in the function help page:
It is right, but with Scilab 6, it looks a bit outdated to me.
Indeed, let's consider the following example:

// Content of the File myTest.sci
function myTest()
    disp("myTest() is running")
    myNextFun()
endfunction
function myNextFun()
     disp("myNextFun() is running")
endfunction
// End of myTest.sci file

When building a library (say "myLib"), this file is compiled, and

  * With Scilab 5 : both functions myTest() and myNextFun() are
    registered in the library, and so *are public*: Both can be called
    from anywhere, noticeably from the top-level, the console.
    The only way to make myNextFun() a private function known only by
    myTest() is to define it IN myTest(), as a nested function.

  * With Scilab 6: only myTest() is registered in myLib library, so is
    public, and can be called from anywhere. In the opposite, myNextFun()
      o is NOT registered in the library
      o so, is unknown from the console,
      o is shared and can be called only by other functions defined in
        the same file.

        This is a more powerful implementation for the functions
        privacy, because then
      o a private function (say myNextFun()) does no longer need to be
        recompiled each time that myTest() is called.
      o IMO, this makes the code clearer

This change in Scilab 6 could be documented in the --> help function 
page, in order to discourage true nested function. By the way, the 
example in the help misses being indented.

However, out of libraries, nested functions can still be used in 
scripts.sce or in files.sci that are just exec()uted, for the same 
purpose: keeping nested functions private.

HTH
Regards
Samuel

Le 10/04/2019 à 16:04, Izabela Wójcik-Grząba a écrit :
> Ok, so why nested function in help is so complicated:
>
> //nested functions definition
> function y=foo(x)
>    a=sin(x)
>    function y=sq(x), y=x^2,endfunction
>    y=sq(a)+1
> endfunction
>
> foo(%pi/3)
>
> Couldn't it be formulated like below:
>
> function y1=foo1(x)
>    a=sin(x);
>    y1=a^2+1;
> endfunction
>
> foo1(%pi/3)
>
> That's why I had problems with my functions and couldn't understand 
> why it has to be so complicated.
>
> Thank you once more.
>
> Iza 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.scilab.org/pipermail/users/attachments/20190410/b2bbd4f3/attachment.htm>


More information about the users mailing list