FAQ: Functions - Return

This community-built FAQ covers the “Return” exercise from the lesson “Functions”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

Introduction To JavaScript

FAQs on the exercise Return

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

4 posts were split to a new topic: Why is return necessary?

12 posts were split to a new topic: Why do I need to use return?

3 posts were split to a new topic: Am I missing something with this code?

A post was split to a new topic: Why doesn’t this run in google console?

3 posts were split to a new topic: What is the point of this function?

I dont really understand why we need to use return in this case, but not in any of the previous examples

Is it because something is being calculated, and we have to retrieve the answer?

As in, expect some result to echo back from a function call that can be useful in future computations.

Hello, I can not seem to understand the usage of the return keyword inside of a function.
Here is an example that I do not understand the console output. I would really appreciate if someone could explain these lines of code step by step.

function callMe(name) {
	return name;
}
let name = callMe("Marios");
console.log(name);

// SEPERATE LINE

function buy(item) {
	return item;
}
buy("Onions");

In maths we first learn about the identity function…

f(x) = x

In code, we would write this much like your examples…

function f(x) {
    return x;
}

Not a lot happening here, but what happens if we wish to transform x?

f(x) = 2x + x

y = f(x)

y = 2x + x
function f(x) {
    return 2 * x + x;
}
y = f(5)
console.log(y)
// 15

return is the path back to the caller, meaning we can pass a value back. In the above the return value is assigned to y.

If our function does not do anything, then a call to the function will result in nothing. Your second example calls the function but doesn’t use or assign the return value, so nothing happens that we can tell.

In maths we are focused on Number, whereas in computing we are focused on Data. A function can process any type of data, numeric, string, data structures, etc.

function doubleLastAndAppend(list) {
    list.push(list.slice(-1) * 2);
    return list;
}

a = doubleLastAndAppend([1])
<- [1, 2]
a = doubleLastAndAppend(a)
<- [1, 2, 4]

Notice that a becomes the return value. We started by passing in a simple array, found the last element, doubled its value and appended it to the array. The return value is then assigned back onto a.

You should be familiar with arrays, at this point, but may not have used push or slice methods. They should come up, if they haven’t already, but it can’t hurt to look these two methods up on MDN.

Array.prototype.push()

Array.prototype.slice()

Until this point it was pretty understandable, but this return…, so confusing :frowning:

Hello, @tuzso.

I would suggest that you experiment on your own with what return does. Just play around with it until things become more clear. Start with simple examples, and steadily increase the complexity as you go. Make sure you always understand what is happening before moving on. Feel free to ask questions along the way. Here is some sample code you may feel free to start with if you like:

function doSomething(param) {
  console.log(`I don't feel like doing something with or without your ${param}.`) //this will print every time the function is called
}  

doSomething() // function is called
console.log(doSomething()) // the function is called, and the implicit return value, 'undefined' is also printed

doSomething('pizza') // function is called, and the message is printed
console.log(doSomething('blinker fluid')) // the function is called, and the implicit return value, 'undefined' is also printed

function doSomethingElse(param1, param2) {
  return param1 == param2 //will return 'true' if they are equal, otherwise, 'false'
}

const myBool = doSomethingElse('pizza', 'hamburger') //calls function and assigns return value to myBool
console.log(myBool) //prints the value assigned to myBool, 'false'

console.log(doSomethingElse(11, 'eleven')) //calls funtion, and the return value is printed then lost since not assigned to any variable