Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Is this python code a function or a procedure? What is the difference between the two?

I have been given this code (see picture)

I can't tell if it is a procedure or function, could you give a reason why it is one and not the other. Thanks

Attachment image

2 Answers

Relevance
  • 4 years ago
    Favorite Answer

    You have a function, period.

    Python does not use the term "procedure", so on technical grounds that is automatically true.

    The normal distinction between "procedure" and "function" in other languages (or in "language neutral" discussions) is that a function returns a value, while a procedure does not. Your getARandomNumber() function returns a value that can be used in an expression, an assignment statement, etc. That makes it a "function" in the generic sense.

    A procedure doesn't return a value. That's not technically possible in Python, since every function returns some kind of value. If you don't have a return statement, Python returns a special value called None for you. So, if you have a "procedure" like:

    def sayHello():

    .... print("Hello")

    then it is indeed legal to use:

    >>> x = sayHello()

    The function will print "Hello", and return None as a result. The assignment statement will store that None value in x. Try it. Then you can:

    >>> print(x)

    None

    (Those >>> prompts are printed by Idle or by Python in interactive mode.)

    PS: It's usually a bad idea to put an import statement in a function. Don't copy that style unless you have a Very Good Reason to do so.

  • Anonymous
    4 years ago

    Both.

    It's a procedure which returns a value (in this case in the variable 'number'). A procedure that returns a value is a function.

Still have questions? Get your answers by asking now.