PY-Into 6
Functions
A function, What is it?
It is away to create re-usable code; something you might use more than once or away of creating blocks of code that are more easily read.
- As an example:
A Math functionA print statement
From lesson 1 we saw c = a**2 + b**2 its called Pythagorean Theorem but the original form was very limiting. How can we make it more usable?
A function can define something with
- no input and no return
- input and no return
- input and a return
def print_lunchtime():
print(" Really its lunch time?\n")
def math_pythag_therem(data):
print("a good book to read might be " + data)
def math_pythag_therem(a,b):
#
# find the hypotenuse of a right triangle
#
result = a**2 + b**2
c = result**(1/2.0)
return c
If you want to read up on the Theorem go to the Wiki
What ideas can you think of for a print statement? Maybe a template for a repeating form block? Try to come up with a print function.
Excel
Now a deep dive.
Look at this example of excel.
Now that we know about functions, what can be described about the program?
Also can this program be made better?
example: excel with small functions?