NEW: Subscribe to Hacking with Swift+ and accelerate your learning! >>

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

   

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?

1      

@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...

   

@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!

   

@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.

   

Hacking with Swift is sponsored by Paw

SPONSORED Use Paw to build, test and describe web APIs. Paw has a lightning fast native macOS interface to compose requests, collaborate in real-time on API specs, and generate client code for your applications. You can import and export API definitions.

Discover Paw for Mac

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

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

Not logged in

Log in
 

Link copied to your pasteboard.