Information about cookies on this page

Our website uses cookies to ensure the functionality of the website, to collect and analyze information about the performance and usage of the website. Privacy Policy

  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in
  
  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in

Structs and Functions

You can pass a whole struct to a function. Instead of three parameters for a monster's name, health and attack, the function gets one monster. Choose the kind of parameter as you learned in the lesson on references:

  • A function that only reads the struct gets a const reference, const Monster& monster. The struct isn't copied, and the function can't change it by mistake.
  • A function that changes the struct gets a reference, Monster& monster.
#include <iostream>
#include <string>

struct Monster {
    std::string name;
    int health;
    int attack;
};

// Prints the monster on one line
void printMonster(const Monster& monster) {
    std::cout << monster.name << ": health " << monster.health << std::endl;
}

// Makes the monster stronger
void levelUp(Monster& monster) {
    monster.health += 10;
    monster.attack += 2;
}

int main() {
    Monster orc = {"Orc", 50, 8};
    printMonster(orc);
    levelUp(orc);
    levelUp(orc);
    printMonster(orc);
    return 0;
}

This prints:

Orc: health 50
Orc: health 70

The struct's definition must be above the functions, because they use the type. That's why a program starts with its types, right below the #include lines, then come the functions, and main is at the bottom.

Without the &, void levelUp(Monster monster) gets a copy of the orc. It makes the copy stronger, the copy disappears when the function ends, and the program prints Orc: health 50 twice, without any error. When a function "does nothing" to your struct, check the & first.

A function can take several structs, and each parameter gets the kind it needs. In the task, the hero buys an item in a shop. The function changes the hero's gold, so the hero is a Hero&. It only reads the item's price, so the item is a const Item&.

Instructions

  1. 1

    In place of the first comment, write a function void printHero(const Hero& hero) that prints the hero on one line, such as Aria: health 100, gold 60. Call it at the end of main (before return 0;). Keep the lines that create Aria and the items as they are: the exercise will also try other values.

  2. 2

    In place of the second comment, write a function void buy(Hero& hero, const Item& item) that takes the item's price from the hero's gold. In main, below the call of printHero, let Aria buy the potion, and then call printHero(aria) again. The program prints Aria: health 100, gold 60 and then Aria: health 100, gold 45.

  3. 3

    Aria can't buy what she can't pay for. Change buy: when the hero has less gold than the item costs, the function changes nothing and prints Not enough gold for Sword., with the item's name. At the end of main, let Aria buy the sword, and print her again. The last two lines are Not enough gold for Sword. and Aria: health 100, gold 45.

Back

7/12

  1. 1. What Is a Struct?
  2. 2. Braces and Default Values
  3. 3. Copies and References
  4. 4. A Vector of Structs
  5. 5. Changing Structs in a Loop
  6. 6. Quiz: Copy or Reference?
  7. 7. Structs and Functions
  8. 8. Returning a Struct
  9. 9. Structs Inside Structs
  10. 10. Enumerations: enum class
  11. 11. Quiz: Rarity Names
  12. 12. Summary
Next

Try it yourself

Do this exercise right here in your browser. No account needed.