Nepedia
Back to Discover
Advanced Reader
💻

Parameter (computer programming)

technology Maturity 11-13 Next article →

In computer programming, a parameter is a variable that represents an argument during a function call. A parameter is often called a formal argument. It is an intrinsic property of a procedure, meaning it is built directly into the function's definition. You can think of a parameter as a type or a blueprint, while the argument is a specific instance of that type. A function's signature defines exactly which parameters it requires to operate. This structure allows programmers to write general rules that can be applied to many different specific values.

When a programmer uses a function, they perform a call invocation. This process involves evaluating each argument expression and associating the result with the corresponding parameter. The set of parameters in a definition is known as the parameter list. Similarly, the set of expressions supplied during a call is called the argument list. For example, in the Python function `def add(x: int, y: int)`, the variables `x` and `y` are parameters. If you call `add(2, 3)`, the numbers 2 and 3 are the arguments. If you call `add(a+1, b+2)`, the expressions `a+1` and `b+2` serve as the arguments.

The way data moves from the argument to the parameter is called argument passing. This mechanism is determined by the programming language's evaluation strategy. One common method is call by value. In this strategy, the parameter acts like a new, local variable that is initialized with a copy of the argument's value. Because the function only has a copy, it cannot modify the original argument's state. Another method is call by reference. This requires the argument to be a variable. In this case, the parameter acts as an alias for the original variable. This means the function works directly with the original data.

Programming languages handle data types and parameters in various ways. In strongly typed languages, every parameter's type must be specified in the procedure declaration. Some languages use type inference to discover these types automatically from the function's body. Other languages, like dynamically typed ones, wait until run-time to resolve types. Weakly typed languages perform very little type resolution. Some functions may have no parameters at all. In formal type theory, such functions take an empty parameter list, often referred to as a unit type.

Programmers must ensure that the argument list matches the parameter list perfectly. If a call supplies too many or too few arguments, a mismatch occurs. This can happen if the arguments are in the wrong order or are of the wrong type. Such errors often cause the procedure to return an unintended answer or trigger a runtime error. To prevent this, some languages like Ada, C++, and Python allow for default arguments. If a caller omits an argument, the computer uses a pre-set value. Some languages provide an implicit default, such as zero or an empty string.

Advanced features provide even more flexibility in how functions operate. Some languages, including PowerShell, support named parameters. This allows the caller to change the order of arguments or omit them by using their specific names. Other languages allow for variable-length argument lists. This means a function can accept any number of arguments and must iterate through them. In functional programming, a concept called currying is used to handle multiple parameters. In languages like Haskell or ML, every function has exactly one parameter. A function that appears to have multiple parameters actually takes one argument and returns a new function.

Parameters can also serve different roles depending on the direction of data flow. While most parameters are used for input, some act as output parameters. These are also known as out parameters or return parameters. Using output parameters is a common practice in C and C++ through call by reference. Other languages, such as Ada, Fortran, C#, and Swift, have built-in support for specifying these modes. This allows a function to pass information back to the caller through the parameter itself. Understanding these distinctions helps programmers manage complex data systems efficiently.

681 words

Where will your curiosity go next?

Keep exploring at your chosen reading level.

More to explore

What is Nepedia?

A free, ad-free encyclopedia for children. Every article is written at five reading levels, so the same page works for a five-year-old and a fifteen-year-old — use the level switcher above to see this one change. No account needed to read.