In everyday conversation, we usually use the word parameter to describe the use of a function or command.

  • The initialCapacity of a HashMap can be set using the constructor’s initialCapacity parameter, which I passed as 1000.
  • Using the rm command -r parameter, you can delete a directory.

However, when reading English documents, argument, parameter and option often appear alternately. Especially, argument and parameter confuse me more. So I looked up some information. Mainly for Java and Shell semantics are sorted out:

Argument and parameter in Java

Arguments and parameters in the Java context are specified very clearly in the official documentation. Oracle Java’s Passing Information to a Method or a Constructor section

Note: Parameters refers to the list of variables in a method declaration. Arguments are the actual values that are passed in when the method is invoked. When you invoke a method, the arguments used must match the declaration’s parameters in type and order.

So, in Java, parameter refers to a function definition. Argument refers to a function call.

Therefore, in the Java Doc, the @param annotation is used to specify what the parameters mean:

    / * * *@param  initialCapacity the initial capacity.
     * @throws IllegalArgumentException if the initial capacity is negative.
     */
    public HashMap(int initialCapacity) {
        this(initialCapacity, DEFAULT_LOAD_FACTOR);
    }
Copy the code

Use IllegalArgumentException to indicate that the argument passed by the call is invalid.

/**
 * Thrown to indicate that a method has been passed an illegal or
 inappropriate argument.
 *
 * @author  unascribed
 * @sinceJDK1.0 * /
public class IllegalArgumentException extends RuntimeException {
Copy the code

In addition, the meaning of the Java context is also the meaning of most programming languages, as described in the Wikipedia Parameter (Computer Programming) -Wikipedia:

The term parameter (sometimes called formal parameter) is often used to refer to the variable as found in the function definition, while argument (sometimes called actual parameter) refers to the actual input supplied at function call.

Wikipedia also refers to parameters as formal parameters, while argument corresponds to actual parameters.

Argument, option, and parameter in the Shell

argument

In Shell, no command, script, or function can define parameters as Java does, so there is no strict Java parameter. In fact, the command line functionality is too complex and complex to be as explicit a parameter as a single-function Java function. Argument is similar, though, and refers to the value passed in at runtime. For example, bash’s man documentation says:

SHELL GRAMMAR Simple Commands A simple command is a sequence of optional variable assignments followed by blank-separated words and redirections, and terminated by a control operator. The first word specifies the command to be executed, and is passed as argument zero. The remaining words are passed as arguments to the invoked command.

When it comes to execution, words separated by Spaces are called arguments, except the first word is the command or function to execute. For example, ls -l/TMP has two arguments: -l and/TMP.

But neither pipe nor redirection counts as an argument; they are not part of the previous command per se. Ls -l/TMP >files. TXT is the third argument.

option

Options are defined and identified by the specific program. A program that accepts those options is written in the program, so options refer to specific commands or functions that make sense. Options can be thought of as a refinement of arguments, which are usually arguments with – or –. This makes it easier for the program to parse. So argument 2 of ls -l/TMP is an option.

Wikipedia’s command-line interface describes options in this way

A command-line option or simply option (also known as a flag or switch) modifies the operation of a command; the effect is determined by the command’s program.

parameter

If you have to correspond to Parameters in Java, you can think of Shell as Positional Parameters, 1st parameter, 2nd parameter, and so on, very simple and crude. What they mean is a matter of implementation, which also creates the need for command-line or shell functions to parse positional arguments themselves. The advantage of simplicity is inclusiveness.

The bash documentation, however, has a section devoted to parameter:

A parameter is an entity that stores values. It can be a name, a number, or one of the special characters listed below under Special Parameters. A variable is a parameter denoted by a name.

Positional Parameters, Special Parameters, such as $#, $? Shell Variables (Variables that shells automatically set or use, like PATH), and Arrays. Parameter here is more like a special variable.

A more practical distinction

The Shell explanation is a little too obscure and impractical. Bash – Difference between terms: “option”, “argument”, and “parameter”? – Stack Overflow, while inaccurate, is more practical. He think:

A parameter is an argument that provides information to either the command or one of its options

Under this interpretation, the distinction is as follows:

  • Argument is the name for everything passed after a command. (This point is the same as the original explanation.)
  • One of the arguments is option, and they take-or--At the beginning. (This point is the same as the original explanation.)
  • The value accepted by option is called parameter (or value). Such assort -k 1, 1 is the parameter of option, indicating that the sorted column is the first column.

conclusion

  • Parameter and argument can be used interchangeably without being strict.
  • In Java or common programming languages, parameter refers to a variable in a function declaration; Argument refers to the actual input passed when the function is called.
  • On the Shell or command line, argument has a similar meaning to Java, referring to the input value at the call or run time. But the meaning of parameter is somewhat obscure and impractical. The argument is the general name of the argument and the option is the general name of the argument--or-Argument, and parameter is the value of option.

reference

  1. Tutorials _ Learning The Java Language _ Classes and Objects)
  2. Parameter (computer programming) – Wikipedia
  3. Command-line interface – Wikipedia
  4. Bash – Difference between terms: “option”, “argument”, and “parameter”? – Stack Overflow

Java and Linux Learning Weekly is published every Friday, updated synchronously on Github, Zhihu, Nuggets