kids encyclopedia robot

Parameter (computer programming) facts for kids

Kids Encyclopedia Facts

In computer programming, a parameter is like a special placeholder. It's a variable that a small program, called a subroutine or function, uses. This placeholder helps the function know what information it needs to do its job.

Think of it like a recipe. The recipe might say "add (amount) of sugar." Here, "(amount)" is the parameter. When you actually make the cake, you decide the amount, like "add 2 cups of sugar." The "2 cups" is the argument.

So, a parameter is the name given in the function's definition. An argument is the actual value you give to that parameter when you use the function.

For example, if you have a function called `add(x, y)`:

  • `x` and `y` are the parameters. They are placeholders for numbers.
  • If you call it like `add(5, 3)`, then `5` and `3` are the arguments. They are the actual numbers used.

When you call a function, the computer takes the arguments you provide. It then gives these values to the matching parameters inside the function. This lets the function work with the specific data you gave it.

How Parameters Work: An Example

Let's look at a simple example in the C programming language. This program defines a function called "SalesTax." It has one parameter named "price."

double SalesTax(double price)
{
  return 0.05 * price;
}

This function calculates sales tax. It takes a `price` (which is a number with decimals, like $10.00). It then returns 5% of that price.

Now, let's see how you would use this function:

SalesTax(10.00);

In this example, `10.00` is the argument given to the function.

  • When `SalesTax(10.00)` is called, the value `10.00` is given to the `price` parameter.
  • Inside the function, it calculates `0.05 * 10.00`, which equals `0.50`.
  • The function then "returns" this result, `0.50`. So, the sales tax on $10.00 is $0.50.

Parameters and Arguments: A Closer Look

The words parameter and argument are often used to mean the same thing. However, in computer science, they have specific meanings.

  • A parameter (also called a formal parameter) is the variable listed in the function's definition. It's like a blank space waiting for information.
  • An argument (also called an actual parameter) is the actual value or data you provide when you use the function.

Imagine a function that adds two numbers: `int Sum(int addend1, int addend2)`.

  • `addend1` and `addend2` are the parameters. They are placeholders for the numbers to be added.

Now, if you call this function:

int value1 = 40;
int value2 = 2;
int sum_value = Sum(value1, value2);
  • Here, `value1` and `value2` are the arguments. They are the actual numbers (40 and 2) that will be used.
  • When `Sum(value1, value2)` is called, the value `40` goes into `addend1`. The value `2` goes into `addend2`.
  • The function then adds `40 + 2` and returns `42`. This `42` is stored in `sum_value`.

It's important to give the correct arguments to a function. If you give too many, too few, or the wrong type of arguments, the program might not work. It could even crash!

Data Types

In many programming languages, you need to tell the computer what kind of data a parameter will hold. This is called its data type. For example, you might say a parameter will hold a whole number (`integer`) or text (`string`).

Some languages try to guess the data type for you. Others are very strict and need you to specify it.

Passing Arguments

The way arguments are given to parameters is called argument passing. The most common way is called call by value. This means the function gets a copy of the argument's value. If the function changes the parameter, it only changes its own copy, not the original value outside the function.

Default Arguments

Some programming languages let you set a default argument for a parameter. This means if you don't provide an argument for that parameter when you call the function, it will use the default value.

Here's an example in PowerShell:

function doc($g = 1.21) {
    "$g gigawatts? $g gigawatts? Great Scott!"
}
  • If you call `doc()`, it uses the default value `1.21` for `$g`.
  • If you call `doc(88)`, it uses `88` instead of the default.

Variable-Length Parameter Lists

Some functions can take a flexible number of arguments. This means you don't have to know exactly how many arguments you'll give it beforehand. The function can then go through all the arguments you provided.

PowerShell example:

function marty {
    $args | foreach { "back to the year $_" }
}
  • `marty 1985` will say "back to the year 1985".
  • `marty 2015 1985 1955` will say "back to the year 2015", then "back to the year 1985", and so on.

Named Parameters

In some languages, you can give arguments by naming the parameter they belong to. This makes your code easier to understand. It also means you don't have to worry about the order of the arguments.

PowerShell example:

function jennifer($adjectiveYoung, $adjectiveOld) {
    "Young Jennifer: I'm $adjectiveYoung!"
    "Old Jennifer: I'm $adjectiveOld!"
}
  • You can call it normally: `jennifer 'fresh' 'experienced'`.
  • Or you can name the parameters: `jennifer -adjectiveOld 'experienced' -adjectiveYoung 'fresh'`. The result is the same!

Output Parameters

Most parameters are used for input: you give information to the function. But sometimes, a parameter is used for output. This means the function uses that parameter to send information back to the main program. These are called output parameters.

Imagine you want a function to calculate both the width and height of something. Instead of returning just one value, it can use output parameters to give you both.

For example, in C#, a function might look like this:

public static bool TryParse(string s, out int result)

This `TryParse` function tries to turn a `string` (text) into an `int` (whole number).

  • `s` is an input parameter (the text to convert).
  • `result` is an `out int` (an output parameter). If the conversion works, the number will be stored in `result`.
  • The function itself returns `true` if it worked, `false` if it failed.

You might use it like this:

int myNumber;
if (Int32.TryParse("123", out myNumber)) {
    // If it worked, myNumber will now be 123
} else {
    // If it failed (e.g., "abc"), handle the error
}

Output parameters are useful when a function needs to give back more than one piece of information.

Images for kids

See also

  • Command-line argument
  • Evaluation strategy
  • Operator overloading
  • Free variables and bound variables
kids search engine
Parameter (computer programming) Facts for Kids. Kiddle Encyclopedia.