What is a procedure function in python?

Definition¶

A procedure allows us to group a block of code under a name, known as a procedure name. We can call the block of code from anywhere in the program to execute the instructions it contains. We can also pass values to the procedure to change how it works.

Note

Wherever possible you should try to use procedures or functions as they tend to make your code more readable.

Easy example¶

def showMenu[]:
    print['Main Menu']
    print['1. Play game']
    print['2. View high scores']
    print['3. Quit']

showMenu[]

Main Menu
1. Play game
2. View high scores
3. Quit

Note

showMenu[] is an example of a procedure call. We can call the procedure as many times as we wish in the program.

Note

A procedure needs to be defined earlier in the program than when it is called.

Syntax¶

#define a procedure
def procedureName[arg1, arg2, ...]:
    print['put instructions here']

#call the procedure
procedureName[]

Examples¶

Example 2 - Use an argument¶

def storyStart[name]:
    print['Once upon a time, ' + name + ' was imprisoned in a castle.']
    print['They were desperate to escape, but couldn\'t.']

userName = input['What is your name? ']

storyStart[userName]

What is your name? Joe
Once upon a time, Joe was imprisoned in a castle.
They were desperate to escape, but couldn't.

Example 3 - Use two arguments¶

The following program first creates a procedure which takes a name and gender and then correctly creates the start of a story using the correct pronouns, he or she. This procedure is used later when it is called using the information which the user inputs.

def storyStart[name, gender]:
    pronoun = ''
    if gender == 'm':
        pronoun = 'He'
    elif gender == 'f':
        pronoun = 'She'

    print['Once upon a time, ' + name + ' was imprisoned in a castle.']
    print[pronoun + ' was desperate to escape, but couldn\'t.']

userName = input['What is your name? ']
gender = input['Are you male or female [type m or f]? ']

storyStart[userName, gender]

What is your name? Joe
Are you male or female [type m or f]? m
Once upon a time, Joe was imprisoned in a castle.
He was desperate to escape, but couldn't.

Example 4 - Using a list as an argument¶

def displayListAndNumber[theList]:
    for i in range[len[theList]]:
        itemNumber = i + 1    #This adds one to the current loop number as Python lists start at zero, but we want the shopping list to start at one.
        print[str[itemNumber] + '. ' + theList[i]]
    print['---------------------']

shoppingList = ['eggs', 'milk', 'ham', 'fish', 'bread']
shoppingListHardware = ['saw', 'drill', 'wood']

displayListAndNumber[shoppingList]
displayListAndNumber[shoppingListHardware]

1. eggs
2. milk
3. ham
4. fish
5. bread
---------------------
1. saw
2. drill
3. wood
---------------------

Example 5 - Using global variables¶

pi = 3.14

def showAreaOfCircle[radius]:
    area = pi * radius * radius
    print['Area: ' + str[area]]

def updatePi[newPi]:
    global pi
    pi = newPi

showAreaOfCircle[10]

updatePi[3.141]

showAreaOfCircle[10]

Note

We need to use the keyword global before we can change a global variable inside a procedure or function. In general you should avoid using global variables and instead pass values to procedures using arguments.

Key points¶

Hint

When you decompose a program into smaller parts, these will usually end up getting programmed in functions or procedures.

Hint

Procedures and functions are ways of abstracting your program. If you can think of parts of the program that are similar, then it is best to abstract them into their own procedure or function.

Note

Procedures and functions are both types of subroutines in programming. Both use the def keyword in Python.

What is an example of a procedure in Python?

showMenu[] is an example of a procedure call. We can call the procedure as many times as we wish in the program. A procedure needs to be defined earlier in the program than when it is called.

What is the difference between a procedure and a function in Python?

Functions differ from procedures in that functions return values, unlike procedures which do not. However, parameters can be passed to both procedures and functions.

What are procedures called in Python?

To call a procedure in Python , simply use its name [include the brackets]: Suppose you wanted to print out player information at the following points in a game: at the end of a level.

WHAT IS function and procedure in programming?

A function would return the returning value/control to the code or calling function. The procedures perform certain tasks in a particular order on the basis of the given inputs. A procedure, on the other hand, would return the control, but would not return any value to the calling function or the code.

Chủ Đề