This is what I got so far about parameters

This lecture was really hard to follow and the way that the subject was presented didn’t help either. I had to read a bunch of Q&A scatered around before I reached a conclusion by myself.
So far it would be nice if someone could tell me if my understanding is correct or not before posting it on Udemy for public view.

Warning: too many letters.

Parameters

  • void myMethod() {}

This is your recipe. This tells you how you want things to be done. Since there’s nothing inside the () it will only use what’s inside the standard recipe. myMethod is the name of the dish.

  • void myMethod(string greeting) {}

This is telling the myMethod the RULE that the recipe will use.
The main ingredient. Think of it as it was a challenge to make the dish myMethod with a specific kind of ingredient like how there are differents types of potato.
The first part inside () says the variable type, in this case is string. The second part is the name of said variable, in this case “greeting”, but it can be anything you want.
What this will do is modify anything inside your recipe that requests your “greeting” to the one you called in your method.

What’s inside the () here is called parameter.
If there isn’t anything in the recipe that uses the ingredient then you should just use the method above this one with blanc ().

  • myMethod(“Hello”)

This says: I want to use this recipe (myMethod) using this specific ingredient (“Hello”) (which is specifing an argument for the parameter). It’s worth noting that the argument MUST be the same type of the parameter. Thats why if it is a string parameter we use a string, if it is an integer parameter we use an int, etc.

What’s inside the () here is called argument.

  • Things to remember:

You can’t have an argument without a parameter OR a parameter without an argument. You can try and see what happens for yourself, thats how I learned.

Your methods declarations (your recipes) can be anywhere in the script, be it in the top, middle or bottom. The only thing that matters is the order inside de recipe since thats how the program will follow each step.

Hi Komaoto,

I’m sorry to hear that you found this lecture confusing. However, you seem to have understood everything correctly. :slight_smile:

Two little details:

You can’t have an argument without a parameter OR a parameter without an argument.

Actually, you can call a method without passing on an argument if you defined a default value for your parameter. For example:

void myMethod(string greeting = ""){}

In this case, the default value of greeting would be an empty string, but it could also be null or “Hello”.

The only thing that matters is the order inside de recipe since thats how the program will follow each step.

Yes, and the methods must be inside a class.

Thanks for the clarification. Didn’t knew about the default values, I will adjust my explanation when I post there. :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms