3

Is print a function or statement.

Why parentheses are not required?

I couldn't find any information about this topic.

studiohack
  • 13,490
kev
  • 12,730

1 Answers1

2

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.

peth
  • 10,000
  • 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 '12 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 '12 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. – peth Apr 23 '12 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 '12 at 21:08
  • There is a huge difference between statements and expressions (function calls are expressions). Expressions return something. For example, take the sprintf() FUNCTION. It is similar to printf, except that it RETURNS a string. print and printf don't return anything (it doesn't make much sense for them to return a value). That means that you can't do length(print $1), that doesn't make sense. But you can do length(sprintf("%s", $1)). – darkfeline Oct 24 '17 at 06:26