skip navigation
 
 
 
WebInSight
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've using parens 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:

yes

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:

Perhaps

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

Conditionals

The first step toward doing something intelligent is being able to choose to take a different action based on the input that you're given. For example, if you're given chocolate cake you might choose to eat it, else if you're given Brussels sprouts you might not. Conditional expressions allow you to specify similar options for your programs.

if

The most simple conditional is an if statement. Let's first look at the syntax:

if (<test>) {
	<statements>
}

An if statement defines a block of code (with curly braces) that is only executed if a test (or condition) is true. Here is a more concrete example:

string response = "I didn't enter the if statement";
if (message == "you may enter") {
	response = "entered the if statement";
}
return response;

Copy this example into your chatbot's HandleMessage method body, compile, run, and observe its behavior. What is the response if you type anything besides "you may enter"? How about if you type "you may enter"? The == (two equal signs) symbol means equals, as opposed to = (one equal sign), which means 'is assigned to'. The code here says to enter the if statement only if the two strings are equal.

Here is the full source for this example: IfBot.cs

if-else

The following code that could be placed in the HandleMessage method causes the chatbot to respond with "Yum!" if the message received is "cake". Otherwise, the chatbot responds with "Yuk. No thanks."

if(message == "cake") {
	return "Yum!";
} else {
	return "Yuk. No thanks.";
}

The if-else statement is very similar to the if statement. In an if-else statement, if the test is true, then the if block is executed, else (if the test is false) the else block is executed. If the first string is not equal to the second string, the computer will follow the instructions in the block of code defined by the curly brackets following the "else."

Here is the full source for this example: IfElseBot.cs

You can also test to see if two things are not equal by using != instead of ==. In computer science lingo, the exclamation point is pronounced "not," so != is pronounced not equals.

if-else-if

There is one more variant of the if statement that is useful when you want to enter one of many blocks conditionally, instead of just one of two. Observe the following code sample:

if (message == "one") {
	return "Entered the first block";
} else if (message == "two") {
	return "Entered the second block";
} else {
	return "Entered the third block";
}

The if-"else if"-else provides a way to chain together many if-else statements so that only one of the conditional blocks are executed. This can be useful in a chatbot because we can only return one time per HandleMessage execution. Let's look at a more practical example:

if (message == "hi") {
	return "hello";
} else if (message == "hey") {
	return "hello";
} else if (message == "bye") {
	return "Alright, see you later";
} else {
	return "Good conversation. Let's keep it rolling!";
}

Here is the full source for this example: IfElseIfBot.cs

Nested conditionals

Since if statements define blocks of code that can have any statements inside of them, it is possible to put another if statement inside this block. This technique is known as nesting conditionals. We'll talk more about this in a later section.

Previous Next
 
Comments to Jeffrey P. Bigham