Sunday, May 19, 2013

Python Functions Parameters and Returned Values


Python has lots of pre-made functions, that you can use right now, simply by 'calling' them. 'Calling' a function involves you giving a function input, and it will return a value 

Code Example 1 - How to call a function
function_name(parameters)
Code Example 2 - Using a function
a = multiply(70)
The computer would actually see this:
Code Example 3 - What the computer sees
a = 350

Code Example 4 - Using raw_input
# this line makes 'a' equal to whatever you type in
a = raw_input("Type in something, and it will be repeated on screen:")
# this line prints what 'a' is now worth
print a
Code Example 5 - What the computer sees
a = "hello"
print "hello"

Code Example 7 - Python version of menu


#calculator program
 
#this variable tells the loop whether it should loop or not.
# 1 means loop. anything else means don't loop.
 
loop = 1
 
#this variable holds the user's choice in the menu:
 
choice = 0
 
while loop == 1:
    #print what options you have
    print "Welcome to calculator.py"
 
    print "your options are:"
    print " "
    print "1) Addition"
    print "2) Subtraction"
 
    print "3) Multiplication"
 
    print "4) Division"
    print "5) Quit calculator.py"
    print " "
 
    choice = input("Choose your option: ")
    if choice == 1:
        add1 = input("Add this: ")
        add2 = input("to this: ")
        print add1, "+", add2, "=", add1 + add2
    elif choice == 2:
        sub2 = input("Subtract this: ")
        sub1 = input("from this: ")
        print sub1, "-", sub2, "=", sub1 - sub2
    elif choice == 3:
        mul1 = input("Multiply this: ")
        mul2 = input("with this: ")
        print mul1, "*", mul2, "=", mul1 * mul2
    elif choice == 4:
        div1 = input("Divide this: ")
        div2 = input("by this: ")
        print div1, "/", div2, "=", div1 / div2
    elif choice == 5:
        loop = 0
 
print "Thank you for using calculator.py!"

Define Your Own Functions 


Code Example 8 - The def operator
def function_name(parameter_1, parameter_2):
    {this is the code in the function}
    {more code}
    {more code}
    return {value to return to the main program}
{this code isn't in the function}
{because it isn't indented}
#remember to put a colon ":" at the end
#of the line that starts with 'def'

Functions run completely independent of the main program. Remember when I said that when the computer comes to a function, it doesn't see the function, but a value, that the function returns? Here's the quote:
Code Example 9 - return
return "Hello"
Code Example 10 - using return
# Below is the function
def hello():
    print "hello"
    return 1234
 
# And here is the function being used
print hello()
Think about the last line of code above. What did it do? Type in the program (you can skip the comments), and see what it does. The output looks like this:
Code Example 11 - the output
hello
1234
So what happened?
when 'def hello()' was run, a function called 'hello' was created When the line 'print hello()' was run, the function 'hello' was executed (The code inside it was run) The function 'hello' printed "hello" onscreen, then returned the number '1234' back to the main program The main program now sees the line as 'print 1234' and as a result, printed '1234 That accounts for everything that happened. remember, that the main program had NO IDEA that the words "hello" were printed onscreen. All it saw was '1234', and printed that onscreen.

Passing Parameters to Functions 
There is one more thing we will cover in this (monsterously huge) lesson - passing parameters to a function. Think back to how we defined functions:

Code Example 12 - Defining functions with parameters
def function_name(parameter_1,parameter_2):
    {this is the code in the function}
    {more code}
    {more code}
    return {value (e.g. text or number) to return to the main program}
Where parameter_1 and parameter_2 are (between the parentheses), you put the names of variables that you want to put the parameters into. Put as many as you need, just have them seperated by commas. When you run a function, the first value you put inside the parentheses would go into the variable where parameter_1 is. The second one (after the first comma) would go to the variable where parameter_2 is. This goes on for however many parameters there are in the function (from zero, to the sky) For example:
Code Example 13 - how parameters work
def funny_function(first_word, second_word, third_word):
    print "The word created is: " + first_word + second_word + third_word
    return first_word + second_word + third_word
When you run the function above, you would type in something like this: funny_function("meat", "eater", "man"). The first value (that is, "meat") would be put into the variable called first_word. The second value inside the brackets (that is, "eater") would be put into the variable called second_word, and so on. This is how values are passed from the main program to functions - inside the parentheses, after the function name.

A Final Program To design - First we will define all the functions we are going to use with the 'def' operator (still remember what an operator is ;) ). Then we will have the main program, with all that messy code replaced with nice, neat functions. This will make it so much easier to look at again in the future.

Code Example 14 - Calculator program
# calculator program
 
# NO CODE IS REALLY RUN HERE, IT IS ONLY TELLING US WHAT WE WILL DO LATER
# Here we will define our functions
# this prints the main menu, and prompts for a choice
def menu():
    #print what options you have
    print "Welcome to calculator.py"
    print "your options are:"
    print " "
    print "1) Addition"
    print "2) Subtraction"
    print "3) Multiplication"
    print "4) Division"
    print "5) Quit calculator.py"
    print " "
    return input ("Choose your option: ")
 
# this adds two numbers given
def add(a,b):
    print a, "+", b, "=", a + b
 
# this subtracts two numbers given
def sub(a,b):
    print b, "-", a, "=", b - a
 
# this multiplies two numbers given
def mul(a,b):
    print a, "*", b, "=", a * b
 
# this divides two numbers given
def div(a,b):
    print a, "/", b, "=", a / b
 
# NOW THE PROGRAM REALLY STARTS, AS CODE IS RUN
loop = 1
choice = 0
while loop == 1:
    choice = menu()
    if choice == 1:
        add(input("Add this: "),input("to this: "))
    elif choice == 2:
        sub(input("Subtract this: "),input("from this: "))
    elif choice == 3:
        mul(input("Multiply this: "),input("by this: "))
    elif choice == 4:
        div(input("Divide this: "),input("by this: "))
    elif choice == 5:
        loop = 0
 
print "Thank you for using calculator.py!"
 
# NOW THE PROGRAM REALLY FINISHES



No comments:

Post a Comment