Calling Functions

This is part of “An intro to Coding” series of posts, with content from the Enki app. If you stumbled upon this, you can start from the beginning.

The robot instructions are almost done! We're getting hungry.

Let's do the part 3 of our recipe¹ now, the grilling of the sandwich:

💡 We can give input values to functions directly, they don't have to be variables. If you notice above, we passed in the duration string "3min" directly into the fry_the_sandwich_on_each_side function.

The recipe¹ instructions are complete.

Now it's time to run the functions we create as commands for the robot.

The final part of the code we're giving the robot is:

💡 The order in which we call the functions matters! You wouldn't want it to prepare the cheese after it fried the sandwich.

Now for the last part.

Let's tell the robot to run the make_a_grill_cheese_sandwich function:

That's it! Bon Appétit!

💡 You should give your functions explanatory names. A good rule is to use verb-noun combinations (prepare_ingredients). If your functions require two verbs or nouns for its identifier, it's probably doing too much and you should split it into separate functions.



Footnotes

[1] The recipe says:

Ingredients:

  • cheese
  • bread
  • butter
  • plate
  • bowl

Instructions:

1. prepare the ingredients
 - Grate the cheese in a bowl
 - Cut the bread into slices

2. build the sandwich
 - Butter the bread on both sides
 - Add the cheese to the sandwich

3. grill the sandwich
 - Preheat a pan
 - Fry the sandwich for three minutes on each side (or until golden brown)

Chat with us to learn more

Continue reading

Our newsletter

Get the best curated articles

No spam!