Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
Let's get back to our cat food store program. We're still working on asking the user how many cans they want to order, and storing the number they enter. Now that we know how to write methods and pass arguments to them, we know most of what we need to add this feature.
Let's get back to our cat food store program. We're still working on asking the user how many cans they want to order, and storing the number they enter.
Cat Food Store Features
Display welcome message- Ask for quantity
- Calculate total
- Discount for large orders
Now that we know how to write methods and pass arguments to them, we know most of what we need to add this feature. Let's give it a try now.
- We'll create a new
Ask
method that takes aquestion
parameter in the form of a string.- For now, we'll just print out the
question
parameter.
- For now, we'll just print out the
- In our
Main
method, we'll callAsk
with the question:Ask("How many cans are you ordering?");
static void Ask(string question)
{
Console.WriteLine(question);
}
static void Main()
{
Console.WriteLine("Welcome to the cat food store!");
Ask("How many cans are you ordering?");
}
- We need to get text user enters at keyboard
- The
Console.ReadLine
method can do that for us - When we call
ReadLine
, it waits for the user to type something and press Enter, and then returns what the user typed. - In this version of
Ask
, we assign the value the user typed to ananswer
variable. - Then we print the value in
answer
.
- The
static void Ask(string question)
{
Console.WriteLine(question);
string answer = Console.ReadLine();
Console.WriteLine(answer);
}
- But really, we need to be able to access the user's answer in the
Main
method.- We learned about variable scope earlier, and so we know that the
answer
variable is only in scope within theAsk
method. - I could try to access it within
Main
:Console.WriteLine(answer);
- ...But I'll get a compile error when I try to run this:
dotnet run
- We learned about variable scope earlier, and so we know that the
We need a way to get the user's answer from the Ask
method back to the Main
method.
- Some methods have a return value, a value they send back to the code that called them.
- At the top of a method definition, in place of
void
, write the type of value the method will return. - In the case of our
Add
andSubtract
methods, they'll return adouble
value. - Return type doesn't have to match the parameter types; you can accept an
int
and astring
parameter and return abool
, for example.
- At the top of a method definition, in place of
using System;
class Program
{
static double Add(double first, double second)
{
return first + second;
}
static double Subtract(double first, double second)
{
return first - second;
}
static void Main(string[] args)
{
double total = Add(3, 5);
Console.WriteLine(total);
double remaining = Subtract(21.3, 7.1);
Console.WriteLine(remaining);
}
}
- Pass return values to other functions
static void Main(string[] args)
{
// We don't have to assign the return value to a variable first.
// We can just pass it straight to WriteLine.
Console.WriteLine(Add(3, 5));
Console.WriteLine(Subtract(21.3, 7.1));
}
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up