Swift functions: no parameters
Goal
Learn how to write and call a simple Swift function. Focus on functions with no parameters and no return value. Understand why these small functions make code clearer.
func showWelcome() {
print("Welcome")
}
showWelcome()
What is a function with no parameters?
It is a named block of code that runs the same way every time. Because it takes no input, it relies on internal logic or constants. It is often used for simple actions like printing a title.
func printTitle() {
print("Room calculator")
}
Why developers use functions
Functions reduce repetition by grouping steps into one name. They improve readability because the name describes the action. They make updates safer because changes happen in one place.
func printDivider() {
print("------------")
}
printDivider()
printDivider()
Example of a no-parameter function
This function prints two lines of introduction text. It shows how multiple steps can be wrapped in one name.
func printRoomIntro() {
print("This program calculates room area.")
print("It also calculates room volume.")
}
Anatomy of a function
The keyword func starts the definition. The function name describes the action. Empty parentheses mean there are no parameters. Curly braces contain the body of the function.
func showWelcome() {
print("Welcome to the room calculator")
}
How to write a no-parameter function
Choose a short verb-based name that describes the action. Write the steps inside the braces in the correct order. Keep the function focused on one clear task.
func printSummaryHeader() {
print("Summary")
print("-------")
}
How to use a no-parameter function in your code
Defining a function does not run it. Call it by name with parentheses to execute the steps. Place the call where you need the action to happen.
printSummaryHeader()
// more output here
printSummaryHeader()
How a no-parameter function contributes to flexibility
It lets you reuse the same output or setup in multiple places. Changing the function updates every place it is called. It makes long programs easier to scan and navigate.
func printNewSection() {
print("\n--- New calculation ---")
}
printNewSection()
printNewSection()
How to test that a no-parameter function works properly in your code
Call the function once and check the output. Call it twice to ensure repeated use is consistent. Compare the output to the expected text.
func showWelcome() {
print("Welcome")
}
showWelcome()
showWelcome()
Task A
Create a function called printRoomIntro(). Print two lines that explain the program's purpose. Call the function at the start of your program.
func printRoomIntro() {
print("This program calculates room area and volume.")
print("It also finds the usable space after furniture.")
}
printRoomIntro()
Task B
Create a function called printDivider(). Print a line of dashes. Call it before and after your summary output.
func printDivider() {
print("--------")
}
printDivider()
print("Summary here")
printDivider()
Task C
Create a function called printUnitsNote(). Print a note that measurements are in metres. Call it before you show any calculations.
func printUnitsNote() {
print("All measurements are in metres.")
}
printUnitsNote()
Task D
Create a function called printGoodbye(). Print a short closing message. Call it at the end of the program.
func printGoodbye() {
print("Done. Thanks for using the calculator.")
}
printGoodbye()