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:

def GetARandomNumber():7

import random

number = random.randint(1,100)

return number

target = GetARandomNumber()

guess = int(input("Guess the random number: "))

while guess != target:

if guess > target:

guess = int(input("Too high, guess lower! "))

else:

guess = int(input("Too low,guess higher! "))

print ("Well Done!")

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

1 Answer

Relevance
  • Chris
    Lv 7
    4 years ago
    Favorite Answer

    Procedure is an old-fashioned term for function. Basic had procedures. Languages nowadays have functions.

    Python uses "def" to define functions.

    There's also the so-called "procedural style" of programming, which is usually contrasted with object-oriented programming.

    The code you posted uses the procedural style, where a program mostly runs from top to bottom, but the execution occasionally jumps elsewhere, then back.

    Your program's execution starts with the line that sets "target", and since the line calls the GetARandomNumber function, execution jumps to the top, inside the function, then back.

Still have questions? Get your answers by asking now.