Swift: Calling a Function

Function Call in Swift:

Let’s learn how to call a function in Swift to execute the code within its body now that we’ve practiced defining it.

Calling a function is the process of executing the code contained within its body. In Swift, we type the function’s name followed by parentheses to call it ().

For example, the boilEgg() function, which contains step-by-step instructions for boiling an egg:

func boilEgg() {
 print("Bring Water to Hard Boil");
 print("Next, Boil Eggs");
 print("Submerge the Eggs in Water");
 print("Peel and Enjoy!");
 print("Serve"); 
} 

We must type the function’s name followed by a pair of parenthesis to call our function:

boilEgg();

The print() statements in the body will be executed when the function is called, resulting in the following output:

Bring Water to Hard Boil
Next, Boil Eggs
Submerge the Eggs in Water
Peel and Enjoy!
Serve

Helpful Tools:

Here are some useful tools to help you along your journey!

Setting up an IDE (Integrated Development Environment) can be difficult for beginners. The Online Compiler will enable you to run your code inside your browser without the need to install an IDE. If you need a more detailed explanation of a specific topic, the best place to find answers is in the Official Documentation.

Scroll to Top
%d bloggers like this: