1

Just started learning python and arrived at function definition. I have questions about the following code:

def abc():
    b = random.randrange(3)
    return b

I want to return a random number without input; however, it just showed neither error nor anything else.

9
  • 1
    It does return a random number; why do you think it does not? Commented Feb 9, 2017 at 1:06
  • 1
    If you looking for output, you need to print it Commented Feb 9, 2017 at 1:07
  • Thanks. I should print (b)? Commented Feb 9, 2017 at 1:08
  • 1
    either put print(b) in your function, or print(abc()) in your script. Commented Feb 9, 2017 at 1:16
  • 1
    Not sure why you got close votes like that. Sure this is a beginner question, but it has all the relevant code with a clear explanation of both expected and actual behaviors. All in all, a well posed question. Commented Feb 9, 2017 at 1:30

3 Answers 3

2

return is not a function: it is a language operation or program statement.

Since you didn't tell it to print, it does not do anything visible—computers usually do exactly what you tell them to do.

To see the returned value, add code to display it. Add this after the function (below the return indented the same as def:

print ('random value is %d' % (abc()))

abc() is the function call
The % operator formats strings; the string on the left is the format control string and the arguments to the right are a list of values. Since the list has only one argument, the parenthesis around it are not needed, but don't hurt. An example of printing two values is:

print ('item %d is "%s"' % (j, s))
Sign up to request clarification or add additional context in comments.

4 Comments

Good explanation. What's with all the parentheses at the end there?
It's like setting up instructions you haven't needed to use yet . You have to call the function now with 'abc()'
Also, "language operation" should probably be "statement" or "built-in statement"
@MadPhysicist: I was being extra general. I have amended my answer with a fuller explanation for a novice.
1

First work first, we make the function ,and you know how to so !

def abc():
  b = random.randrange(3)
  return b

Secondly we need to know that return returns the output in the location where we called the function.

for example:

def example():
   return "learning"
print (example())     #"learning" is sent to this line because example() is called here!

I hope you are clear about return now! Finally for your seeing output - you can print the output inside the function ,which would be a bad practice!

def abc():
  b = random.randrange(3)
  print (b)
  return b

Also print the output from where it was called (like I said before)!

random_number = abc() # if you want to store the returned result somewhere
print(random_number)

or (works same)

print(abc())  # directly print the result; no storing the result

Comments

0

When the function abc()returns something let's say int value 6. you can understand as whenever you call this function abc(), it gives value 6. Your code is only a function definition, and if you want to see the value, you must call the function first and print to the console by either

print(abc())

Or store the return value to a variable then print

var = abc()
print(var)

1 Comment

write in English please, this is not txt msging

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.