Unit 6: Functions Flashcards

1
Q

Abstraction

A

groups things together

an amalgamation

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Calling a function

A

type the function’s name so that the programme will run the steps contained in the function’s implementation; e.g., greet (name: “Leslie”, from: “New York”) = “call the greet function with Leslie and New York” | to call a function on = to call a method that’s associated with a particular instance (e.g., trackTimer.reset() = “call the reset method on the track timer” )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Caller

A

code that uses a function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Decomposition

A

breaks down a single long list into multiple smaller lists, giving each small list its own name; a long function could be decomposed into smaller functions and calling those from the original function instead

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Function

A

combines detailed steps into a single block of code that can be used repetitively; some functions have inputs and change their behaviour based on arguments from their caller, some have outputs and give a result, and some functions have neither

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Calling a Function

A

You probably didn’t realize it at the time, but you’ve already taken advantage of abstraction and functions when you used print() to print “Hello, world!” to the console. In Swift, print() is a function. When you use it, you are calling the function:

print("Hello, world!")
print(360)
/*:
Just as you perform many activities when you get dressed, many things happen when you call the `print()` function, including:
- Turning whatever you hand in to it, including numbers, into a string.
- Adding a newline character, so each call to `print()` ends up on a new line.
- Making that string show up in the console.

In this case, you’re calling a function that someone else has already created. You don’t need to know every detail about how print() works in order to call it.

This is a large part of what makes functions so powerful. They provide a way to combine detailed steps into a definition that can be used again and again.

For the rest of this playground, you’ll practice calling functions and learn how to define functions of your own.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Repeating Yourself

A

You’ve looked at a lot of playgrounds by now. On each page the code runs from top to bottom, and you see the outcome in the results sidebar.

You’ve also learned how to print to the console. The following code is going to print a nursery rhyme. Open the console to read it.

print("Row, row, row your boat")
print("Gently down the stream")
print("Merrily, merrily, merrily, merrily")
print("Life is but a dream")
print("        ~        ")
print("Row, row, row your boat")
print("Gently down the stream")
print("If you see a crocodile")
print("Don't forget to scream")
print("        ~        ")
print("Row, row, row your boat")
print("Gently down the stream")
print("This song is quite repetitive")
print("Can you spot the theme")
/*:
 There are two things that programmers try to avoid:
 - Repeating themselves
 - Repeating themselves

Writing the same code more than once gives you more things to write, to understand and to fix.

How many of the lines of code above are repeated? How can you fix this? Find out on the next page.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

A Single Piece of Work

A

You’ve looked at a lot of playgrounds by now. On each page the code runs from top to bottom, and you see the outcome in the results sidebar.

You’ve also learned how to print to the console. The following code is going to print a nursery rhyme. Open the console to read it.

print("Row, row, row your boat")
print("Gently down the stream")
print("Merrily, merrily, merrily, merrily")
print("Life is but a dream")
print("        ~        ")
print("Row, row, row your boat")
print("Gently down the stream")
print("If you see a crocodile")
print("Don't forget to scream")
print("        ~        ")
print("Row, row, row your boat")
print("Gently down the stream")
print("This song is quite repetitive")
print("Can you spot the theme")
/*:
 There are two things that programmers try to avoid:
 - Repeating themselves
 - Repeating themselves

Writing the same code more than once gives you more things to write, to understand and to fix.

How many of the lines of code above are repeated? How can you fix this? Find out on the next page.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Breaking it Down

A

Each verse of the nursery rhyme follows a pattern:

The first two lines are the same each verse.
The next two lines change each verse.
You take a breath between verses.

When programmers are faced with a long list of tasks, like printing the original nursery rhyme, they often break the single long list down into multiple smaller lists. This is called decomposition. When you’re choosing how to break apart your list, it’s helpful to find the smallest pieces of work that are still meaningful. For example, to split a single verse into multiple meaningful functions, you could do this:

func rowTheBoat() {
    print("Row, row, row your boat")
    print("Gently down the stream")
}
func merrilyDream() {
    print("Merrily, merrily, merrily, merrily")
    print("Life is but a dream")
}
func breatheBetweenVerses() {
    print("        ~        ")
}
//: Now you can use these functions to print the first verse and the start of the second verse like this:
rowTheBoat()
merrilyDream()
breatheBetweenVerses()
rowTheBoat()
//: - callout(Exercise): Write a function called `crocodileScream()` for the second two lines of the second verse, and call it to complete the song in the console.
func crocodileScream() {
    print("if you see a crocodile")
    print("don't forget to scream")
}

crocodileScream()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Functions within Functions

A

When you declare a function, you’re grouping lines of code and giving a name to the group.

You can then call that function in a single line, which can itself become just another line in another group. In other words, you can write a function that calls other functions.

Here’s another way to write the first verse of the nursery rhyme:

func rowTheBoat() {
    print("Row, row, row your boat")
    print("Gently down the stream")
}
func merrilyDream() {
    print("Merrily, merrily, merrily, merrily")
    print("Life is but a dream")
}

func verseOne() {
rowTheBoat()
merrilyDream()
}

verseOne()

/*: 
  - callout(Exercise): Write a function for an alternate second verse of the song using the laughing submarine function below.
*/
func laughingSubmarine() {
    print("Ha! Ha! Fooled you all")
    print("I’m a submarine")
}

// Write the verse two function below

func SecondVerse() {
    laughingSubmarine()
}

laughingSubmarine()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Infinite Loops

A

Programmers sometimes make the mistake of calling a function from itself. When the function is called, it calls itself, which calls itself, which calls itself…

A classic example from real life is found on shampoo bottles: “Lather, rinse and repeat”. If those instructions were code, a computer would interpret them by lathering, rinsing, lathering and rinsing again, and so on. You’d never leave the shower!

This is called an infinite loop. It’s not really infinite, because in most cases it will cause the program to run out of memory and eventually crash (or the bottle to run out of shampoo).

func rowTheBoat() {
    print("Row, row, row your boat")
    print("Gently down the stream")
}
func merrilyDream() {
    print("Merrily, merrily, merrily, merrily")
    print("Life is but a dream")
}

func verseOne() {
rowTheBoat()
merrilyDream()
}

verseOne()

/*:
 - experiment: Make an infinite loop in the code above by editing the `verseOne` function, so it calls `verseOne()` after it calls `merrilyDream()`. Look at the console and the results sidebar. Remove the line to stop the loop. It might take a while until the playground recovers – infinite loops are hard work.

Next, understand how functions make working on longer programs easier to understand.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Hiding Complexity

A

might seem that printing the nursery rhyme using functions is both longer and more complicated than the original code on the first page of this playground. If you attempt to think about every single function at the same time, then it is. But the point of using functions is to break things down into understandable, reusable parts. Each part does a very clear piece of work.

When working on an app, you’re never looking at every single line of code. You’ll call a function knowing what it does, but not necessarily how it does it. For example, in this page all of the song creation functions have been built into the page already, so you can write the song very simply:

verseOne()
breatheBetweenVerses()
verseTwo()
breatheBetweenVerses()
verseThree()
/*: 
 - experiment: Move the lines around to sing the verses in a different order or copy lines to repeat a verse.

On the next page, learn about another benefit of functions - making your programs easier to change.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Change Something Once

A

Containing work in a function can make it easier when things change. You only need to change the code in once place, and you’ll know where to do it because you understand how the function works and what it does.

In this page, the functions merrilyDream(), crocodileScream(), repetitiveTheme() and breatheBetweenVerses() have already been defined.

The other functions are declared below:

func rowTheBoat() {
    print("Row, row, row your boat")
    print("Gently down the stream")
}

func verseOne() {
rowTheBoat()
merrilyDream()
}

func verseTwo() {
rowTheBoat()
crocodileScream()
}

func verseThree() {
rowTheBoat()
repetitiveTheme()
}

verseOne()
breatheBetweenVerses()
verseTwo()
breatheBetweenVerses()
verseThree()
/*:
- callout(Exercise): It’s been decided that the rhyme shouldn’t be about boats any more.\
Update the print statements in rowTheBoat() so the song follows the same pattern but is about something else.\
The pattern is:\
verb, verb, verb “your” noun\
la la la la rhyme\
For example, you could use “Ride, ride, ride your bike”, “With your cycling team”\
\
You only have to update two lines of code, but the changes will be in effect everywhere that function is called.

Next, review what you’ve learned.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Wrapup

A

You’ve now seen how functions can be used to group code together so that a complicated task can be performed with a single line of code, even if that line of code relies on many lines written somewhere else.

You’ve also seen that using a function instead of writing out code over again can make changes easier. If you decide to change how a program works, you may only need to change it in one place.

Finally, you‘ve seen how functions are a powerful example of abstraction.

Practice what you’ve learned with the exercises on the following pages.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly