Parameters & Arguments in 60 seconds
Think: Teleporters.
Some functions require a bit of outside information to do their job. A simple example would be a damage calculator in a video game. At it’s essence, damage calculation could be boiled down to “current health minus damage”
So in order to make a function out of this, we’d give the function a name, then give it two parameters. These parameters are like little portals for information.
When defining the function, we create an “in” portal called a parameter, and we’ll use the “out” portal like a variable inside the function. The image below contains an example in C#, with two parameters: currentHealth
and damageIssued
. Let’s focus in closely on just one of those parameters.
Note: Because C# is a static language, I told it to expect an integer and to return an integer. Dynamic languages don’t care about the type.
To use this function, I simply need to pass in an argument. The argument is just a fancy way of saying “passing information to this parameter.”
When I pass the argument “15” to the calculateDamage
parameter damageIssued
, the function will return the integer “85”.