c# · level 1 · unit 7 · lesson 2
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
A is a slot where your function catches information.
Picture your function as a machine. Right now it does the same thing every time. Cut a slot in the front, and you can feed in something new on each go.
That slot is a parameter: a box in the function's brackets, with a type and a name, like string name in void Greet(string name). Inside the function, use it like any other box.
The value you drop in when you call is the . Call Greet("Mia") and name holds "Mia". Call Greet("Leo") and the same machine greets Leo instead.
using System;
Greet("Mia");
Greet("Leo");
void Greet(string name)
{
Console.WriteLine($"Hello, {name}!");
} Need two slots? Separate the parameters with commas, and the arguments too.
guess first
Two slots this time. What will this show?
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
using System;
Order("lemonade", "chips");
void Order(string food, string drink)
{
Console.WriteLine($"I would like {food} with {drink}.");
}
your turn
Your turn. Run the birthday card machine.
Type a name between each pair of quotes, and an age, as a whole number, after each comma. Then run it and watch one machine make two cards.
output
press run to see what happens
fix it
Rex is hungry, but this will not run. Can you fix it?
output
press run to see what happens
- Read the error. Which name does C# say does not exist?
- The parameter on line 5 is called pet, but line 7 uses a different name.
- Change animal to pet inside the curly brackets on line 7, then run.
nailed it
your functions take orders now. :D
- A parameter is a slot in the function's brackets.
- An argument is the value you drop in when you call.
- Arguments fill parameters in order: first into first.
new words in your collection
- parameter
- argument
This lesson isn’t saved. A free account remembers every lesson you finish.