DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on • Edited on

Function in Python

Buy Me a Coffee

*Memos:

A function:

  • can do a specific task.
  • can reuse and reduce code.
  • must have a def, identifier and ():.
  • can have zero or more parameters.
  • can have one or more return statements to return a value.
  • can have one or more pass statements, return statements and values.
  • must have code.
  • can be indirectly assigned to a variable but not directly.

Code is more if not using a function as shown below:

num1 = 3
num2 = 5

print(num1+num2) # 8

num1 = 7
num2 = 2

print(num1+num2) # 9

num1 = 1
num2 = 4

print(num1+num2) # 5
Enter fullscreen mode Exit fullscreen mode

Code is less if using a function as shown below:

def func(num1, num2):
    return num1+num2

print(func(3, 5)) # 8
print(func(7, 2)) # 9
print(func(1, 4)) # 5
Enter fullscreen mode Exit fullscreen mode

Zero or more parameters can be set as shown below:

def func():
    print("No param")

func() # No param

def func(p1):
    print(p1)

func("p1") # p1

def func(p1, p2):
    print(p1, p2)

func("p1", "p2") # p1 p2

def func(p1, p2, p3):
    print(p1, p2, p3)

func("p1", "p2", "p3") # p1 p2 p3
Enter fullscreen mode Exit fullscreen mode

One or more return statements can be used as shown below:

def func():
    return "Hello"

print(func()) # Hello

def func():
    return [0, 1, 2]

print(func()) # [0, 1, 2]

def func():
    return "Hello"
    return [0, 1, 2]

print(func()) # Hello

def func():
    return [0, 1, 2]
    return "Hello"

print(func()) # [0, 1, 2]

def func():
    return

print(func()) # None
Enter fullscreen mode Exit fullscreen mode

You can use one or more pass statements, return statements or values to do nothing in a function, returning None as shown below:

*Memos:

  • Conventionally, a pass statement is used for the function to do nothing.
  • The function with no code gets error.
def func():
    pass

print(func()) # None

def func():
    pass
    pass

print(func()) # None

def func():
    return

print(func()) # None

def func():
    return
    return

print(func()) # None

def func():
    "Hello"

print(func()) # None

def func():
    "Hello"
    "World"

print(func()) # None

def func():
    100

print(func()) # None

def func():
    100
    200

print(func()) # None

def func(): # SyntaxError: incomplete input
    # No code
Enter fullscreen mode Exit fullscreen mode

You can pass a class and function to a function as shown below:

class mycls:
    v = "Hello"

def myfn():
    return "World"

def func(cls, fn):
    print(cls.v, fn())

func(mycls, myfn) # Hello World
Enter fullscreen mode Exit fullscreen mode

You can define a class and function in a function as shown below:

def func():
    class mycls:
        v = "Hello"
    def myfn():
        return "World"
    print(mycls.v, myfn())

func() # Hello World
Enter fullscreen mode Exit fullscreen mode

You can write a function in one line as shown below:

def func(num1, num2): return num1+num2

print(func(3, 5)) # 8

def func(): return

print(func()) # None

def func(): pass

print(func()) # None
Enter fullscreen mode Exit fullscreen mode

You can indirectly assign a function to a variable but you cannot directly assign a function to a variable like JavaScript except Lambda as shown below:

def func(num1, num2):
    return num1+num2

v = func
print(v(3, 5)) # 8
Enter fullscreen mode Exit fullscreen mode
v = def func(num1, num2):
        return num1+num2
# SyntaxError: invalid syntax

v = func(num1, num2):
        return num1+num2
# SyntaxError: invalid syntax
Enter fullscreen mode Exit fullscreen mode
v = lambda num1, num2: num1+num2
print(v(3, 5)) 8
Enter fullscreen mode Exit fullscreen mode

Top comments (0)