help-gawk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Using function arguments


From: Wolfgang Laun
Subject: Re: Using function arguments
Date: Sat, 8 Apr 2023 10:08:50 +0200

Most of the time code will be cleaner if one function computes one value.

If the values belong to a single object, use an array to represent all
properties of the object. You can pass the array to a function and fill in
any properties. Example:

function sphere( r, s ){
   s["v"] = 4.0*PI*r*r*r/3
   s["a"] = 4*r*r*PI
}
BEGIN {
   PI = 3.14159
   va = sphere( 1, s )
   print s["v"], s["a"]
}
Alternatively, set s["r"] to avoid the first argument.

Wolfgang

On Fri, 7 Apr 2023 at 23:00, uzibalqa via Help-gawk <help-gawk@gnu.org>
wrote:

>
> ------- Original Message -------
> On Thursday, April 6th, 2023 at 9:44 PM, Manuel Collado <
> mcollado2011@gmail.com> wrote:
>
>
> > Please read the gawk manual section on functions.
> >
> > https://www.gnu.org/software/gawk/manual/gawk.html#User_002ddefined
> > https://www.gnu.org/software/gawk/manual/gawk.html#Return-Statement
>
> After reading the links, I am under the impression that function arguments
> can only be used as input.  For output one can use "result var".  I am
> struggling about what to do when I want to output a number of variables
> set inside the function.
>
> > In short:
> >
> > function product(a, b) {
> > return a * b
> > }
> >
> > HTH.
> >
> > El 5/4/23 a las 22:41, uzibalqa via Help-gawk escribió:
> >
> > > I am having difficulties understanding how to use gawk function
> arguments.
> > > Customarily, arguments define variables that are local to the function.
> > >
> > > But can arguments be used as input that the function can use
> > >
> > > As example
> > >
> > > function densel(class, loc) {
> > > if (class == 1) { print "Class is 1" }
> > > if (class == 2) { print "Class is 2" }
> > > loc = 8
> > > }
> > >
> > > BEGIN {
> > > class=2
> > > densel(class)
> > > }
> > >
> > > Then there is option of using "return class" which can actually return
> a value by calling
> > >
> > > x=densel(class)
> >
> >
> > --
> > Manuel Collado - http://mcollado.z15.es
>
>

-- 
Wolfgang Laun


reply via email to

[Prev in Thread] Current Thread [Next in Thread]