A function is a reusable portion of a program, sometimes called a procedure or subroutine.
f(x) = 2x + 5In mathematics, the symbol 'x' is a placeholder, and when you run the function for a value, you "plug in" the value in place of x. Consider the following equation, which we then simplify:
y = f(10) // must evaluate f(10) y = 2 * 10 + 5 // plug in 10 for x y = 20 + 5 y = 25 // so f(10) gives the answer 25In programming, we would say that the call f(10) returns the value 25.
functionName (argumentList)
where the argumentList is a comma-separated list of arguments (data
being sent into the function). Use the call anywhere that the
returned answer would make sense.
def functionName (parameters): # known as the function header
statements(s)
...
def functionName (parameters): # known as the function header statements(s) ... return expression
# This program demonstrates a function.
# First, we define a function named message.
def message():
print 'I am Arthur'
print 'King of the Britons'
# Call the message function.
message()
# This program has two functions. First we # define the main function. def main(): print 'I have a message for you.' message() print 'Goodbye!' # Next we define the message function. def message(): print 'I am Arthur' print 'King of the Britons.' # Call the main function. main()
# Definition of the main function. def main(): get_name() print 'Hello', name # This causes an error! # Definition of the get_name function. def get_name(): name = input('Enter your name: ') # Call the main function. main()
# Create a global variable. my_value = 10 # The show_value function prints # the value of the global variable. def show_value(): print my_value # Call the show_value function. show_value()Code: global1.py
# Create a global variable. number = 0 def main(): global number number = int(input('Enter a number: ')) show_number() def show_number(): print('The number you entered is', number) # Call the main function. main()Output:
Enter a number: 13 The number you entered is 13Code: global2.py
MY_CONSTANT = 0.0 def main(): print MY_CONSTANT # Prints the value of the global constant. MY_CONSTANT = 9.0 # This is a new variable with scope in the main function only.
def main(): name = "My Name" print "Your name is: ",name pass_name(name) def pass_name(n): print "The name passed is: ",n main()Output:
Your name is: My Name The name passed is: My Name
# This program demonstrates what happens when you # change the value of a parameter. def main(): value = 99 print 'The value is', value change_me(value) print 'Back in main the value is', value def change_me(arg): print 'I am changing the value.' arg = 0 print 'Now the value is', arg # Call the main function. main()Output:
The value is 99 I am changing the value. Now the value is 0 Back in main the value is 99Code: change_me.py
def main(): var1 = 0 var2 = 1 var3 = 2 # parameters used in the order they are passed my_function(var1, var2, var3) # Keyword arguments specifies which parameter the # arguments should be passed into as. # Its position in the function call does not matter. my_function(param3=var3, param1=var1, param2=var2) def my_function(param1, param2, param3): print "Params: ", param1, param2, param3 main()Output:
Params: 0 1 2 Params: 0 1 2
def sum(num1, num2): result = num1 + num2 return resultAlternatively:
def sum(num1, num2): return = num1 + num2
def main(): print "Lets calculate the average of two numbers." ave = (get_value() + get_value()) / 2.0 print "Average: ", ave def get_value(): return input("Enter a number: ") main()Output:
Lets calculate the average of two numbers. Enter a number: 10 Enter a number: 58 Average: 34.0Code: value_return.py
def get_name(): #Get the users first and last name. first = raw_input("Enter your first name: ") last = raw_input("Enter your first name: ") # Return both names. return first, last def main(): # Values are assigned in the order they are returned. # The number of variables has to match the number of # return values. firstName, lastName = get_name() print firstName, lastName main()Output:
Enter your first name: First Enter your first name: Last First LastCode: multi_return.py