PROGRAMMING

  MINTE9
REMEMBERS




Definition

p45 ! A function is a named sequence of statements.
 
# A function is a named sequence of statements.
#
# A function definition specifies the name and the statements.
# Expression in parantheses is called the argument
#
# To end the function, you have to enter an empty line.
# Statements inside a function don't run until the function is called
#
# In Python, there is value called None ...
# which represents the absence of a value
#
# You can split a singule instruction on multple lines with \


def myfunc(a): 
    return a%2

print (myfunc(3)) # 1


def myprint(x):
    return

print (myprint("2")) # None


print("Hello " + \
        "World") # Hello World

Import

To use a function from a module, you have to import it.
 
# To use a function from a module, you have to import it.
# 
# When you save your scripts ...
# take care not to use names used by Python modules.
# such as math, sys, csv, etc

import math

print(math.pi)
    # 3.141592653589793
print(math.sin(math.pi/2))
    # 1.0

Local variable

After the function is called, the local variable is destroyed.
 
# A function local variable is destroyed ...
# after the function is called
#
# If you need to modify a global variable from within a function ...
# use global statement.


def myfunc(a, b):
    c = a + b
    return c

assert myfunc(3, 4) != None
assert myfunc(3, 4) == 7

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


n = 0
def parse():
    global n
    for i in range(10):
        n = i

parse()

assert n != 0
assert n == 9

Questions     github Language
Conditional
        A B C D E F
🔔
1/4