The problem: repeated code
Imagine a student who has to write three lines of text on a whiteboard three times. As a Swift program, the straightforward version repeats the same three print calls:
print("This is Andy")
print("Andy made a mistake")
print("Andy suffers the consequences")
print("This is Andy")
print("Andy made a mistake")
print("Andy suffers the consequences")
print("This is Andy")
print("Andy made a mistake")
print("Andy suffers the consequences")
Nine lines, but only three different instructions. This is manageable, until the text becomes ten lines long, or has to be repeated a hundred times. The program would grow with every repetition, and every line would have to be kept identical by hand.
The fix: put the lines in a function
A function lets you write the lines once and run them whenever you want, just by calling the function:
func printMessage() {
print("This is Andy")
print("Andy made a mistake")
print("Andy suffers the consequences")
}
printMessage()
printMessage()
printMessage()
The output is the same as before. The three instructions now exist in exactly one place, and each printMessage() line stands for all three of them.
What functions buy you
- Repetition is cheap. Printing the text five times instead of three means two more calls, not six more lines.
- A change is made once. If the student's name changes to Mike, you edit the three strings inside the function. Every call picks up the new text automatically.
- Names explain intent.
printMessage()says what happens at that point in the program, and the details are in the body for anyone who needs them. - Fewer copy-paste mistakes. When the same lines appear in ten places, it's easy to fix nine of them and forget the tenth.
The calls themselves still repeat. Loops, which come later, remove that repetition too.
The exercise, step by step
The starter code is a mainFunction that already prints Sorry, I didn't hear you once. You add a function that introduces Andy, and call it before and after that line.
- Define
introduceYourselfabovemainFunction. It has empty parentheses, a body in braces and no return type, so it returns nothing. - Print the first line inside it:
print("My name is Andy"). - Print the second line on the next line of the body:
print("I love coding"). - Call it at the start of
mainFunction. The callintroduceYourself()runs bothprintlines. - Call it again after the
"Sorry, I didn't hear you"line. The introduction is printed a second time without a single line of it being repeated.
The solution in Swift
func introduceYourself() {
print("My name is Andy")
print("I love coding")
}
func mainFunction() {
introduceYourself()
print("Sorry, I didn't hear you")
introduceYourself()
}
When mainFunction runs, it prints the following:
My name is Andy
I love coding
Sorry, I didn't hear you
My name is Andy
I love coding
If the introduction ever changes, only the body of introduceYourself needs editing, and both calls pick up the change. The text has to match the lesson exactly, including the words Andy and coding, because the tests compare the printed lines.
Common mistakes
- Defining the function and not calling it. The definition alone prints nothing.
- Calling without parentheses.
introduceYourselfalone doesn't call the function, and Swift reports it as an error. - Copying the
printlines intomainFunctionas well. That repeats the code the function exists to avoid. - Wrong place for the calls. The first call goes before the
Sorry, I didn't hear youline, and the second goes after it. Swapping them changes the output order.
Key takeaways
- A function holds code you want to reuse. You write it once and call it by name.
- Changing the function's body changes every call at the same time.
- Each call runs the entire body, in order, from top to bottom.
