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

Parameters and Results

Unit 2: Algorithms|Play

What you'll build

  • A function that makes a different sentence based on the values you pass in to it.

What you'll learn

  • How to make more powerful functions by passing in values.
  • How to make functions that return values.
  • How to name functions and arguments to make your programs readable.

Key vocabulary

  • Argument
  • Argument label
  • Parameter
  • Return
  • Side effect

Introduction

The functions you’ve used so far can be thought of as opaque boxes with a big “Go” button on the outside. You hit the button and the function does its work. The work is the same every time. If you want the function to do different work, you have to write a different function.

In this lesson, you’ll learn that functions can be much more powerful. By passing values in to functions, you can make them more flexible so they can be used in a variety of situations. And by calling functions that return values, you can use the results of one function as the input to another. You’ll also learn about ways to make your programs easier to understand and easier to read, even after you’ve added more complexity.

Go Build

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

Screenshot of the ParametersAndResults playground

Reflection Questions

You can now build functions that take information in and use it in the work they perform. But the work is still the same.

What if you could do different work that depends on the information that’s passed in?

Can you think of some real-life processes and tasks that fit into the various ways you can define a function (with or without parameters, with or without a return type)?

Summary

You can now think of a function as more than a simple opaque box with a single button. A function is more like a machine in a factory, with arguments going in at one end, work happening inside, and a return value coming out the other end.

With this kind of complexity, functions are capable of doing some powerful work.

When it comes to writing programs that are easy to read and understand, you’ve learned that naming your functions and their arguments is very important. You've also learned that function calls are another type of expression—they can be used anywhere your code expects a value.

If you've been keeping track, you can list all the kinds of expressions you've learned about:

  • literals
  • constants and variables
  • mathematical expressions
  • function calls

In the next lesson, you’ll learn how your code can make decisions as your program runs. You can use this decision-making ability to write functions that do different work depending on the parameters passed in.

Report error