Recall & Review
beginner
What is the basic syntax to declare a function in Swift?
Use the
func keyword, followed by the function name, parentheses for parameters, and curly braces for the function body.<br><br>Example:<br>func greet() {<br> print("Hello")<br>}Click to reveal answer
beginner
How do you declare a function in Swift that takes parameters?
Inside the parentheses, list parameters with their names and types separated by colons.<br><br>Example:<br>
func greet(name: String) {<br> print("Hello, \(name)!")<br>}Click to reveal answer
beginner
How do you specify a return type in a Swift function declaration?
Add an arrow
-> followed by the return type after the parameter list.<br><br>Example:<br>func add(a: Int, b: Int) -> Int {<br> return a + b<br>}Click to reveal answer
beginner
What keyword do you use to return a value from a Swift function?
Use the
return keyword followed by the value you want to send back.<br><br>Example:<br>func square(number: Int) -> Int {<br> return number * number<br>}Click to reveal answer
beginner
Can Swift functions have no parameters and no return value? How?
Yes, simply declare the function with empty parentheses and no return type.<br><br>Example:<br>
func sayHello() {<br> print("Hello!")<br>}Click to reveal answer
Which keyword starts a function declaration in Swift?
✗ Incorrect
Swift uses
func to declare functions.How do you declare a function that returns an integer in Swift?
✗ Incorrect
The return type is specified with
-> Type after the parameter list.What symbol separates parameter names and their types in Swift function declarations?
✗ Incorrect
Parameters are declared as
name: Type.Which keyword is used to send a value back from a Swift function?
✗ Incorrect
Use
return to output a value from a function.Can a Swift function have no parameters and no return type?
✗ Incorrect
Functions can have empty parentheses and no return type.
Explain how to declare a function in Swift that takes two integers and returns their sum.
Think about the function name, parameters, return type, and how to return the result.
You got /4 concepts.
Describe the parts of a Swift function declaration and their purpose.
Consider what each part does in the function.
You got /5 concepts.