Multiple Parameters and Arguments
Multiple Parameters and Arguments
Students will be able to:
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.
Classroom Discussion - Examples of Multiple Parameters »
Ask the class for examples of when a function might need to have more than one parameter.
- Adding numbers -
- Making a list of classmate’s birthdays -
- Giving players in-game currency -
- Buying potions in an in-game store -
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
- Create a new script in ServerScriptService.
- Code a new function named
sortGarbage()with two parameters,
- At the bottom of the script, call the function.
- Check the code using a print statement inside the function.
Inside the function, parameters can be used just like variables. In this example,
trash is destroyed and then
recyclable is given sparkles.
- Destroy whatever is passed in through the trash parameter the same as if trash were any other variable.
- 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
- 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.
- 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.
Values that get passed through parameters are called arguments. In this example, the first argument is
cementBlock, and the second is
- At the bottom of your script, where you called the
sortGarbage()function, pass the
- 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
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.
Concept Check »
- 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.
- Still on a white board, have three students pass in two or three numbers.
- Have the class call out the results for each set of numbers passed in.
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.
Previous Parameters and Events 2: Buttons Next Using if/then Statements