Day 5: Functions Part 1
Lesson 4: Input, Return
In the last lesson we saw that functions can receive an input argument, well they can also return a value as well.
In this playground you'll see "Hello Cool Coder" logged twice. The first time from within the sayHello
function and the second time in the main part of the code. This is where we assign the return from the function to a const called message
and then print that out.
Let's break down what's going on here:
The function receives the input
name
and createshelloMessage
by concatenating"Hello "
withname
.The function then prints that message out
On the final line of the function
helloMessage
is returned. This means thathelloMessage
will now be accessible in the main body of codeWe can get that returned value from
sayHello(usersName)
and store it in a const, in this casemessage
, and then use it further on in our code
Here's another, slightly more realistic example:
What about if we want more than one input?