Lably
Skip to content

Parameter (computer programming)

Variable that represents an argument to a function

  • 20 min read
  • 5 chapters
  • 2 labs on Lably

Learn it on Lably

For example, consider the Python function

<span id="mwHw"></span><span class="k" id="mwIA">def</span><span class="w" id="mwIQ"> </span><span class="nf" id="mwIg">add</span><span class="p" id="mwIw">(</span><span class="n" id="mwJA">x</span><span class="p" id="mwJQ">:</span> <span class="nb" id="mwJg">int</span><span class="p" id="mwJw">,</span> <span class="n" id="mwKA">y</span><span class="p" id="mwKQ">:</span> <span class="nb" id="mwKg">int</span><span class="p" id="mwKw">)</span> <span class="o" id="mwLA">-></span> <span class="nb" id="mwLQ">int</span><span class="p" id="mwLg">:</span> <span class="k" id="mwLw">return</span> <span class="n" id="mwMA">x</span> <span class="o" id="mwMQ">+</span> <span class="n" id="mwMg">y</span>

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.

<span id="mwUA"></span><span class="kt" id="mwUQ">double</span><span class="w" id="mwUg"> </span><span class="nf" id="mwUw">salesTax</span><span class="p" id="mwVA">(</span><span class="kt" id="mwVQ">double</span><span class="w" id="mwVg"> </span><span class="n" id="mwVw">price</span><span class="p" id="mwWA">)</span><span class="w" id="mwWQ"> </span><span class="p" id="mwWg">{</span> <span class="w" id="mwWw"> </span><span class="k" id="mwXA">return</span><span class="w" id="mwXQ"> </span><span class="mf" id="mwXg">0.05</span><span class="w" id="mwXw"> </span><span class="o" id="mwYA">*</span><span class="w" id="mwYQ"> </span><span class="n" id="mwYg">price</span><span class="p" id="mwYw">;</span> <span class="p" id="mwZA">}</span>

Sign in to keep reading

4 more chapters are free with a Lably account.

More in Computing and communication