Why do we put something into the parenthesesof the ShowMainMenu method?

All right, I’m confused. Here’s what I have

    void Start()
    {
        ShowMainMenu("Hello Noah");
    }
   
    void ShowMainMenu (string greeting) {
        Terminal.ClearScreen();
        Terminal.WriteLine(greeting);
        Terminal.WriteLine("What would you like to hack into?");
        Terminal.WriteLine("Press 1 for the local library");
        Terminal.WriteLine("Press 2 for the police station");
        Terminal.WriteLine("Enter your selection:");
    }

Why do we add “Hello Noah” in the variable (I think) ShowMainMenu? How does that declare that the string greeting is “Hello Noah”? Do the brackets of the first ShowMainMenu (the one in the Start function) and the brackets for the ShowMainMenu function correspond?

Hi Noah,

In C#, all methods need parentheses behind their name. ShowMainMenu is defined with a parameter meaning that the method expects data. To send data to a method, you put the expected value or object reference into the parentheses. Does that make sense so far?

A variable is a data container. In your example, "Hello Noah" is not a variable because you cannot reuse it. It is a string, just as the ShowMainMenu method expects it. This data gets assigned to the greeting variable which exists only within the scope of the ShowMainMenu method.

This would be an example with a variable:

    void Start()
    {
        string s = "Hello Noah";
        ShowMainMenu(s);
    }

I named it differently on purpose to show you that you do not pass on the variable but the data the variable contains. Of course, you could name the variable greeting, too, because the name is unambiguous within the scope of the Start method.

Did this clear it up for you?

Thank you so much! I completely understand it now.

I’m glad I was able to help. :slight_smile:


See also:

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

Privacy & Terms