Python Function Fundamentals

Basic function #

How to create and call function in Python?

# create function greet
def greet():
   print("Hello Python!")

# call the greet() function
greet()

Function parameters and arguments #

Function take parameters and arguments for flexibility and usability.

# name => the parameter
def greet(name):
   print(f"Hello {name}")

# call greet() function
# with argument => "ndlrfz"
greet("ndlrfz")

Function with return value #

Difference between print and return in Python:

  • print: Display information to the console for users to see.
  • return: Instead of printing, you can pass the output to the caller, or use return to exit a function or return value.

Example function with print:

# function with print()
def jumlah(a, b):
   print(a + b)

test1 = jumlah(5, 10)

test2 = jumlah(test1, 5)

Result: TypeError

Example function with return:

def jumlah(a, b):
   return a +_ b

test1 = jumlah(5, 15)

test2 = jumlah(test1, 10)
print(test2)

Result: 30

Function with default parameters #

  • Define default parameter and argument within the function.
  • When function get called, the default parameter will be executed if no argument provided by users.

Example:

def greet(name="world"):
   print(f"Hello {name}!")

greet() #output: Hello world!
greet("ndlrfz") #output: Hello ndlrfz!

Function with *args (non-keyword arguments) and **kwargs(keyword arguments) #

  • Function with *args and *kwargs can take any numbers of arguments.
  • The *args or non-keyword arguments such as string and int.
  • The **kwargs or keyword arguments is key value arguments.
  • The * and ** or asterisks that enabled the functionality in Python.
  • You can replace args or kwargs with the name as you want.

Example of *args or non-keyword arguments:

def jumlah_semua(*args):
   return sum(args)

print(jumlah_semua(1, 5, 10, 15, 20, 25, 30)) #output: 106

Example of **kwargs or keyword arguments:

def user_info(**kwargs):
   for key, value in kwargs.items():
      print(f"{key}: {value}")

user_info(name="admin", age=25, born=2000)

#output:
#name: admin
#age: 25
#born: 2000

Todo #

  • High-order function, Lambda, Decorators