Subroutine

Subroutine

Post by DE TT » Wed, 27 Feb 2002 01:07:11



Hi,

how Do I start a subroutine, inwhich the value of a variable stays in the
memory?

e.g.

varia = 1
...goto subroutine
varia = 1
...goto subroutine

:subroutine
echo $varia
....return to main routine

Thanx

 
 
 

Subroutine

Post by Ben Altma » Wed, 27 Feb 2002 02:03:40



Quote:> Hi,

> how Do I start a subroutine, inwhich the value of a variable stays in the
> memory?

> e.g.

> varia = 1
> ...goto subroutine
> varia = 1
> ...goto subroutine

> :subroutine
> echo $varia
> ....return to main routine

I presume you are referring to functions and local variables.  You don't
mention which shell you are using.  In ksh or bash you just do:
    function func {
        typeset localvar

    }

Ben

 
 
 

Subroutine

Post by Chris F.A. Johnso » Wed, 27 Feb 2002 02:55:36





> > Hi,

> > how Do I start a subroutine, inwhich the value of a variable stays in the
> > memory?

> > e.g.

> > varia = 1
> > ...goto subroutine
> > varia = 1
> > ...goto subroutine

> > :subroutine
> > echo $varia
> > ....return to main routine

> I presume you are referring to functions and local variables.  You don't
> mention which shell you are using.  In ksh or bash you just do:
>     function func {
>         typeset localvar

>     }

The OP was not clear, but I thought he was referring to just the
opposite: variables set in a subroutine/function and accessible in the
main script:

        subroutine() {
            echo $varia
        }

        varia=1
        subroutine

--
    Chris F.A. Johnson                        http://cfaj.freeshell.org
    ===================================================================
    My code (if any) in this post is copyright 2002, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License

 
 
 

Subroutine

Post by TTD » Thu, 28 Feb 2002 03:49:42







> > > Hi,

> > > how Do I start a subroutine, inwhich the value of a variable stays in
the
> > > memory?

> > > e.g.

> > > varia = 1
> > > ...goto subroutine
> > > varia = 1
> > > ...goto subroutine

> > > :subroutine
> > > echo $varia
> > > ....return to main routine

> > I presume you are referring to functions and local variables.  You don't
> > mention which shell you are using.  In ksh or bash you just do:
> >     function func {
> >         typeset localvar

> >     }

> The OP was not clear, but I thought he was referring to just the
> opposite: variables set in a subroutine/function and accessible in the
> main script:

> subroutine() {
>     echo $varia
> }

> varia=1
> subroutine

> --
>     Chris F.A. Johnson                        http://cfaj.freeshell.org
>     ===================================================================
>     My code (if any) in this post is copyright 2002, Chris F.A. Johnson
>     and may be copied under the terms of the GNU General Public License

Well it was not quite what I meant Chris, but I think it works.

What I meant was. I have a variabele varia=1 and then a function needs to
start to do something using the varia

I am using ksh and thought I had to do something like this.

varia=1
routine()

varia=2
routine

function routine()
{
 echo $local

Quote:}

But Unix gives me the errormessage and says he does'nt expect the "(" after
the 'function routine()'. Leaving this () away results in doing nothing.
 
 
 

Subroutine

Post by TTD » Thu, 28 Feb 2002 03:51:47


eeh my example had an error

not echo $local should stand there, but echo $varia.

sorry....

 
 
 

Subroutine

Post by Chris F.A. Johnso » Thu, 28 Feb 2002 04:32:27








> > > > Hi,

> > > > how Do I start a subroutine, inwhich the value of a variable stays in
> the
> > > > memory?

> > > > e.g.

> > > > varia = 1
> > > > ...goto subroutine
> > > > varia = 1
> > > > ...goto subroutine

> > > > :subroutine
> > > > echo $varia
> > > > ....return to main routine

> > > I presume you are referring to functions and local variables.  You don't
> > > mention which shell you are using.  In ksh or bash you just do:
> > >     function func {
> > >         typeset localvar

> > >     }

> > The OP was not clear, but I thought he was referring to just the
> > opposite: variables set in a subroutine/function and accessible in the
> > main script:

> > subroutine() {
> >     echo $varia
> > }

> > varia=1
> > subroutine

> Well it was not quite what I meant Chris, but I think it works.

> What I meant was. I have a variabele varia=1 and then a function needs to
> start to do something using the varia

> I am using ksh and thought I had to do something like this.

> varia=1
> routine()

> varia=2
> routine

> function routine()
> {
>  echo $local
> }
> But Unix gives me the errormessage and says he does'nt expect the "(" after
> the 'function routine()'. Leaving this () away results in doing nothing.

There are two different methods to declare a function:

        function routine {
           echo $varia
        }

and

        routine() {
           echo $varia
        }

Defining the function does just that; to execute the function, you
must call it AFTER it has been defined.

--
    Chris F.A. Johnson                        http://cfaj.freeshell.org
    ===================================================================
    My code (if any) in this post is copyright 2002, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License