Wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Exploring Functions in Swift

Total questions: 15

Worksheet time: 8mins

Name
Class
Date
1.

Hey there, coding wizards! 🌟 Faza, Dian, and Laila are on a quest to master Swift programming. Can you help them out? What is the magical incantation for defining a function in Swift?

a)

define functionName(parameters) {}

b)

function functionName(parameters) {}

c)

func functionName(parameters) -> ReturnType {}

d)

functionName(parameters) -> ReturnType;

2.

Hey there, coding wizards! Icha, Nagata, and Suci are having a friendly debate about Swift functions. Can you help them settle the score? How do you specify parameter types in a Swift function?

a)

Parameter types are indicated by 'Type(paramName)' in the function signature.

b)

You define parameter types with 'Type: paramName' in the function header.

c)

You declare parameter types using 'Type = paramName' in the function body.

d)

You specify parameter types by using the syntax 'paramName: Type' in the function definition.

3.

Hey there, coding wizards! 🌟 What do you think is the magical purpose of the return value in a Swift function?

a)

Is it to define the function's parameters like a secret code? 🤔

b)

Or is it to send data back to the caller, like a friendly message from Irfan? 📬

c)

Could it be to handle errors during execution, like a superhero saving the day? 🦸‍♂️

d)

Maybe it's to document the function's purpose, just like Suci writes in her diary? 📖

4.

Hey there, coding wizards! 🌟 Can our friend Irfan tell us if a Swift function can have multiple return values? If so, how can he do that? Let's see what Freja and Suci think!

a)

Yes, by using global variables instead of return values.

b)

Yes, but only if they are using classes.

c)

Yes, a Swift function can have multiple return values using tuples.

d)

No, Swift functions can only return a single value.

5.

Hey there, coding wizards! 🌟 Have you ever wondered what a closure in Swift is and how it can work its magic in functions? Let's dive into this fun world of programming!

a)

A closure in Swift is a predefined method that automatically executes when a program starts.

b)

A closure in Swift is a function that only works with integers and cannot accept parameters.

c)

A closure in Swift is a type of variable that holds a single value and cannot change.

d)

A closure in Swift is a self-contained block of functionality that can capture and store references to variables and constants from its surrounding context, often used as callback handlers in functions.

6.

Hey there, coding wizards! 🌟 How would you explain a closure in Swift to your friend Rakka while you both are working on a fun app project together?

a)

A closure is a self-contained block of functionality that can capture and store references to variables and constants from its surrounding context.

b)

A closure is a method that only works with classes and not structs.

c)

A closure is a function that cannot access any external variables.

d)

A closure is a type of variable that can only hold integer values.

7.

Hey there, coding wizards! Freja, Rahman, and Angga are having a lively debate about Swift programming. They want to know: What is the difference between a closure and a function in Swift?

a)

A closure is always named and does not capture context, while a function can capture context.

b)

A closure is a type of function that cannot capture variables, while a function can.

c)

A closure can capture context and is unnamed, while a function is named and does not capture context.

d)

A closure is a named block of code that captures context, while a function is unnamed and does not.

8.

Hey Faza, have you ever wondered what higher-order functions are in Swift? Let's dive into this fun concept!

a)

Higher-order functions cannot take parameters.

b)

Higher-order functions are only used for sorting arrays.

c)

Higher-order functions are limited to mathematical operations.

d)

Higher-order functions are functions that can accept other functions as parameters or return them.

9.

Hey there, coding wizards! Can you help Faza, Nagata, and Rina by giving an example of a higher-order function in Swift? Let's see who can come up with the best answer!

a)

An example of a higher-order function in Swift is the 'filter' function.

b)

An example of a higher-order function in Swift is the 'map' function.

c)

An example of a higher-order function in Swift is the 'forEach' function.

d)

An example of a higher-order function in Swift is the 'reduce' function.

10.

Hey there, coding wizards! 🌟 How do you think our friend Irfan can pass a function as a parameter to another function in Swift? Let's help him out!

a)

By using a protocol to define the function parameter type.

b)

By defining the parameter as a function type, e.g., (Int) -> Int.

c)

By declaring the parameter as a closure type, e.g., (Int) -> Void.

d)

By passing the function as a string to the other function.

11.

Hey there, coding wizards! 🌟 What do you think is the significance of the 'in' keyword in Swift closures? Let's see if you can help Faza, Rakka, and Dian figure this out!

a)

The 'in' keyword is a marker for closure's access control.

b)

The 'in' keyword is used to separate the parameters and return type from the body of a closure.

c)

The 'in' keyword indicates the start of a closure's parameters.

d)

The 'in' keyword is used to define the return type of a closure.

12.

Hey there, coding wizards! Irfan, Indah, and Agung are on a quest to master Swift closures. Can you help them figure out how to return a closure from a function in Swift?

a)

func generateMultiplier(factor: Int) -> (Int) -> Int { return { num in num / factor } }

b)

func createAdder(value: Int) -> (Int) -> Int { return { num in num - value } }

c)

func makeDecrementer(decrementAmount: Int) -> (Int) -> Int { return { number in number * decrementAmount } }

d)

func makeIncrementer(incrementAmount: Int) -> (Int) -> Int { return { number in number + incrementAmount } }

13.

Hey there, Swift enthusiasts! Have you ever wondered what those trailing closures are all about? Let's dive into this fun question!

Imagine Suci and Nagata are working on a Swift project together. They come across a concept called trailing closures. What are trailing closures in Swift?

a)

Trailing closures are closures that must be placed inside parentheses.

b)

Trailing closures are closures written outside the parentheses of a function call when they are the last argument.

c)

Trailing closures are closures that cannot be used as function arguments.

d)

Trailing closures are used to define functions within a class.

14.

Hey there, coding wizards! Suci, Rizky, and Lina are having a friendly debate about Swift functions. They want to know: How do you use default parameter values in Swift functions?

a)

You use default parameter values in Swift functions by specifying them in the function signature, like this: `func example(param: Type = defaultValue) {}`.

b)

You define default values outside the function declaration.

c)

Default parameters are set using a separate function call.

d)

You cannot use default values in Swift functions at all.

15.

Hey there, coding wizards! 🌟 Have you ever wondered about the magical world of JavaScript? Let's dive into a fun question: What is the difference between 'let' and 'var' when declaring parameters in a function? Imagine our friends Nagata and Freja are debating this topic in class!

a)

The main difference is that 'let' is block-scoped, while 'var' is function-scoped.

b)

Both 'let' and 'var' are block-scoped in functions.

c)

'let' allows redeclaration, while 'var' does not.

d)

'let' is function-scoped, while 'var' is block-scoped.