Parameter (computer science)


Parameter (computer science)

In computer programming, a parameter is a variable which takes on the meaning of a corresponding argument passed in a call to a subroutine. In the most common case, call-by-value, a parameter acts within the subroutine as a local (isolated) copy of the argument, but in other cases, e.g. call-by-reference, the argument supplied by the caller can be affected by actions within the called subroutine (as discussed in evaluation strategy).

Nearly all programming languages support subroutine parameters. The semantics for how parameters can be declared and how the arguments get passed to the parameters of subroutines are defined by the language, but the details of how this is represented in any particular computer system depends on the calling conventions of that system.

Parameters and arguments

A parameter represents a value that the procedure expects you to supply when you call it. The procedure's declaration defines its parameters.

You can define a procedure with no parameters, one parameter, or more than one. The part of the procedure definition that specifies the parameters is called the parameter list.

An argument represents the value you supply to a procedure parameter when you call the procedure. The calling code supplies the arguments when it calls the procedure. The part of the procedure call that specifies the arguments is called the argument list.

Although parameters are also commonly referred to as arguments, arguments are more properly thought of as the actual values or references assigned to the parameter variables when the subroutine is called at runtime. 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". 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.

Many programmers use "parameter" and "argument" interchangeably, depending on context to distinguish the meaning. In practice, distinguishing between the two terms is usually unnecessary in order to use them correctly or communicate their use to other programmers. Alternatively, the words "actual" and "formal" can be used to distinguish between an argument and a parameter, respectively. For example, the equivalent terms "actual argument" and "actual parameter" may be used instead of "argument"; and "formal argument" and "formal parameter" may be used instead of "parameter".

To better understand the difference, consider the following subroutine written in C:int sum(int addend1, int addend2){ return addend1 + addend2;}The subroutine "sum" has two parameters, named "addend1" and "addend2". 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).

The code which calls the "sum" subroutine might look like this:int sumValue;int value1 = 40;int value2 = 2;

sumValue = sum(value1, value2);The variables "value1" and "value2" are initialized with values. The variables are not arguments or parameters.

At runtime, the values assigned to these variables are passed to the subroutine "sum". In the "sum" subroutine, the parameters "addend1" and "addend2" are evaluated, yielding the arguments 40 and 2, respectively. The values of the arguments are added, and the result is returned to the caller, where it is assigned to the variable "sumValue".

Datatypes

In strongly-typed programming languages that are explicitly typed, each parameter's type is specified in the subroutine's declaration. Languages using type inference attempt to discover the types automatically from the function's body and usage, while weakly-typed programming languages defer type resolution to run-time.

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").

Argument passing

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.

Default arguments

Some programming languages such as Python, C++, and Windows PowerShell allow for a default argument to be explicitly or implicitly given in a subroutine's declaration. This allows the caller to omit that argument when calling the subroutine. If the default argument is explicitly given, then that value is used if it is not provided by the caller. 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.

PowerShell example:

function doc($g = 1.21) { "$g gigawatts? $g gigawatts? Great Scott!" }

PS> doc 1.21 gigawatts? 1.21 gigawatts? Great Scott!

PS> doc 88 88 gigawatts? 88 gigawatts? Great Scott!

Default arguments can be seen as a special case of the variable-length argument list.

Variable-length parameter lists

Some languages allow subroutines to be defined to accept a variable number of arguments. For such languages, the subroutines must iterate through the list of arguments.

PowerShell example:

function marty { $args | foreach { "back to the year $_" } }

PS> marty 1985 back to the year 1985

PS> marty 2015 1985 1955 back to the year 2015 back to the year 1985 back to the year 1955

Named parameters

Some programming languages allow subroutines to have named parameters. This allows the calling code to be more self-documenting. 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.

PowerShell example:

function jennifer($young, $old) { "Young Jennifer: I'm $young!" "Old Jennifer: I'm $old!" }

PS> jennifer 'old' 'young' Young Jennifer: I'm old! Old Jennifer: I'm young!

PS> jennifer -old 'young' -young 'old' Young Jennifer: I'm old! Old Jennifer: I'm young!

ee also

*Command-line argument
*Evaluation strategy
*Operator overloading
*Free variables and bound variables


Wikimedia Foundation. 2010.

Look at other dictionaries:

  • computer science — noun the branch of engineering science that studies (with the aid of computers) computable processes and structures • Syn: ↑computing • Topics: ↑computer, ↑computing machine, ↑computing device, ↑data processor, ↑electronic computer, ↑ …   Useful english dictionary

  • AP Computer Science — Advanced Placement Computer Science (also called APCS) is the name of two distinct Advanced Placement courses and examinations offered by the College Board to high school students as an opportunity to earn college credit for a college level… …   Wikipedia

  • Coupling (computer science) — In computer science, coupling or dependency is the degree to which each program module relies on each one of the other modules.Coupling is usually contrasted with cohesion. Low coupling often correlates with high cohesion, and vice versa. The… …   Wikipedia

  • Default (computer science) — A default, in computer science, refers to a setting or value automatically assigned to a software application, computer program or device, outside of user intervention. Such settings are also called presets, especially for electronic devices. The …   Wikipedia

  • Reflection (computer science) — In computer science, reflection is the process by which a computer program can observe and modify its own structure and behavior. The programming paradigm driven by reflection is called reflective programming .In most modern computer… …   Wikipedia

  • Session (computer science) — In computer science, in particular networking, a session is a semi permanent interactive information interchange, also known as a dialogue, a conversation or a meeting, between two or more communicating devices, or between a computer and user… …   Wikipedia

  • Closure (computer science) — In computer science, a closure (also lexical closure, function closure, function value or functional value) is a function together with a referencing environment for the non local variables of that function.[1] A closure allows a function to… …   Wikipedia

  • Callback (computer science) — In computer programming, a callback is executable code that is passed as an argument to other code. It allows a lower level software layer to call a subroutine (or function) defined in a higher level layer. Usually, the higher level code starts… …   Wikipedia

  • Branch (computer science) — For other uses, see Branch (disambiguation). A branch is sequence of code in a computer program which is conditionally executed depending on whether the flow of control is altered or not (at the branching point). The term can be used when… …   Wikipedia

  • Method (computer science) — In object oriented programming, the term method refers to a subroutine that is exclusively associated either with a class (called class methods, static methods, or factory methods) or with an object (called instance methods). Like a procedure in… …   Wikipedia


Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”

We are using cookies for the best presentation of our site. Continuing to use this site, you agree with this.