PROGRAMMING

m9/ PYTHON
REMEMBERS




Functions

p45 A function is a named sequence of statements. The expression in parantheses is called the argument. To use a function from a module, you have to import it.
 
>>> int('31')
31
>>> int(3.99999)
3
 
>>> import math

>>> math.pi
3.141592653589793

>>> math.sin(math.pi/2)
1.0

Function Definiton

p49 ! A function definition specifies the name and the statements. To end the function, you have to enter an empty line. After the function is called, the local variable is destroyed.
 
# /var/www/python/test.py

def myfunc(a):
    return a%2

# statements inside a function don't run ...
# until the function is called

print(myfunc(3)) # 1


# python test.py
... 1 lines
 
 
def myfunc(a, b):
    c = a + b
    return c

print(myfunc(3, 4)) # 7

print(c) # NameError: name 'c' is not defined

Questions    
Turtle

        A B C D E F
🔔
1/2