skip navigation
Previous Next

Calling Methods

Calling Methods without Parameters

We have briefly discussed that you have been writing code inside the HandleMessage method, which is then somehow called (executed) by other code in another source code file. Methods are a very powerful feature of programming languages because it allows people to write code once, put it in a method, and reuse it forever.

Many methods, including all of the ones that we will call, return values. The idea here is that when you call a method, possibly giving it input, it will compute some value and return it back to your code. This is similar to a function in mathematics. When methods return a value back to our code, we need to store it in a variable, otherwise it will be lost. Here is the syntax for calling a method and storing the value returned into a variable:

<type> <variable name> = <method name>

For example, if a method existed named GetTheHobbitBookText, we could call this method and store the value returned like this:

string text = GetTheHobbitBookText();

Note that the variable name is still arbitrary here. Also, observe that we're using parentheses after the method name.

Now that we have the value returned from GetTheHobbitBookText stored into the string variable text, we can treat it like any other string; we could return it from the HandleMessage method or use it in string concatenation.

We have written a method for this tutorial named User.Input, which randomly returns one of the following strings: "yes", "no", or "maybe". To call User.Input and store the returned value into a string variable, we could write this code:

string answer = User.Input();

We have included the code for this method in a file in the chatbots directory, so you can call it from any of your chatbots.

Calling Methods Exercise 1

Try this exercise: Write a chatbot that will tell fortunes by answering "yes", "no", or "maybe" after a person runs it. As a first step, write a chatbot that will randomly respond with one of the following: "yes", "no", or "maybe". You should call the User.Input method once in your code. Here is one example response:


Next, use string concatenation to change to respond with "The great fortune teller bot's response is: " before the random answer of "yes", "no", or "maybe". Here is one example response:

The great fortune teller bot's response is: maybe

Here is a solution to this exercise: FortuneTellerBot.cs

Calling Methods with Parameters

We saw that the HandleMessage method takes parameters, which must be supplied by the code calling the method. Let's try to call a method with parameters. We have written the User.RandomChoice method, which takes a list of string parameters and randomly returns one of them. For example, this code inside HandleMessage:

string word = User.RandomChoice("Yes", "No", "Perhaps", "Maybe", "Not Likely", "Crane");
return word;

would cause 6 equally random responses. One of these responses is:


Calling Methods Exercise 2

Try this exercise: Write a chatbot that acts as a coin-flipper program that randomly responds "heads" or "tails". You should call the User.RandomChoice method once and only once in your HandleMessage method (or just say bot).

Here is a sample solution: CoinFlipper.cs

Previous Next
This work is supported by the following National Science Foundation Grants: CNS-087508, CNS-0549481, IIS-0811884, IIS-0415273
Send comments to Richard Ladner