Chevron Left Icon
Method Calls and Input
Lesson 1 of 2
Chevron Right Icon
  1. 1
    Imagine making a hamburger: 1. Place the bread down 2. Add the burger patty 3. Add the pickles 4. Place the bread on top What if you had to say each step every time you ordered a hamburger? It’s …
  2. 2
    You’ve been using methods since you started learning C#! Commands like Console.WriteLine() and Math.Min() are methods. Each method has a different behavior: The first method prints something to t…
  3. 3
    Like a math function or a factory machine, a method takes input and returns output. We’ve just seen how input works (arguments). Let’s see how output works. When a method returns a value, it e…
  4. 4
    Up until now, you’ve been calling built-in methods: methods that are available whenever you use C#. Sometimes you need a custom method for your specific program. In that case, you’ll need to define…
  5. 5
    Remember calling methods with arguments, like Math.Min(3, 4)? Methods that you define can use arguments as well, making them more versatile and useful. While we are defining our method, we don’t k…
  6. 6
    One thing to watch for with parameters: they can only be used inside their method! static void YourMethodName(string message) { Console.WriteLine(message); } Console.WriteLine(message); // causes…
  7. 7
    To make our functions even more flexible, we can make certain parameters optional. If someone calls your method without all the parameters, the method will assign a default value to those missing…
  8. 8
    Say your method has lots of optional parameters, but you only want to specify one when you call it. In this example, your method has five optional parameters: static void YourMethodName(int a = 0…
  9. 9
    Say you want to use Math.Round(), a built-in method. You go to the Microsoft documentation to learn how to use it, and find at least…
  10. 10
    You learned a lot this lesson: congrats on finishing! Here’s what you’ve covered: Call a method with its name and parentheses: VisitPlanets(); Store a method’s returned value in a variable: do…
  1. 1
    What’s the outcome of calling a method? Sometimes a message is printed to the console: Console.WriteLine(“Hello World!”); Sometimes a value is returned: Math.Floor(15.6); // Returns 15 Sometimes…
  2. 2
    The basic way to return values from a method is to use a return statement! (A well-constructed programming language shouldn’t have a lot of surprises.) static string Yell(string phrase) { ret…
  3. 3
    As we mentioned before, we don’t like surprises — they lead to mistakes. So, when we call a method, we’d like to know what type of value will be returned. This is done in the method definitio…
  4. 4
    A method can only return one value, but sometimes you need to output two pieces of information. For example the Int32.TryParse() method tries to parse its input as an integer. If it can, it ret…
  5. 5
    We can use out parameters in our own methods as well. In this example, Yell() converts phrase to uppercase and sets a boolean variable to true: static string Yell(string phrase, out bool wasYellCa…
  6. 6
    As with return, out is a very useful keyword, but it can lead to errors if used incorrectly. Here are two common ones: This error means that the out parameter needs to be assigned a value within t…
  7. 7
    Congrats on finishing! You can now use and define methods with output. Here’s what else you’ve learned in this lesson: Methods return values with the return keyword. Every method has a return …

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo