Our First Function

Alright, this section might be a bit mind-blowing, because it will totally change how you think about code. It might seem a little confusing at once, but hang in there, you’ll figure it out with practice!

Why Repeat Yourself?

Take a look at the following program. It should look pretty familiar to you now - it’s prompting the user for input, then reading the input back into variables.

using System;

//get the user's first name
Console.Write("What's your First Name?");
string firstName = Console.ReadLine();

//get the user's last name
Console.Write("What's your last name?");
string lastName = Console.ReadLine();

//get the user's pet name
Console.Write("What's your pet's name?");
string petName = Console.ReadLine();

But I’d like you to take a look at the overall pattern in this code.

There are 3 sections here, and they’re all very similar. The only thing that’s different for each of these 3 sections is the text that’s written to the console, and the variable that stores the user input.

Try It Now: Writing our function

So let’s write a function that does all of the common stuff above, and then just call it 3 times and let it do the ‘dirty work’ for us.

Go ahead and write this out, exactly as you see it below. Write it above all of the other code in your file (after the using System; line)

string GetUserInput(string prompt)
{

}

There is a lot of new stuff happening here, so lets break this down!

On the first line above, we have what’s called a function declaration:

This is a bit like a variable declaration - first, we have a type (in this case, string). Next, we have the function name, (in this case GetUserInput).

Then, we have a set of parenthesis, with “string prompt” in between them. The “string prompt” between the parenthesis looks a bit like a variable declaration, doesn’t it?’ That’s a “parameter” - an input value that will be passed into this function.

Now on the line next to the function declaration, we have a curly brace facing to the right, and then another curly brace facing left.

Those curly braces tell the computer where the code for the function will start and end. So we have delcared a function called GetUserInput. It takes a single string parameter called ‘prompt’, and it has a return type of ‘string’, which means that the function needs to return a string when it’s done.

Giving our function… Functionality!

So now that we’ve written our function, ‘let’s put some statements inside it, between the two curly braces:

string GetUserInput(string prompt)
{	
	Console.Write(prompt);	
	string userInput = Console.ReadLine();
	return userInput;
}

This code is doing exactly what each of the 3 sections above do. Except, instead of passing in a specific string to the Console.WriteLine() function, we are passing in the “prompt” parameter.

To say that another way:

Our function takes a string parameter called ‘prompt’, and we pass that parameter into Console.WriteLine() - which it will then write to the Console.

We also don’t have a specific variable name here, like “UserName” - instead, when we call Console.ReadLine(), we store it in a generic sounding variable called UserInput. Then we “return” that UserInput variable, which is another way of saying we’re goint to pass it back to whatever code called this function.

Calling our function

Ok, we have written our function - now let’s modify the rest of our program to call this function, passing in our prompts, and storing the values the function returns. Your program should look something like this:

using System;

string GetUserInput(string prompt)
{
	Console.Write(prompt);
	string userInput = Console.ReadLine();
	return userInput;
}


string name = GetUserInput("What's your first name?");
string lastName = GetUserInput("What's your last name?");
string petName = GetUserInput("What's your pet name?");

This is kind of cool, isn’t it? We can now do in one line what used to take 2 lines. The more complex our code gets, the more useful it becomes to put code into functions.


Previous submodule:
Next submodule: