Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript introduction to function returns

Pavel

Well-Known Coder
i know all of this is simple, but its where i am coming from

what is a return?

i know the word means "give back"

what is being given back via a return?

from what does a return come from / created?

maybe provide a few, simple, introductory examples with descriptive comments, please?

and anything else i should know

please know i appreciate your time, assistance and patience

this is a tough concept for me, so please keep it simple and clear for now :)

MANY THANKS!
 
I'm not familiar with javascript but, I think the concepts are the same on most scripting languages.
I will give some examples in python on returns
Python:
# Create a function with a variable set with some text and return when the function is called
def myfunc():
    myvar = 'This is a variable' # a variable set with some text
    return myvar # gets returned when the function is called

# In python calling a function with a return value will not print unless you call the print function
myfunc()

# This function call will print
print(myfunc())

# A function with an argument
# This function has a return if the argument is set and a return if it's not set
def myfunc2(arg=None):
    if arg:
        return arg
    return 'No argument was set'

# Again to see the returned value you will need to call print
print(myfunc2())

print(myfunc2('This argument was set '))

# In my opinion return is a way of displaying the data when you need it.
# Generally function are for manipulating data and returning the result.

I hope this helps
 
what is being given back via a return?
The value being returned by a function depends on its return type - this is just any variable type(integer, string or character, floating-point, etc.) For example: int func1 returns an integer value whereas char func2 returns a character value.

Take a look over @menator01 examples, as they clearly show how to use returns. They are a simple concept that you needn't be too concerned about unless you are writing a compiler/interpreter which at that point, you will need to know how the insides of returns work.
 
# Create a function with a variable set with some text and return when the function is called def myfunc(): myvar = 'This is a variable' # a variable set with some text return myvar # gets returned when the function is called
what is calling a function ?

example / definition of calling the function please

please, what does “return to caller” mean?

so my returns come from my variable? myvar = ("my name is Pavel")

when the function is called, return is "my name is Pavel"

"return when the function is called" whats this?


Code:
let x = myFunction(4, 3);   

function myFunction(a, b) {
  return a * b;           
}

WHY return a * b?

can i return a + b?

keep it simple :)

i sincerely appreciate both your time and guidance
 
WHY return a * b?

can i return a + b?
a*b is just an example statement; return statements can return any piece of data(integer, string/character, floating-point, etc.) or they can return a value that has been calculated by an operation(+, -, *, /, %).

please, what does “return to caller” mean?
Let's say we have two functions: main and foo. In main, we call foo and pass two integer arguments to it(a and b). Foo will return a value(let's say, return a+b. Once we have reached that return statement, program execution returns to main and the program continues to run from there until an exit signal has been issued(put simply: program runs until it quits).

w3schools would be a good starting point. Here is a link on functions.
I'd just like to point out that W3Schools has had some issues in the past in regards to accurate, up-to-date information(alongside a few other issues). They're probably better now, but despite that, consider using something like MDN from now on - the information is consistently kept up-to-date, they document the latest web standards, and searching for information is very easy.
 

New Threads

Buy us a coffee!

Back
Top Bottom