Skip to content

Latest commit

 

History

History
83 lines (60 loc) · 1.06 KB

chapter_03.md

File metadata and controls

83 lines (60 loc) · 1.06 KB

function

define function

👉function example

def hello():
    print ("hello world!")

👉 function with parameter

def hello(name):
    print (f"hello, {name}")

👉 function must with return

def add(a, b):
    print (a+b)

add(3, 5)

👉 return

def add(a, b):
    return (a+b)

c = add(3, 5)

print (c)

👉 return None

def add(a, b):
    print (a+b)
    return()

add(3, 5)

👉 arguments, keyword arguments and print

def add(a, b):
    print (a + b)

add()
print ("cat", "dog", "pig")

print ("cat", "dog", "pig", sep=',')

local and global scope

👉local variables cannot be used in global scope

def spam():
    eggs = 1234

print eggs

👉 local scope cannot use variabales in other local scope

def spam():
    eggs = 1234
    bacon()
    print (eggs)


def bacon():
    ham = 4567
    eggs = 0

spam()