Functions-2

Functions-2

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:
Think back to that calculator program. Did it look a bit messy to you? I think it did, so lets re-write it, with functions.
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
                The initial program had 34 lines of code. The new one actually had 35 lines of code! It is a little longer, but if you look at it the right way, it is actually simpler.
                You defined all your functions at the top. This really isn't part of your main program - they are just lots of little programs, that you will call upon later. You could even re-use these in another program if you needed them, and didn't want to tell the computer how to add and subtract again.
If you look at the main part of the program (between the line 'loop = 1' and 'print "Thank you for..."'), it is only 15 lines of code. That means that if you wanted to write this program differently, you would only have to write 15 or so lines, as opposed to the 34 lines you would normally have to without functions.
Tricky Ways You Can Pass Parameters:
                Finally, as a bit of an interlude, I will explain what the line add(input("Add this: "), input("to this: ")) means.
                I wanted to fit everything onto one line, with as few variables as possible. Remember what functions look like to the main program? Whatever value they return. If the numbers you passed to the add() function were 2 and 30, the main program would see this:
Code Example 15 - The results of fancy parameter work

add(2, 30)

                The add program would then run, adding 2 and 30, then printing the result. The add program has no 'return' operator - it doesn't return anything to the main program. It simply adds two numbers and prints them onscreen, and the main program doesn't see anything of it.
                Instead of (input("Add this: "), input("to this: ")) as the parameters for the add program, you could have variables. For example,

Code Example 16 - variables as parameters

num1 = 45
num2 = 7
add(num1, num2)

                For the above, remember that the function you are passing the variables to cannot change the variables themselves - they are simply used as values. You could even put the values straight into the function:
Code Example 17 - the end result

add(45, 7)


                                This is because the only thing the function sees are the values that are passed on as parameters. Those values are put into the variables that are mentioned when 'add' is defined (the line 'def add(a,b)' ). The function then uses those parameters 

No comments: