Programming in Dart: Functions & Closures

Jun 21 2022 · Dart 2.16, Flutter, DartPad

Part 1: Meet the Function

05. Challenge: Create a Function

Episode complete

Play next episode

Next
About this episode
Leave a rating/review
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 04. Understand Named Parameters Next episode: 06. Store a Function

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Transcript: 05. Challenge: Create a Function

At this point, you should feel pretty good idea about working with functions. So naturally, I have a challenge with you. In this episode, you’re going to get three challenges. So let’s get started.

For your first challenge, I want you to write a function named youAreWonderful, with a String parameter called name. It should return a string using name, and say something like “You’re wonderful, Douglas.”.

Pause the video and try it out.

Hopefully that challenge wasn’t too hard. Let’s dive into now. First, let’s define a function called youAreWonderful. This will return a String.

String youAreWonderful

Next, let’s pass in the name parameter.

String youAreWonderful(String name) {

}

Okay, now let’s return the text from the function.

return 'You are wonderful, $name';

At this point, we have to call it. In the main function, add the following:

print(youAreWonderful('Douglas'));

Now run the code. Look at that - Douglas is wonderful indeed.

Okay, that first challenge wasn’t too hard. Let’s take it up a notch. Add another int parameter to that function called numberPeople so that the function returns something like “You’re wonderful, Douglas. 10 people think so.”. Pause the video and try it out.

Okay - now for the next level. The first thing we’ll do is update our function body to add a new parameter. This will take in the numberOfPeople.

String youAreWonderful(String name, int numberOfPeople) {

Now let’s update the return String with the numberOfPeople added to it.

return 'You are wonderful, $name. $numberOfPeople people think so.';

Finally, let’s update the calling area to pass in the number of people.

print(youAreWonderful('Douglas', 10));

When we run the code, we get our newly updated message. Awesome job, but we aren’t done just yet.

Now for the final level of the challenge. I want you to make both inputs named parameters. Make name required and set numberOfPeople to have a default of 30. Pause the video and try it out.

Okay - how was that? Let’s make our alterations. First, let’s make some named parameters by surrounding them with braces.

String youAreWonderful({ String name, int numberOfPeople } ) 

We’re getting some errors because Dart expects optionals to be used but we haven’t defined them. First lets make the name required.

String youAreWonderful({ required String name, int numberOfPeople } ) 

Next we provide a default value for the number of people.

String youAreWonderful({ required String name, int numberOfPeople = 30} ) 

Now we get an error in our calling code. Whenever you change a function signature, you’ll often run into errors. Thankfully, Dart lets us know exactly where the problem is. Let’s update it now.

print(youAreWonderful(name: 'Douglas', numberOfPeople: 10));

Let’s also add another one.

print(youAreWonderful(name: 'Jeremy'));

This time, we don’t specify the number of people, but our function has us covered. When we run the program, we get our message. Well done.