Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
      You have completed C# Basics!
      
    
You have completed C# Basics!
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 
Askmethod that takes aquestionparameter in the form of a string.- For now, we'll just print out the 
questionparameter. 
 - For now, we'll just print out the 
 - In our 
Mainmethod, we'll callAskwith 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.ReadLinemethod 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 ananswervariable. - 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 
Mainmethod.- We learned about variable scope earlier, and so we know that the 
answervariable is only in scope within theAskmethod. - 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 
AddandSubtractmethods, they'll return adoublevalue. - Return type doesn't have to match the parameter types; you can accept an 
intand astringparameter 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