Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Swift Functions and Optionals Functions Function Return Types

What is the difference between printing a value and returning a value in a function

Functions

1 Answer

Gunhoo Yoon
Gunhoo Yoon
5,027 Points

You cannot re-use printed value but you can re-use returned value. You can think printing as sending data to screen and returning as sending data outside of function within program.

You should test this by yourself since I'm not familiar with swift.

Prepare two functions, one that prints and another that returns. Save both result as separate variable and try to print out those variable or re-use it somehow.

i tried to do this but no clear result , :(

Gunhoo Yoon
Gunhoo Yoon
5,027 Points

Sorry I don't have knowledge in Swift but in general, programming languages agree on what print and return do.

So consider this.

// Pseudocode

// Function that only prints (on behind it actually returns void-like value such as Null, None, Undefined etc...)
function foo () {
    print(1)
}

//Function that returns.
function bar () {
    return 1
}

// These will produce different result but actual result might differ from language to language.
var x = foo() + 5
var y = bar() + 5

print(x)
print(y)