c# · level 1 · unit 7 · lesson 1
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
A is a recipe card with a name on top.
You have used commands like Console.WriteLine. Now make your own. A function is a set of instructions under one name. Write the recipe once, then use it as often as you like.
To one, write , a name with a capital letter, empty brackets and a block. void means it does a job but hands nothing back. Put your functions below the main lines.
To it, write its name with brackets and a semicolon, like Cheer();. Every line in its block then runs, top to bottom. Call it twice and it runs twice.
using System;
Cheer();
Cheer();
void Cheer()
{
Console.WriteLine("Go team!");
Console.WriteLine("You can do it!");
} C# reads the whole file before it runs, so a call can come before the function it uses.
guess first
Kick off! In what order will this show?
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
using System;
Console.WriteLine("Kick off!");
Goal();
Console.WriteLine("Full time!");
void Goal()
{
Console.WriteLine("GOAL!");
}
your turn
Your turn. Write your own chant.
Pick a name for your function, starting with a capital letter, and type it in all three name blanks. Put any words you like between the quotes, then run it.
output
press run to see what happens
fix it
The dog will not bark. Can you fix it?
output
press run to see what happens
- The function is written perfectly. Look at line 3, where you try to use it.
- Writing a function's name on its own does not call it. Something is missing after the name.
- Add brackets after Bark on line 3, so it says
Bark();, then run.
nailed it
you wrote your very own command. :D
- A function is a set of instructions with a name.
- Define it with
void, a name, brackets and a block. - Call it with its name, brackets and a semicolon.
new words in your collection
- function
- define
- call
- void
This lesson isn’t saved. A free account remembers every lesson you finish.