ArticleslgStudy

computer science

Parameter (computer programming)

Parameter (computer programming) is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Parameter (computer programming) rather than just read about it. In short: In computer programming, a parameter, or formal argument, is a variable that represents an argument to a function call. A function's signature defines its parameters.

Key takeaways

  • Parameter (computer programming) belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Parameter (computer programming) to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Parameter (computer programming) from memory before moving on to harder problems.

Reference excerpt

In computer programming, a parameter, or formal argument, is a variable that represents an argument to a function call. A function's signature defines its parameters. A call invocation involves evaluating each argument expression of a call and associating the result with the corresponding parameter. For example, consider the Python function

Variables x and y are parameters, each of type int. For call add(2, 3), the expressions 2 and 3 are arguments. For call add(a + 1, b + 2), the arguments are a + 1 and b + 2. Parameter passing is defined by a programming language. Evaluation strategy defines the semantics for how parameters can be declared and how arguments are passed to a function. Generally, with call by value, a parameter acts like a new, local variable initialized to the value of the argument. If the argument is a variable, the function cannot modify the argument state because the parameter is a copy. With call by reference, which requires the argument to be a variable, the parameter is an alias of the argument.

Example The following C source code defines a function named salesTax with one parameter named price; both the function and parameter are typed double. For call salesTax(10.00), the argument 10.00 is passed to the function as the double value 10 and assigned to parameter variable price, and the function returns 0.5.

Parameters and arguments The terms parameter and argument may have different meanings in different programming languages. Sometimes they are used interchangeably, and the context is used to distinguish the meaning. The term parameter (sometimes called formal parameter) is often used to refer to the variable as found in the function declaration, while argument (sometimes called actual parameter) refers to the actual input supplied at a function call statement. 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. A parameter is an (unbound) variable, while the argument can be a literal or variable or more complex expression involving literals and variables. 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. The specification for pass-by-reference or pass-by-value would be made in the function declaration and/or definition. Parameters appear in procedure definitions; arguments appear in procedure calls. 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. Loosely, a parameter is a type, and an argument is an instance. A parameter is an intrinsic property of the procedure, included in its definition. 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. In general, a procedure may be defined with any number of parameters, or no parameters at all. If a procedure has parameters, the part of its definition that specifies the parameters is called its parameter list. By contrast, the arguments are the expressions supplied to the procedure when it is called, usually one expression matching one of the parameters. Unlike the parameters, which form an unchanging part of the procedure's definition, the arguments may vary from call to call. Each time a procedure is called, 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 sometimes thought of as the actual values or references assigned to the parameter variables when the function is called at run-time. When discussing code that is calling into a function, any values or references passed into the function 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 function definition, the variables in the function's parameter list are the parameters, while the values of the parameters at runtime are the arguments. Consider the following C function, sum, which has two parameters, a and b. It adds the values passed into the parameters, and returns the result to the function's caller.

The following is an example of calling sum. The variables x and y are initialized and then passed to sum as the arguments. At runtime, the values assigned to these variables are passed to sum. In sum, the parameters a and b 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 result.

Because of the difference between parameters and arguments, it is possible to supply inappropriate arguments to a procedure. 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. 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.

Alternative convention in Eiffel Within the Eiffel software development method and language, the terms argument and parameter have distinct uses established by convention. 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. Consider the following routine definition:

The routine sum takes two arguments a and b, which are called the routine's formal arguments. A call to sum specifies actual arguments, as shown below with x and y.

Parameters are also thought of as either formal or actual. Formal generic parameters are used in the definition of generic classes. 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:

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Parameter (computer programming)

Start with the simplest possible case. Write down what Parameter (computer programming) claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Parameter (computer programming) before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Parameter (computer programming) ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Parameter (computer programming)

In research
Parameter (computer programming) appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Parameter (computer programming) in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Parameter (computer programming) is common in secondary-school and first-year university syllabi. It links to neighbouring topics Subroutines, Variable (computer science), so understanding it makes those chapters shorter.
In everyday life
Look for Parameter (computer programming) outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study Parameter (computer programming) in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Parameter (computer programming) means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Parameter (computer programming) out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Parameter (computer programming) in simple terms?

In computer programming, a parameter, or formal argument, is a variable that represents an argument to a function call. A function's signature defines its parameters.

Why does Parameter (computer programming) matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Parameter (computer programming)?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Parameter (computer programming).

Tags

  • Subroutines
  • Variable (computer science)

Keep exploring