UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

About print and return, what makes them different from each other? ..

Forums > Swift

@Allen  

let driving3 = {
    print("I'm driving in my car")
}

func travel(action: () -> Void) {
    print("I'm getting ready to go.")
    action()
    print("I arrived!")
}

travel(action: driving3)

result is: I'm getting ready to go.

I'm driving in my car

I arrived!

I thought: I'm driving in my car

I'm getting ready to go

I arrived!

confused by closure parameter return and print

it looks the same to me as they both output the result

2      

print and return perform two very different functions. print, of course, prints out the given data to the debugging console. return indicates that a func should exit. Sometimes return returns something, like in return 0 or whatever, but other times it exits without passing any data back to the code that called the func.

let driving3 = {
    print("I'm driving in my car")
}

func travel(action: () -> Void) {
    print("I'm getting ready to go.")
    action()
    print("I arrived!")
}

travel(action: driving3)

In this example, the return type is Void, meaning this is one of those functions (actually a closure here, which is basically an anonymous function) that doesn't return anything. So we don't need to worry about return here at all. The type signature () -> Void just means we want a closure that take no parameters and returns no result.

So what happens is that you define a closure (a.k.a. anonymous function) and store it in the constant driving3. This closure matches the type signature () -> Void that we are expecting in the travel(action:) function. Then when you then pass driving3 into the travel(action:) function, it gets assigned to the action parameter. When you append () to action, this has the effect of calling the closure stored inside and whatever code it contains is executed. In this case, it's the statement print("I'm driving in my car"). Essentially, then, the code does this:

func travel(action: () -> Void) {
    print("I'm getting ready to go.")
    print("I'm driving in my car") // <-- because this is what was passed in from driving3 to the action parameter
    print("I arrived!")
}

Make sense?

3      

@Allen  

thanks a lot dude for the reply!! I'll try to understand them emm.. not a native speaker, gradutally been tough as I moving on...

2      

@Allen  

I guess i understand a bit, so the let driving3 is assigned to the func ... that's why the result shows that order, action() calling the name action: , for the value "I'm driving in my car". Thanks again dude it really helps!

2      

@Allen  

took me some times to understand .. but I get it now.

print is simply print to the console, the return value is none.

while return returned none or something and stored that value to the function so that the program could use it later if needed.

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.