Parameter (computer programming)

From Wikipedia for FEVERv2
Jump to navigation Jump to search

In computer programming, a parameter or a formal argument, is a special kind of variable, used in a subroutine to refer to one of the pieces of data provided as input to the subroutine. Parameter (computer programming)_sentence_0

These pieces of data are the values of the arguments (often called actual arguments or actual parameters) with which the subroutine is going to be called/invoked. Parameter (computer programming)_sentence_1

An ordered list of parameters is usually included in the definition of a subroutine, so that, each time the subroutine is called, its arguments for that call are evaluated, and the resulting values can be assigned to the corresponding parameters. Parameter (computer programming)_sentence_2

Unlike argument in usual mathematical usage, the argument in computer science is thus the actual input expression passed/supplied to a function, procedure, or routine in the invocation/call statement, whereas the parameter is the variable inside the implementation of the subroutine. Parameter (computer programming)_sentence_3

For example, if one defines the add subroutine as def add(x, y): return x + y, then x, y are parameters, while if this is called as add(2, 3), then 2, 3 are the arguments. Parameter (computer programming)_sentence_4

Note that variables (and expressions thereof) from the calling context can be arguments: if the subroutine is called as a = 2; b = 3; add(a, b) then the variables a, b are the arguments, not the values 2, 3. Parameter (computer programming)_sentence_5

See the Parameters and arguments section for more information. Parameter (computer programming)_sentence_6

In the most common case, call by value, a parameter acts within the subroutine as a new local variable initialized to the value of the argument (a local (isolated) copy of the argument if the argument is a variable), but in other cases, e.g. call by reference, the argument variable supplied by the caller can be affected by actions within the called subroutine (as discussed in evaluation strategy). Parameter (computer programming)_sentence_7

The semantics for how parameters can be declared and how the (value of) arguments are passed to the parameters of subroutines are defined by the language, but the details of how this is represented in any particular computer system depend on the calling conventions of that system. Parameter (computer programming)_sentence_8

Example Parameter (computer programming)_section_0

The following program in the C programming language defines a function that is named "SalesTax" and has one parameter named "price". Parameter (computer programming)_sentence_9

The type of price is "double" (i.e. a double-precision floating point number). Parameter (computer programming)_sentence_10

The function's return type is also a double. Parameter (computer programming)_sentence_11

After the function has been defined, it can be invoked as follows: Parameter (computer programming)_sentence_12

In this example, the function has been invoked with the argument 10.00. Parameter (computer programming)_sentence_13

When this happens, 10.00 will be assigned to price, and the function begins calculating its result. Parameter (computer programming)_sentence_14

The steps for producing the result are specified below, enclosed in {}. Parameter (computer programming)_sentence_15

0.05 * price indicates that the first thing to do is multiply 0.05 by the value of price, which gives 0.50. return means the function will produce the result of 0.05 * price. Parameter (computer programming)_sentence_16

Therefore, the final result (ignoring possible round-off errors one encounters with representing decimal fractions as binary fractions) is 0.50. Parameter (computer programming)_sentence_17

Parameters and arguments Parameter (computer programming)_section_1

The terms parameter and argument may have different meanings in different programming languages. Parameter (computer programming)_sentence_18

Sometimes they are used interchangeably, and the context is used to distinguish the meaning. Parameter (computer programming)_sentence_19

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. Parameter (computer programming)_sentence_20

For example, if one defines a function as def f(x): ..., then x is the parameter, and if it is called by a = ...; f(a) then a is the argument. Parameter (computer programming)_sentence_21

A parameter is an (unbound) variable, while the argument can be a literal or variable or more complex expression involving literals and variables. Parameter (computer programming)_sentence_22

In case of call by value, what is passed to the function is the value of the argument – for example, f(2) and a = 2; f(a) are equivalent calls – while in call by reference, with a variable as argument, what is passed is a reference to that variable - even though the syntax for the function call could stay the same. Parameter (computer programming)_sentence_23

The specification for pass-by-reference or pass-by-value would be made in the function declaration and/or definition. Parameter (computer programming)_sentence_24

Parameters appear in procedure definitions; arguments appear in procedure calls. Parameter (computer programming)_sentence_25

In the function definition f(x) = x*x the variable x is a parameter; in the function call f(2) the value 2 is the argument of the function. Parameter (computer programming)_sentence_26

Loosely, a parameter is a type, and an argument is an instance. Parameter (computer programming)_sentence_27

A parameter is an intrinsic property of the procedure, included in its definition. Parameter (computer programming)_sentence_28

For example, in many languages, a procedure to add two supplied integers together and calculate the sum would need two parameters, one for each integer. Parameter (computer programming)_sentence_29

In general, a procedure may be defined with any number of parameters, or no parameters at all. Parameter (computer programming)_sentence_30

If a procedure has parameters, the part of its definition that specifies the parameters is called its parameter list. Parameter (computer programming)_sentence_31

By contrast, the arguments are the expressions supplied to the procedure when it is called, usually one expression matching one of the parameters. Parameter (computer programming)_sentence_32

Unlike the parameters, which form an unchanging part of the procedure's definition, the arguments may vary from call to call. Parameter (computer programming)_sentence_33

Each time a procedure is called, the part of the procedure call that specifies the arguments is called the argument list. Parameter (computer programming)_sentence_34

Although parameters are also commonly referred to as arguments, arguments are sometimes thought of as the actual values or references assigned to the parameter variables when the subroutine is called at run-time. Parameter (computer programming)_sentence_35

When discussing code that is calling into a subroutine, any values or references passed into the subroutine are the arguments, and the place in the code where these values or references are given is the parameter list. Parameter (computer programming)_sentence_36

When discussing the code inside the subroutine definition, the variables in the subroutine's parameter list are the parameters, while the values of the parameters at runtime are the arguments. Parameter (computer programming)_sentence_37

For example, in C, when dealing with threads it's common to pass in an argument of type void* and cast it to an expected type: Parameter (computer programming)_sentence_38

To better understand the difference, consider the following function written in C: Parameter (computer programming)_sentence_39

The function Sum has two parameters, named addend1 and addend2. Parameter (computer programming)_sentence_40

It adds the values passed into the parameters, and returns the result to the subroutine's caller (using a technique automatically supplied by the C compiler). Parameter (computer programming)_sentence_41

The code which calls the Sum function might look like this: Parameter (computer programming)_sentence_42

The variables value1 and value2 are initialized with values. Parameter (computer programming)_sentence_43

value1 and value2 are both arguments to the sum function in this context. Parameter (computer programming)_sentence_44

At runtime, the values assigned to these variables are passed to the function Sum as arguments. Parameter (computer programming)_sentence_45

In the Sum function, the parameters addend1 and addend2 are evaluated, yielding the arguments 40 and 2, respectively. Parameter (computer programming)_sentence_46

The values of the arguments are added, and the result is returned to the caller, where it is assigned to the variable sum_value. Parameter (computer programming)_sentence_47

Because of the difference between parameters and arguments, it is possible to supply inappropriate arguments to a procedure. Parameter (computer programming)_sentence_48

The call may supply too many or too few arguments; one or more of the arguments may be a wrong type; or arguments may be supplied in the wrong order. Parameter (computer programming)_sentence_49

Any of these situations causes a mismatch between the parameter and argument lists, and the procedure will often return an unintended answer or generate a runtime error. Parameter (computer programming)_sentence_50

Alternative convention in Eiffel Parameter (computer programming)_section_2

Within the Eiffel software development method and language, the terms argument and parameter have distinct uses established by convention. Parameter (computer programming)_sentence_51

The term argument is used exclusively in reference to a routine's inputs, and the term parameter is used exclusively in type parameterization for generic classes. Parameter (computer programming)_sentence_52

Consider the following routine definition: Parameter (computer programming)_sentence_53

The routine sum takes two arguments addend1 and addend2, which are called the routine's formal arguments. Parameter (computer programming)_sentence_54

A call to sum specifies actual arguments, as shown below with value1 and value2. Parameter (computer programming)_sentence_55

Parameters are also thought of as either formal or actual. Parameter (computer programming)_sentence_56

Formal generic parameters are used in the definition of generic classes. Parameter (computer programming)_sentence_57

In the example below, the class HASH_TABLE is declared as a generic class which has two formal generic parameters, G representing data of interest and K representing the hash key for the data: Parameter (computer programming)_sentence_58

When a class becomes a client to HASH_TABLE, the formal generic parameters are substituted with actual generic parameters in a generic derivation. Parameter (computer programming)_sentence_59

In the following attribute declaration, my_dictionary is to be used as a character string based dictionary. Parameter (computer programming)_sentence_60

As such, both data and key formal generic parameters are substituted with actual generic parameters of type STRING. Parameter (computer programming)_sentence_61

Datatypes Parameter (computer programming)_section_3

In strongly typed programming languages, each parameter's type must be specified in the procedure declaration. Parameter (computer programming)_sentence_62

Languages using type inference attempt to discover the types automatically from the function's body and usage. Parameter (computer programming)_sentence_63

Dynamically typed programming languages defer type resolution until run-time. Parameter (computer programming)_sentence_64

Weakly typed languages perform little to no type resolution, relying instead on the programmer for correctness. Parameter (computer programming)_sentence_65

Some languages use a special keyword (e.g. void) to indicate that the subroutine has no parameters; in formal type theory, such functions take an empty parameter list (whose type is not void, but rather unit). Parameter (computer programming)_sentence_66

Argument passing Parameter (computer programming)_section_4

The exact mechanism for assigning arguments to parameters, called argument passing, depends upon the evaluation strategy used for that parameter (typically call by value), which may be specified using keywords. Parameter (computer programming)_sentence_67

Default arguments Parameter (computer programming)_section_5

Some programming languages such as Ada, C++, Clojure, Common Lisp, Fortran 90, Python, Ruby, Tcl, and Windows PowerShell allow for a default argument to be explicitly or implicitly given in a subroutine's declaration. Parameter (computer programming)_sentence_68

This allows the caller to omit that argument when calling the subroutine. Parameter (computer programming)_sentence_69

If the default argument is explicitly given, then that value is used if it is not provided by the caller. Parameter (computer programming)_sentence_70

If the default argument is implicit (sometimes by using a keyword such as Optional) then the language provides a well-known value (such as null, Empty, zero, an empty string, etc.) if a value is not provided by the caller. Parameter (computer programming)_sentence_71

PowerShell example: Parameter (computer programming)_sentence_72

Default arguments can be seen as a special case of the variable-length argument list. Parameter (computer programming)_sentence_73

Variable-length parameter lists Parameter (computer programming)_section_6

Some languages allow subroutines to be defined to accept a variable number of arguments. Parameter (computer programming)_sentence_74

For such languages, the subroutines must iterate through the list of arguments. Parameter (computer programming)_sentence_75

PowerShell example: Parameter (computer programming)_sentence_76

Named parameters Parameter (computer programming)_section_7

Some programming languages—such as Ada and Windows PowerShell—allow subroutines to have named parameters. Parameter (computer programming)_sentence_77

This allows the calling code to be more self-documenting. Parameter (computer programming)_sentence_78

It also provides more flexibility to the caller, often allowing the order of the arguments to be changed, or for arguments to be omitted as needed. Parameter (computer programming)_sentence_79

PowerShell example: Parameter (computer programming)_sentence_80

Multiple parameters in functional languages Parameter (computer programming)_section_8

In lambda calculus, each function has exactly one parameter. Parameter (computer programming)_sentence_81

What is thought of as functions with multiple parameters is usually represented in lambda calculus as a function which takes the first argument, and returns a function which takes the rest of the arguments; this is a transformation known as currying. Parameter (computer programming)_sentence_82

Some programming languages, like ML and Haskell, follow this scheme. Parameter (computer programming)_sentence_83

In these languages, every function has exactly one parameter, and what may look like the definition of a function of multiple parameters, is actually syntactic sugar for the definition of a function that returns a function, etc. Function application is left-associative in these languages as well as in lambda calculus, so what looks like an application of a function to multiple arguments is correctly evaluated as the function applied to the first argument, then the resulting function applied to the second argument, etc. Parameter (computer programming)_sentence_84

Output parameters Parameter (computer programming)_section_9

An output parameter, also known as an out parameter or return parameter, is a parameter used for output, rather than the more usual use for input. Parameter (computer programming)_sentence_85

Using call by reference parameters, or call by value parameters where the value is a reference, as output parameters is an idiom in some languages, notably C and C++, while other languages have built-in support for output parameters. Parameter (computer programming)_sentence_86

Languages with built-in support for output parameters include Ada (see ), Fortran (since Fortran 90; see ), various procedural extensions to SQL, such as PL/SQL (see PL/SQL functions) and Transact-SQL, C# and the .NET Framework, Swift, and the scripting language TScript (see TScript function declarations). Parameter (computer programming)_sentence_87

More precisely, one may distinguish three types of parameters or parameter modes: input parameters, output parameters, and input/output parameters; these are often denoted in, out, and in out or inout. Parameter (computer programming)_sentence_88

An input argument (the argument to an input parameter) must be a value, such as an initialized variable or literal, and must not be redefined or assigned to; an output argument must be an assignable variable, but it need not be initialized, any existing value is not accessible, and must be assigned a value; and an input/output argument must be an initialized, assignable variable, and can optionally be assigned a value. Parameter (computer programming)_sentence_89

The exact requirements and enforcement vary between languages – for example, in Ada 83 output parameters can only be assigned to, not read, even after assignment (this was removed in Ada 95 to remove the need for an auxiliary accumulator variable). Parameter (computer programming)_sentence_90

These are analogous to the notion of a value in an expression being an r-value (has a value), an l-value (can be assigned), or an r-value/l-value (has a value and can be assigned), respectively, though these terms have specialized meanings in C. Parameter (computer programming)_sentence_91

In some cases only input and input/output are distinguished, with output being considered a specific use of input/output, and in other cases only input and output (but not input/output) are supported. Parameter (computer programming)_sentence_92

The default mode varies between languages: in Fortran 90 input/output is default, while in C# and SQL extensions input is default, and in TScript each parameter is explicitly specified as input or output. Parameter (computer programming)_sentence_93

Syntactically, parameter mode is generally indicated with a keyword in the function declaration, such as void f(out int x) in C#. Parameter (computer programming)_sentence_94

Conventionally output parameters are often put at the end of the parameter list to clearly distinguish them, though this is not always followed. Parameter (computer programming)_sentence_95

TScript uses a different approach, where in the function declaration input parameters are listed, then output parameters, separated by a colon (:) and there is no return type to the function itself, as in this function, which computes the size of a text fragment: Parameter (computer programming)_sentence_96

Parameter modes are a form of denotational semantics, stating the programmer's intent and allowing compilers to catch errors and apply optimizations – they do not necessarily imply operational semantics (how the parameter passing actually occurs). Parameter (computer programming)_sentence_97

Notably, while input parameters can be implemented by call by value, and output and input/output parameters by call by reference – and this is a straightforward way to implement these modes in languages without built-in support – this is not always how they are implemented. Parameter (computer programming)_sentence_98

This distinction is discussed in detail in the Ada '83 Rationale, which emphasizes that the parameter mode is abstracted from which parameter passing mechanism (by reference or by copy) is actually implemented. Parameter (computer programming)_sentence_99

For instance, while in C# input parameters (default, no keyword) are passed by value, and output and input/output parameters (out and ref) are passed by reference, in PL/SQL input parameters (IN) are passed by reference, and output and input/output parameters (OUT and IN OUT) are by default passed by value and the result copied back, but can be passed by reference by using the NOCOPY compiler hint. Parameter (computer programming)_sentence_100

A syntactically similar construction to output parameters is to assign the return value to a variable with the same name as the function. Parameter (computer programming)_sentence_101

This is found in Pascal and Fortran 66 and Fortran 77, as in this Pascal example: Parameter (computer programming)_sentence_102

This is semantically different in that when called, the function is simply evaluated – it is not passed a variable from the calling scope to store the output in. Parameter (computer programming)_sentence_103

Use Parameter (computer programming)_section_10

The primary use of output parameters is to return multiple values from a function, while the use of input/output parameters is to modify state using parameter passing (rather than by shared environment, as in global variables). Parameter (computer programming)_sentence_104

An important use of returning multiple values is to solve the semipredicate problem of returning both a value and an error status – see Semipredicate problem: Multivalued return. Parameter (computer programming)_sentence_105

For example, to return two variables from a function in C, one may write: Parameter (computer programming)_sentence_106

where x is an input parameter and width and height are output parameters. Parameter (computer programming)_sentence_107

A common use case in C and related languages is for exception handling, where a function places the return value in an output variable, and returns a boolean corresponding to whether the function succeeded or not. Parameter (computer programming)_sentence_108

An archetypal example is the TryParse method in .NET, especially C#, which parses a string into an integer, returning true on success and false on failure. Parameter (computer programming)_sentence_109

This has the following signature: Parameter (computer programming)_sentence_110

and may be used as follows: Parameter (computer programming)_sentence_111

Similar considerations apply to returning a value of one of several possible types, where the return value can specify the type and then value is stored in one of several output variables. Parameter (computer programming)_sentence_112

Drawbacks Parameter (computer programming)_section_11

Output parameters are often discouraged in modern programming, essentially as being awkward, confusing, and too low-level – commonplace return values are considerably easier to understand and work with. Parameter (computer programming)_sentence_113

Notably, output parameters involve functions with side effects (modifying the output parameter) and are semantically similar to references, which are more confusing than pure functions and values, and the distinction between output parameters and input/output parameters can be subtle. Parameter (computer programming)_sentence_114

Further, since in common programming styles most parameters are simply input parameters, output parameters and input/output parameters are unusual and hence susceptible to misunderstanding. Parameter (computer programming)_sentence_115

Output and input/output parameters prevent function composition, since the output is stored in variables, rather than in the value of an expression. Parameter (computer programming)_sentence_116

Thus one must initially declare a variable, and then each step of a chain of functions must be a separate statement. Parameter (computer programming)_sentence_117

For example, in C++ the following function composition: Parameter (computer programming)_sentence_118

when written with output and input/output parameters instead becomes (for F it is an output parameter, for G an input/output parameter): Parameter (computer programming)_sentence_119

In the special case of a function with a single output or input/output parameter and no return value, function composition is possible if the output or input/output parameter (or in C/C++, its address) is also returned by the function, in which case the above becomes: Parameter (computer programming)_sentence_120

Alternatives Parameter (computer programming)_section_12

There are various alternatives to the use cases of output parameters. Parameter (computer programming)_sentence_121

For returning multiple values from a function, an alternative is to return a tuple. Parameter (computer programming)_sentence_122

Syntactically this is clearer if automatic sequence unpacking and parallel assignment can be used, as in Go or Python, such as: Parameter (computer programming)_sentence_123

For returning a value of one of several types, a tagged union can be used instead; the most common cases are nullable types (option types), where the return value can be null to indicate failure. Parameter (computer programming)_sentence_124

For exception handling, one can return a nullable type, or raise an exception. Parameter (computer programming)_sentence_125

For example, in Python one might have either: Parameter (computer programming)_sentence_126

or, more idiomatically: Parameter (computer programming)_sentence_127

The micro-optimization of not requiring a local variable and copying the return when using output variables can also be applied to conventional functions and return values by sufficiently sophisticated compilers. Parameter (computer programming)_sentence_128

The usual alternative to output parameters in C and related languages is to return a single data structure containing all return values. Parameter (computer programming)_sentence_129

For example, given a structure encapsulating width and height, one can write: Parameter (computer programming)_sentence_130

In object-oriented languages, instead of using input/output parameters, one can often use call by sharing, passing a reference to an object and then mutating the object, though not changing which object the variable refers to. Parameter (computer programming)_sentence_131

See also Parameter (computer programming)_section_13

Parameter (computer programming)_unordered_list_0


Credits to the contents of this page go to the authors of the corresponding Wikipedia page: en.wikipedia.org/wiki/Parameter (computer programming).