0
0
Swiftprogramming~5 mins

Function declaration syntax in Swift - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
Adef
Bfunction
Cfunc
Dfun
How do you declare a function that returns an integer in Swift?
Afunc example() -> Int {}
Bfunc example(): Int {}
Cfunc example() returns Int {}
Dfunc example() Int {}
What symbol separates parameter names and their types in Swift function declarations?
A,
B=
C->
D:
Which keyword is used to send a value back from a Swift function?
Areturn
Byield
Csend
Doutput
Can a Swift function have no parameters and no return type?
AYes, but only if marked with special keyword
BYes, by using empty parentheses and no return arrow
CNo, functions must return a value
DNo, functions must have parameters
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.