VNUHCM
Previous lesson Next lesson
  • Learning content
  • Help
    Do you have any questions while learning?
    Learning instructions Frequently asked questions Email for support
    • Tiếng Việt
    • English
    • Member's information
    • Registered courses
    • Log out
  • Cohota
  • HƯỚNG DẪN HỌC TẬP

  • View detail >>
    You have completed 0% of the course
  • HƯỚNG DẪN SINH VIÊN ĐĂNG NHẬP HỆ THỐNG
    • Hướng dẫn đăng nhập
    • Hướng dẫn vào khóa học
  • Introduction
    • Welcome
  • Unit 1: Values
    • Introduction - Unit 1: Values
    • Get Started With Values
    • Play with Values
    • Playground Basics
    • Naming and Identifiers
    • Simulation
    • Strings
    • Constants and Variables
    • Word Games
    • Build a PhotoFrame App
    • Design for People
  • Episode 1: The TV Club
    • Introduction - Episode 1: The TV Club
    • Searching for Content
    • Sharing Personal Information
    • Ordering Online
    • Reflection: Episode 1
  • Unit 2: Algorithms
    • Introduction - Unit 2: Algorithms
    • Get Started with Algorithms
    • Play with Programs
    • Functions
    • Types
    • Parameters and Results
    • Making Decisions
    • BoogieBot
    • Data Visualization
    • Build a QuestionBot App
    • Design an Experience
  • Episode 2: The Viewing Party
    • Introduction - Episode 2: The Viewing Party
    • Accessing the Show
    • Streaming on the Network
    • Reflection: Episode 2
  • Unit 3: Organizing Data
    • Introduction - Unit 3: Organizing Data
    • Get Started with Organizing Data
    • Play with Complex Data
    • Instances, Methods, and Properties
    • Arrays and Loops
    • Structures
    • Enums and Switch
    • Testing Code
    • Processing Data
    • Pixel Art
    • Password Security
    • Visualization Revisited
    • Build a BouncyBall App
    • Design a Prototype
  • Episode 3: Sharing Photos
    • Introduction - Episode 3: Sharing Photos
    • Capturing Images
    • Posting on Social Media
    • Reflection: Episode 3
  • Unit 4: Building Apps
    • Introduction - Unit 4: Building Apps
    • Get Started with App Development
    • Play with App Components
    • Color Picker
    • ChatBot
    • Rock, Paper, Scissors
    • MemeMaker
    • Build an ElementQuiz App
    • Design for Impact
  • Appendix
    • Episode Technical Concepts
    • Glossary
Course overview
Assessment

Progress
Criteria name Weighting (%) Score Progress (%)
Unit 2: Algorithms

Functions

Unit 2: Algorithms|Play

What you'll build

  • A program that makes a repetitive song about a topic of your choosing.

What you'll learn

  • How to avoid typing the same lines of code when you want to repeat the work they do.
  • How to define and call functions.
  • Why breaking down a program into small, understandable parts is a good idea.

Key vocabulary

  • Abstraction
  • Call a function
  • Decomposition
  • Function

Introduction

So far in this course, the code in your playgrounds has started at the top, run to the bottom and then stopped. If you wanted to do anything more than once, you’d have to type it again.

In this lesson, you’ll learn how to group your code into building blocks called functions. You'll also learn how to use those building blocks to repeat code easily, and how to assemble smaller blocks into larger, more powerful programs.

Keeping code inside a function makes it easier to tweak the way your program works. You’ll learn that it’s easier to understand and work with a large program made of smaller functions than one made up of many many lines of code.

Go Build

Open the Functions.playground file in your course resources and follow the instructions.

Screenshot of the Functions playground

Reflection Questions

Think of a collection of everyday tasks that you perform, like getting ready in the morning.

How would you separate the activities into functions?

Can you reuse any of the functions for getting ready for bed at night?

The functions you've learned about so far are very specific. They do the same exact thing every time they’re called.

If you wanted to create functions to sing Happy Birthday to your friends, how many Happy Birthday functions would you have to write?

You’ve learned that functions group smaller tasks together.

How many small tasks do you think should fit inside one function before you rewrite it as two functions?

If you have only one or two small tasks in a function, is that too small?

How do you decide?

Summary

Functions are one of the fundamental building blocks of computer programs.

They allow your mind to focus on different levels of detail. When writing a function, you can concentrate on exactly what that single function does and how it does it. This is a fundamental computer science concept known as procedural abstraction. By breaking a problem into pieces, you can write functions to solve each piece independently. Once you know all the functions work, you can combine them to solve the bigger problem.

Later, when you write code that uses your function, you can put the how details to one side, and just remember what the function does. So you only need to concentrate on one level of detail at a time.

In the next lesson, you’ll learn more about how Swift distinguishes between different kinds of data.

Report error