Basic Python Function

Thapanee Boonchob
3 min readNov 1, 2020

Function is block of code that will run only when it is called.

You can enter/not enter parameter(s) into function (depend on your function). When calling the function. It will run and the result will be returned.

How to define function?

Creating a function starts with the keyword (1) def followed by the (2) function name. Then add a (3) parameter (if any) within parentheses and end this line with (4) a colon.

After that, you can write (5) a code inside the function block and end it with (6) return statement (optional).

def myfirst_function():
print("This is my first function")

Calling a function

calling the name of the function followed by parentheses and parameters (if any)

myfirst_function()>>> This is my first function

Add Parameter(s) to function

We can insert parameters into functions (parameter is variable but actual value in parameter is called argument)

def sum_value(num1, num2):
print(num1+num2)
sum_value(10, 25)
>>> 35

The precautions for inserting arguments into the function are that the arguments must be sorted correctly and the number of arguments must be equal to number of parameters, otherwise an error will occur.

def sum_value(num1, num2):
print(num1+num2)
sum_value(10)
>>> TypeError: sum_value() missing 1 required positional argument: 'num2'

Keyword Argument

You can call a function and put arguments as key = value, so you don’t need to sort the arguments correctly.

def hello_function(firstname, lastname):
print(f"Hello {firstname} {lastname}")
hello_function('Boonchob', 'Thapanee')
>>> Hello Boonchob Thapanee
hello_function(lastname='Boonchob', firstname='Thapanee')
>>> Hello Thapanee Boonchob

Default Parameter

Default Parameter is the assignment of arguments, if the function is called but not received the arguments, the program will run without an error.

def myfavorite_food(food='pizza'):
print(f"My favorite food is {food})
myfavorite_food('noodle')
>>> My favorite food is noodle
myfavorite_food()
>>> My favorite food is pizza

Return Values

From all examples above you can see that the function will print the result as output, but if we want the result go into the variable. We can use return command.

def sum_function(num1, num2):
return num1+num2
sum_function(10, 25)
>>>
result = sum_function(10, 25)
print(result)
>>> 35

You can see that this time the function will not print the output, but it will insert the result value into the variable. If we do not have a variable to support the function call. Calling the function will have no effect.

Lambda function

Lambda is a one-line function, commonly used in place of simple functions. The syntax of lambda is lambda parameter(s): expression.

result = lambda a, b: a+b
print(result(10, 25))
>>> 35

If-Else in Function and Lambda

We can use if-else statement in a function or lambda function.

def double_even_number(num):
if num%2 == 0:
return num*2
else:
return num
result_func = double_even_number(10)
print(result_func)
>>> 20
result_lambda = lambda x: x*2 if x%2 == 0 else x
print(result_lambda(10))
>>> 20

I will separate the lambda function with if-else into 3 parts:

  1. lambda x: define variable x
  2. x*2 if x%2 == 0 return x*2 if x mod 2 equal to 0 or x is even number (True)
  3. else x return x if x mod 2 not equal to 0 or x is odd number (False)

For more details or to learn other topics (Thai version) please visit

--

--