When working with any programming language, you will often hear the acronym, DRY. That is, do not repeat yourself.
By having similar bits of code sprinkled all throughout your app, one simple change becomes several changes which you may miss or introduce bugs.
Functions are a way for us to encapsulate code so we can call it on demand. Now instead of lots of places calling duplicate code,
we can have lots of places calling the same function. The function will run our code so when if we want to make changes, we need only change our function.
When we define our functions, we must designate a return type. For this, we have two different types.
The first type is known as a void function because it returns a void type. That is nothing.
Before I go on - let me just say that definition of void in Dart is not exactly correct. Void has a very unique meaning in dart which we’ll explore in a later episode in this part but for now, think of void as meaning nothing. We’ll circle around and build on this definition in a bit once you are comfortable with functions.
You may also have returning functions. Let’s say you have a multiply function. You pass in two numbers, the function does the calculation, then returns the result. This is a returning function. You return a result using the return keyword.
If we want to pass in values to our function, we use parameters. These parameters go in parenthesis after the function name. These are temporary variables that only exist in the body of our function.
Let’s get to work defining a few functions.
To get started, open a browser and head over to DartPad.dev. If you have code already present that’s not the example code, click the New Pad button and select the Dart option. Then click create. Alternatively, you can click the reset button to clear the old code.
You can see that the example code has a function already defined for us. This is the main function. You can see that the main function is a void function since the returning value is defined as void. There are no parameters.
The return type, the function name and parameter list is known as the function signature. Your code goes between the braces. This is the function’s body.
Let’s define a new function to print out a name. This function isn’t going to return a value so we’ll define it as a void function.
void
Now we’ll give it a name. Like variables, we start with a lowercase letter. We’ll call this print name.
void printName
Next, we’ll add parenthesis for our parameters. Mind you, you need the parenthesis whether you are using parameters or nots.
void printName()
Now, we’ll add our first parameter. We’ll define a name which is a type of String.
void printName(String name)
We’ll add the function body by using braces.
void printName(String name) {
}
That’s our function. Now let’s print out the name. We’ll simply use the print command.
void printName(String name) {
print(name);
}
Now let’s call our function. This should be very familiar as you’ve been using functions all along. The print command is just a function. Up on the main function, use your new printName function.
printName('Brian');
Now run your program and you’ll see your name printed out to the console several times. Okay, that’s nice, but what if we want to control the amount of times a name is printed out. First, let’s remove that for loop and move it our new function.
void printName(String name) {
for (int i = 0; i < 5; i++) {
print(name);
}
}
Now, let’s add a new parameter. Let’s call it repeat. We’ll add it after the name parameter. Simply, put a comma after the name and add the new parameter.
void printName(String name, int repeat) {
}
Doing so, you’ll notice we’ll get an error. We’ve changed our function signature which means we have to change any code that calls that function. Thankfully, the compiler will let you know where you need to change the code. Let’s print out our name ten times.
printName('Brian', 10);
Now, let’s update our printName function to use our repeat parameter.
for (int i = 0; i < repeat; i++) {
Run the code. You’ll see your name printed out the console ten times. Nice work!