Define a function.

"""
Output the lyrics to Johnny Rivers's "Secret Agent Man" (1966).
"""

import sys

print("There's a man who leads a life of danger")
print("To everyone he meets he stays a stranger")
print("With every move he makes, another chance he takes")
print("Odds are he won't live to see tomorrow.")
print()

print("\tSecret agent man, secret agent man")
print("\tThey've given you a number and taken away your name.")
print()

print("Beware of pretty faces that you find")
print("A pretty face can hide an evil mind")
print("Ah, be careful what you say, or you'll give yourself away")
print("Odds are you won't live to see tomorrow.")
print()

print("\tSecret agent man, secret agent man")
print("\tThey've given you a number and taken away your name.")
print()

#guitar bridge

print("\tSecret agent man, secret agent man")
print("\tThey've given you a number and taken away your name.")
print()

print("Swingin' on the Riviera one day")
print("And then layin' in the Bombay alley next day")
print("Oh no, you let the wrong word slip while kissing persuasive lips")
print("The odds are you won't live to see tomorrow.")
print()

print("\tSecret agent man, secret agent man")
print("\tThey've given you a number and taken away your name.")
print()

print("\tSecret agent man.")

sys.exit(0)
There's a man who leads a life of danger
To everyone he meets he stays a stranger
With every move he makes, another chance he takes
Odds are he won't live to see tomorrow.

	Secret agent man, secret agent man
	They've given you a number and taken away your name.

Beware of pretty faces that you find
A pretty face can hide an evil mind
Ah, be careful what you say, or you'll give yourself away
Odds are you won't live to see tomorrow.

	Secret agent man, secret agent man
	They've given you a number and taken away your name.

	Secret agent man, secret agent man
	They've given you a number and taken away your name.

Swingin' on the Riviera one day
And then layin' in the Bombay alley next day
Oh no, you let the wrong word slip while kissing persuasive lips
The odds are you won't live to see tomorrow.

	Secret agent man, secret agent man
	They've given you a number and taken away your name.

	Secret agent man.

Consolidate the repetition.

"""
Output the lyrics to Johnny Rivers's "Secret Agent Man" (1966).
"""

import sys

def chorus():                                                                    
    "Print the chorus, including the following empty line." #This is a docstring.
    print("\tSecret agent man, secret agent man")                                
    print("\tThey've given you a number and taken away your name.")              
    print()                                                                      
    return   #Unnecessary: the function returns at this point anyway.            


print("There's a man who leads a life of danger")
print("To everyone he meets he stays a stranger")
print("With every move he makes, another chance he takes")
print("Odds are he won't live to see tomorrow.")
print()

chorus()   #The def must have already been executed.

print("Beware of pretty faces that you find")
print("A pretty face can hide an evil mind")
print("Ah, be careful what you say, or you'll give yourself away")
print("Odds are you won't live to see tomorrow.")
print()

chorus()

#guitar bridge

chorus()

print("Swingin' on the Riviera one day")
print("And then layin' in the Bombay alley next day")
print("Oh no, you let the wrong word slip while kissing persuasive lips")
print("The odds are you won't live to see tomorrow.")
print()

chorus()

print("\tSecret agent man.")

sys.exit(0)

The printed output is the same.

A function that takes an argument

The function verse takes one argument. The function is also to use, but not to change, the (global) variable verses.

"""
Output the lyrics to Johnny Rivers's "Secret Agent Man" (1966).
"""

import sys

verses = ( #a tuple containing 3 strings.  Each string containsn 4 lines.
    None,  #Make the index numbers start at 1.

    """\
There's a man who leads a life of danger
To everyone he meets he stays a stranger
With every move he makes, another chance he takes
Odds are he won't live to see tomorrow.
""",

    """\
Beware of pretty faces that you find
A pretty face can hide an evil mind
Ah, be careful what you say, or you'll give yourself away
Odds are you won't live to see tomorrow.
""",

    """\
Swingin' on the Riviera one day
And then layin' in the Bombay alley next day
Oh no, you let the wrong word slip while kissing persuasive lips
The odds are you won't live to see tomorrow."
"""
)

def verse(i):
    "Print verse number i, including the following empty line."
    print(verses[i])


def chorus():
    "Print the chorus, including the following empty line."
    print("\tSecret agent man, secret agent man")
    print("\tThey've given you a number and taken away your name.")
    print()


verse(1)
chorus()
verse(2)
chorus()

#guitar bridge

chorus()
verse(3)
chorus()
print("\tSecret agent man.")

sys.exit(0)

The printed output is the same.

Other uses of functions

  1. Code that must be packaged as a function:
    1. The code that is executed when the user presses a tkinter.Button must be packaged as a function.
    2. The code that is executed when the user selects a choice from a tkinter.OptionMenu must be packaged as a function.
    3. The code that is passed as the key argument of sort, sorted, min, and max must be packaged as a function.
  2. When we talk about local vs. global variables, we’ll see that a function lets us kill a variable when the variable has finished its useful life.