UevKF8zjRV_WEtpJsXhr

Multiple Parameters and Arguments

Multiple Parameters and Arguments

Creating Multiple Parameters

Functions can actually have more than one parameter, allowing for multiple pieces of information to be passed into a function. A game example might be assigning a player to a team. Here you would need a parameter to pass in the player, and a parameter to pass in which team to assign them to.

To set up multiple parameters, separate the parameter names with a comma.

Ask the class for examples of when a function might need to have more than one parameter.

Examples:

  • Adding numbers - (number1, number2)
  • Making a list of classmate’s birthdays - (name, birthday)
  • Giving players in-game currency - (player, numberCoins)
  • Buying potions in an in-game store - (item, howMany)

Practice with Multiple Parameters

To practice working with multiple parameters, create a function that sorts objects in your game into trash or recycling.

  • The first parameter will be trash, which will be destroyed.
  • The second parameter will be for recycling. That will be made shiny and new again.

Code the Function

  1. Create a new script in ServerScriptService.
  2. Code a new function named sortGarbage() with two parameters, trash and recyclable.
  3. At the bottom of the script, call the function.
  4. Check the code using a print statement inside the function.

Using Parameters

Inside the function, parameters can be used just like variables. In this example, trash is destroyed and then recyclable is given sparkles.

  1. Destroy whatever is passed in through the trash parameter the same as if trash were any other variable.
  1. Create a sparkle, then parent it to whatever gets passed in through recyclable.

Passing Values Through Parameters

Time to take out the trash! With the parameters set up, the functions are ready to receive values.

Set Up Values

  1. Create two objects using basic parts to represent things you might throw away. One should be trash and one recyclable. Make sure they have unique names.
Object
Name CementBlock GlassBottle
  1. At the top of the script, add variables for the objects you created.

Pass in Values

The next step is to pass the trash objects in. To do this, call the receiving function and pass both values in.

Example: sortGarbage(cementBlock, glassBottle)

Values that get passed through parameters are called arguments. In this example, the first argument is cementBlock, and the second is glassBottle.

  1. At the bottom of your script, where you called the sortGarbage() function, pass the trash and recyclable objects.
  1. Test your function. The object passed in first will be destroyed. The second object will sparkle.

Order of Parameters

Typically, arguments (values that get passed through) are matched to parameters based on the order they’re passed in. For instance, the first argument is used for the first parameter. The second argument is used for the second.

In our example, there are two parameters. Whatever gets passed in first will always be trash, and the second will always be recyclable.

Too Few Or Too Many Arguments

If you were to only pass in only one value, that value will be treated as trash. If you were to pass in three values, nothing would happen to the third value passed in.


  1. On a whiteboard or screen, have your classroom walk a student through how to create a function to subtract one number from a second number.
  2. Still on a white board, have three students pass in two or three numbers.
  3. Have the class call out the results for each set of numbers passed in.

Summary

Parameters are placeholders through which values can be passed into functions. Arguments are the values that get passed through the placeholder. When calling functions, the order that values get passed into the receiving function determines which parameter they’re used for.