0

I am using GNU Awk 5.1.0, API: 3.0 and have the following awk script. Testing this has shown that one can never use a function argument to change the variable used as input to it. Thus in the following example, one gets p: 21. Meaning that as long as pass a variable as an argument to the function, I would not be able to change it to something else after the function terminates. Is this correct?

awk '
    function solarex_nuclmn(e) {
        e = 34
    }

    BEGIN {
        p = 21
        solarex_nuclmn(p)
        print "p: " p
    }
'

I understand that I can get the value p: 34 using return e.

awk '
    function solarex_nuclmn(e) {
        e = 34
        return e
    }

    BEGIN {
        p = 21
        p = solarex_nuclmn()
        print "p: " p
    }
'

Then there is the possibility to using global variable. Although most times people would like to avoid using global variable.

awk '
    function solarex_nuclmn(e) {
        G = 34
    }

    BEGIN {
        G = 21
        solarex_nuclmn()
        print "G: " G
    }
'

Finally the possibility of using an array

awk '
    function solarex_nuclmn(a) {
        a[0] = 34
    }

    BEGIN {
        a[0] = 21
        solarex_nuclmn(a)
        print "a[0]: " a[0]
    }
'

And that is all. There are no other possibilities for having p: 34, am I right? Either use return, global, or array. For changing multiple values, the best way would be to pass an array as argument (because one can change array element values and also add additional array elements).

I am looking for a portable way for a function to change values outside the scope of the function. But if there is something specific to GAWK, I would also like to hear it.

11
  • 1
    Function call arguments are local. You can't change globals this way. Use return values or just modify the global in the function body. Apr 8, 2023 at 2:23
  • I know how to use return value but not how to use globals.
    – Vera
    Apr 8, 2023 at 3:10
  • It looks like you just want to define an array, but you can also make a function that returns a value based on the value fed in.
    – DopeGhoti
    Apr 8, 2023 at 3:13
  • 1
    The code in your question was far more complicated than necessary to demonstrate your needs so I tidied it up. Going forward please post minimal, complete verifiable examples that only demonstrate the problem you're asking about, not just whatever code you have lying around that includes the problem somewhere, so it's as easy as possible for as many people as possible to help you.
    – Ed Morton
    Apr 8, 2023 at 13:13
  • 1
    Regarding Somehow, the value is getting assumed to be of local scope - see the answers+comments to your questions from 2 days ago (unix.stackexchange.com/q/742079/133219 and stackoverflow.com/questions/75941917/…) and others for the explanation of Somehow.
    – Ed Morton
    Apr 8, 2023 at 13:21

1 Answer 1

4

I'm not sure what you are looking for, but here are 3 uses of functions in GNU awk showing typical uses of the arguments and return values:

function f(a,b){ return a+b; }
function g(a,b){ A=a; B=b; }
function h(a,b){ SYMTAB[a] = SYMTAB[b]; }
BEGIN{
  A=99; B=88; a=77; b=66;
  x=f(1,2);   print "x=" x
  g(4,5);     print "A=" A, "B=" B
  h("A","B"); print "A=" A
}

f() returns a value so it is used in expressions and assignments such as x=f(1,2). g() sets globals A and B. They are globals because they are not in the parameter list a,b. h() implements a sort of call by name, where a global name is passed in as a string, and the global variable of that name is accessed via the GNU builtin SYMTAB symbol table. This produces the output

x=3
A=4 B=5
A=5
7
  • Correct, I understand your examples. What I want to confirm is that if a pass a variable (p = 21 say) as argument, I can never get the function to change the value of the argument to something else (e.g. p = 34), which I can use after calling solarex_nuclmn(p). Meaning that solarex_nuclmn(p) ; print p will never give me a value of p equal to the value changed by the function.
    – Vera
    Apr 8, 2023 at 13:43
  • Every variable in awk is global, except within a function when all the formal parameters in the definition are local. If p is not in the formal parameters of function f then calling f can change p. Eg function f(a,b){p=34;} called with f() or f(99) or f(p) will change p. If you have a function g(a,b,p){p=33;} then calling g will not change global p. But if g(a,b,p){f();} then g() will change global p.
    – meuh
    Apr 8, 2023 at 14:25
  • I am only considering the case when the variable names a and b are in the formal parameter arguments of function f(a, b). Then the function f(a, b) can never change a and b outside the function scope.
    – Vera
    Apr 8, 2023 at 14:41
  • 1
    Again, you're not trying to output the arguments, you are trying to populate them. See the comments and answers associated with your previous questions for the confusion you're causing by using the term "output" in this context.
    – Ed Morton
    Apr 8, 2023 at 19:24
  • 1
    Right. Because using print within a function is considered output. You are correct. my meaning is populating the values of arguments.
    – Vera
    Apr 8, 2023 at 19:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .