0
0
Swiftprogramming~30 mins

Function declaration syntax in Swift - Mini Project: Build & Apply

Choose your learning style9 modes available
Function declaration syntax
📖 Scenario: Imagine you are creating a simple calculator app that can greet users and perform basic math operations.
🎯 Goal: You will learn how to declare functions in Swift to greet users and add two numbers.
📋 What You'll Learn
Declare a function called greetUser that prints a greeting message.
Declare a function called addNumbers that takes two Int parameters and returns their sum.
Call both functions to see their output.
💡 Why This Matters
🌍 Real World
Functions help organize code into reusable blocks, making apps easier to build and maintain.
💼 Career
Knowing how to declare and use functions is essential for any Swift developer working on iOS or macOS apps.
Progress0 / 4 steps
1
Create a simple function to greet the user
Declare a function called greetUser that prints the message "Hello, welcome to the calculator!".
Swift
Need a hint?

Use the func keyword followed by the function name and parentheses. Inside curly braces, use print to show the message.

2
Add a function to sum two numbers
Declare a function called addNumbers that takes two Int parameters named a and b, and returns their sum as an Int.
Swift
Need a hint?

Remember to specify parameter types and the return type after an arrow ->. Use return to send back the sum.

3
Call the functions to see their output
Call the greetUser() function and then call addNumbers(a: 5, b: 7) storing the result in a variable called result.
Swift
Need a hint?

Call functions by writing their name followed by parentheses. Assign the returned value to a variable using let.

4
Print the result of the addition
Print the value stored in the variable result using print.
Swift
Need a hint?

Use print(result) to show the sum on the screen.