UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

DAY 7

Functions, part one


Functions let us wrap up pieces of code so they can be used in lots of places. We can send data into functions to customize how they work, and get back data that tells us the result that was calculated.

Believe it or not, function calls used to be really slow. Steve Johnson, the author of many early coding tools for the Unix operating system, said this:

“Dennis Ritchie (the creator of the C programming language) encouraged modularity by telling all and sundry that function calls were really, really cheap in C. Everybody started writing small functions and modularizing. Years later we found out that function calls were still expensive, and our code was often spending 50% of its time just calling them. Dennis had lied to us! But it was too late; we were all hooked...”

Why would they be “hooked” on function calls? Because they do so much to help simplify our code: rather than copying and pasting the same 10 lines of code in a dozen places, we can instead wrap them up in a function and use that instead. That means less code duplication, but also means if you change that function – perhaps adding more work – then everywhere using it will automatically get the new behavior, and there’s no risk of you forgetting to update one of the places you pasted it into.

Today you have four tutorials to follow, and you’ll learn how to write your own functions, how to accept parameters, and how to return data. Once you’ve completed each video you can go over any optional reading if you need it, then take a short test to help make sure you’ve understood what was taught.

  1. How to reuse code with functions
  2. How to return values from functions
  3. How to return multiple values from functions
  4. How to customize parameter labels

Have some questions about functions?

Hit Send below to start a virtual conversation with me.

Share your progress!

If you use Twitter, the button below will prepare a tweet saying you completed today, along with a celebratory graphic, the URL to this page, and the challenge hashtag. Don't worry – it won't be sent until you confirm on Twitter!

Need help? Tweet me @twostraws!

 

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

100 Days of SwiftUI


The 100 Days of SwiftUI is a free collection of videos, tutorials, tests, and more to help you learn SwiftUI faster. Click here to learn more, or watch the video below.

Back to 100 Days of SwiftUI

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.