0

I am an amateur Python coder learning in school. We recently went over functions and did some work at home. I am confused on the point/meaning of the return statement. Our goal was to simply turn a letter into its ASCII counter part using ord(). My code works as intended however what is the use of the return statement, and how can it be used in this situation to take this function further?

Letter = input("What is your letter? ")
def ordfunction ( x ):
    x=ord(x) 
    print(x)
    return[x]

ordfunction(Letter)  x
  • `return` means "immediately leave function and **return** to place where this function was executed" and "**return** value" – furas Feb 01 '17 at 04:38
  • with "return" you can do `result = ordfunction(Letter)` or `print(ordfunction(Letter))` because this function returns value. – furas Feb 01 '17 at 04:40
  • BTW: if you don't use `return` that function as default does `return None` – furas Feb 01 '17 at 04:42

5 Answers5

1

Whenever we write a function it perform some task, on basis of execution it provide some result. Now we have lot of options to use this result.

One we can print/display that result on console. for which we use print to show o/p. Here is use:-

Letter = input("What is your letter? ")
def ordfunction ( x ):
    x=ord(x) 
    print(x)

ordfunction(Letter)

So it will display output on console apart from this it dosn't perform any things. We can use this output to store in a file or send on any device.

Further other way is to use return to provide result of function. Here we can hold this value to use further in any kind of calculation like:-

Letter = input("What is your letter? ")
def ordfunction ( x ):
    x=ord(x) 
    return x

a = ordfunction(Letter)
print (a+100)

So return with provide use result of execution to use it further throughout program.

Further you can refer:- Why would you use the return statement in Python?

Community
  • 1
  • 1
Rakesh Kumar
  • 4,319
  • 2
  • 17
  • 30
0

There are basically two things that a method/function can do: change some kind of state or give the caller some kind of information.

For example, if you write something to the console, you've changed something about the system state. This is sometimes called a side effect.

The second thing a method can do is give its caller some kind of information. return is a way to pass data back to the caller - it's the "result" of the method. (In many programming languages, you can also use it to exit a method immediately, but that's a different topic).

Think about a common function: 2 + 3. Most people aren't used to thinking of + as a function, but it is. (If it helps, you can think of this as plus(2, 3)). In this case, the plus function "returns" five - that's the result of the operation, the information you were looking for when you "called" it to begin with.

Hopefully, this clarifies things a little bit - if not please feel free to comment and I can edit.

0

Quoting definition of a math function:

In mathematics, a function is a relation between a set of inputs and a set of permissible outputs with the property that each input is related to exactly one output.

In short, the return is the output. Think about a math function f(x) = x + 100.

  • x is the input
  • x + 100 is the implementation of the function
  • The return is the output of the function, which is the result of x + 100

Equivalent python function:

def f_of_x(x):
  result = x + 100
  return result 
greedy52
  • 1,345
  • 9
  • 8
0

Return is your answer to one statement; example: is it raining? Yes, it's raining Your answer is returned to your question. Simple

-6

I also had trouble understanding the difference between return and print. After you return a function you should:

print(ordfunction(Letter))

To see your function. Print is just made for humans to see what is going on. We usually don't need the print function.

Ziya
  • 1
  • 2