LogoDart Beginner Tutorials

Part 2: Feeding Time! – Introducing Functions and Data Types#

Congratulations! You've successfully hatched your digital pet and given it a name. But wait, what's that? Your little digital friend is giving you a very insistent "chirp-chirp-I'm-hungry" signal! It's time to learn about functions and data types so we can properly care for our new companion.

In the previous section, we wrote all our code directly inside the main function. That works for small programs, but as our pet simulator grows, it'll become much harder to manage. This is where functions come to the rescue! Functions are like mini-programs within our main program. They help us organize our code, making it cleaner, more readable, and easier to reuse.

Let's create a function to feed our pet. This function will need to track how hungry our pet is, so we'll use a new data type: int, which represents whole numbers. We'll use an int variable to store the pet's hunger level. The higher the number, the hungrier the pet is.

First, we need to add a hunger level to our pet. Since we want to keep track of this information with our pet, we'll use a class (we'll explore classes more deeply in a later part). For now, just add this to your code:

class DigitalPet {
  String name;
  int _hunger; //Adding hunger level, we'll learn about the underscore later

  DigitalPet(this.name) : _hunger = 5; //Give your pet an initial hunger level
}

Now let's create our feed() function. We'll make it a void function (meaning it doesn't return any value), and it will update the pet's hunger:

  void feed() {
    print('$name is eating...');
    _hunger = (_hunger - 2).clamp(0, 10); // Hunger decreases by 2, but stays between 0 and 10
    print('$name finished eating! Hunger decreased.');
  }

The .clamp(0, 10) part is crucial. It ensures that the hunger level never goes below 0 or above 10, keeping things realistic for our pet. We'll use this clamp() method throughout the simulator to make sure our pet's attributes stay within sensible bounds.

Finally, we need to call (use) this function from our main function:

void main() {
  // ... (previous code from Part 1) ...

  DigitalPet myPet = DigitalPet(petName); //Now we create an instance of the class
  myPet.feed(); //And we call our new function

  // ... (rest of your main function code) ...
}

Now run your code again! You'll see a message indicating that your pet is eating, and its hunger level will be reduced. We've successfully used a function to update our pet's status!

Notice how we keep track of the hunger level inside the DigitalPet class using the _hunger variable. The underscore _ before the variable name is a Dart convention indicating that this variable is intended for internal use within the class itself, a concept called encapsulation which makes our code more robust.

In the next part, "Playtime Fun!", we'll add even more interactivity and introduce the exciting concepts of async and await to simulate actions that take some time, like playing with our pet. Get ready for more fun and games!