Is print a function or statement.

Why parentheses are not required?

I couldn't find any information about this topic.

link|improve this question

feedback

1 Answer

up vote 1 down vote accepted

print is a statement that doesn't require (but optionally accepts) parentheses.

The entire list of items may optionally be enclosed in parentheses. The parentheses are necessary if any of the item expressions uses the `>' relational operator; otherwise it could be confused with a redirection

(see The print Statement)

Some people always use the parentheses, because e.g. they prefer the function syntax and it has no negative side effects.

link|improve this answer
What makes a phrase a statement is either (1) its ability to occur in certain syntactic contexts, for instance as a complete line of the awk script, or (2) its not returning a value. By either understanding, print is a statement. Both understandings are problematic though. The first because on some (but not all) awk implementations, any expression can occur in a statement context, so on this understanding all expressions would be statements. The second because assignments and user-defined function calls return values but the first is indisputably and the second is disputably a statement. – dubiousjim Apr 19 at 11:18
There are also functions that don't require parentheses: getline is one example, and on some implementations of awk, you can use length without parentheses as a shorthand for length($0). – dubiousjim Apr 19 at 11:20
@dubiousjim: Please feel free to edit the answer as necessary on the finer points I may have left out or not understood. – user112553 Apr 23 at 18:37
I didn't mean to criticize your answer, but just to think out loud about the (not-immediately obvious) difficulties giving a principled account of why, for example, print is counted a statement in awk, but getline is counted a function. – dubiousjim Apr 23 at 21:08
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.