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

Raphael Reiter
Raphael Reiter
6,820 Points

Print Vs Return

Hi there,

I'm not sure i understand the difference between print and return

Thanks

3 Answers

João Elvas
João Elvas
5,400 Points

Hello my friend,

This is actually a very simple concept.

Print: prints what you want to the console

Return: Will return the value you want to the place in the code where you want.

Lets pretend you have a function named, myFunc with "return 5"

When you call myFunc() that function will return 5. so if you do "2 + myFunc()" it will be 7

On the other side, if myFunc() prints "5" when you call myFunc() it will print 5 and will not return nothing to the calling place.

Good luck!

This is confusing to many people.

Let's say I have a function that takes in two arguments: num1 and num2. It prints out the sum of the two. And we got another function that does the same thing but it returns the sum of the two.

So if we call the first print that prints, it will just print the value onto the screen. It doesn't actually return anything. If we later call the function and store the return value into a variable like this:

let sum = printSum(5, 5)

The variable sum won't actually have the result of 5+5. Instead, it will have the value of nil (which means nothing). On the other hand, if we call the other function that returns the sum, like this:

let sum: Int = returnSum(5, 5)

The sum variable will actually contain the value of 5+5.

Recap:

Print displays a value onto the screen.

Return actually returns a value that can be later processed. Keep in mind that return doesn't print anything (unless you `print1 the return value).

I hope this helps. ~Alex

Raphael Reiter
Raphael Reiter
6,820 Points

Thanks guys, its more clear now. :-)