• 4 hours
  • Easy

Free online content available in this course.

course.header.alt.is_video

course.header.alt.is_certifying

Got it!

Last updated on 6/15/21

Understand how functions work

While coding is great fun, nobody wants to write code that already exists! Suppose you need to know how much money you spent on March 1st. You could write the computer code to determine this amount and move on. Fast forward a few months, and now you need to know how much money you spent on June 15th. You could write the computer code to determine that amount; but should you? It would essentially be a duplication of code and effort, right? The programming logic would be identical - except for the specific date being looked up.

Instead, imagine you could take the group of instructions, then reuse them again and again. Well, this is what functions do! A function allows computer code (your instructions) to be packaged, named, and reused wherever you need it. It wraps code and logic up altogether and assigns it a name. Then, all you have to do is call the function’s name, and ta-da! ✨It does your bidding.

What can functions do?

Functions can be designed and programmed to do almost anything! However, they should be narrowly focused on a specific task. For example, they could be any one of these:

  • A print function, so that you can print your documents.

  • A route mapping function, so that you can get from your place to the closest movie theater.

  • A program that plays samba every time someone calls you (yeah, that’s a ringtone. 📱 Functions are everywhere!)

Functions can even be something silly, like making a banana make a sound every time you touch it. This could be very useful if you have a bunch of bananas and want to play them like a piano.
🍌🍌🍌

Naming functions

As you saw in the video, functions have names.  That's part of what's so great about them: all you have do is call a function's name, and it'll be there. It's like a friend who appears right when you need them.

Since you call a function by its name, this name should be closely linked with what it does.  For example, if you had a function that automatically looked up contact information for friends, you could name it "contact info," but that doesn't really tell you what it's doing.  It could be deleting contact information, publishing it, sending out letters - it all depends on how you interpret it.  Instead, if you name it "Look up my friends' contact information" you'd be much more aligned with what it actually does.  

Try it out for yourself!

Make a list of repetitive tasks that you have done at work or school this week.  Which task do you think you will have to do again in the coming weeks? Which tasks do you think are worth turning into a function?

Keep this list!  When you learn your first programming language, you may be able to write some code for these and save yourself some time! 🤓

Summary

  • Coding is fun but you should not duplicate time and effort: use functions for repetitive tasks.

  • Functions contain a series of instructions you can use again and again.

  • Functions have names.

  • When you need a function, you can call it by name.  

Example of certificate of achievement
Example of certificate of achievement