DEV Community


Posted on • Updated on

Understanding Functions In Python

Difficulty: Easy

What's of Function ?

Function can simply be defined as a sequence of statements that execute in a certain order.For function to work it has to be called or invoked,by simply using the function’s name with parentheses.

There are three types of functions in Python:
1.Built-in functions : These are functions that come preloaded in python.You only need to call them.Examples: help(),print() , min()
2.User-Defined Functions (UDFs):These are functions created by the user to avoid repetition of code.In this blog,We will majorly focus on this type of functions.
3.Anonymous functions: which are also called lambda functions because they are not declared with the standard def keyword.

1. User-Defined Functions

In python,UDFs are defined using a keyword "def"

Syntax of a function

def function_name(arguments):
      write your code here
Enter fullscreen mode Exit fullscreen mode

Note: A function can take an argument(1 argument or more) or no argument.You can add as many arguments as you want, just separate them with a comma.Further discussion on argument will be discussed on a different section of this blog.
Example 1 : Create a function with no argument

#Function with no argument
def Greetings():
  print('Hello ,Merry Christmas')
Enter fullscreen mode Exit fullscreen mode

Example 2 :Create a function that multiply any number passed to it by 10 and takes one argument

def Multiply_By10(num):
    num = num * 10
#calling the function
Enter fullscreen mode Exit fullscreen mode
Result: 70
Enter fullscreen mode Exit fullscreen mode

For more examples on arguments , check out this Notebook.
Before proceeding, you will realize , in some context ,they will use the name Argument and parameters interchangeably.

What's the difference between Parameters and Arguments ?

Both are used in the function, but a parameter may refer to a variable listed inside the parentheses in the function definition . In Example 2 , "num" is a parameter.

Now, an argument may be defined as the value that is sent to the function when it is called. In example 2 the value "7" is an argument

Type of Arguments

1.Default arguments:

These are arguments with default values assigned to them during function definition. We assign a default value to an argument using the assignment operator in python(=).
When calling such a function without passing any argument it will use the default value. Example:

def Say_Name(user="Kim"):
    print(f'Welcome back home,{user}'
#This will return
Welcome back Home ,Kim
Enter fullscreen mode Exit fullscreen mode

NB:For all types of arguments example codes, check out this Notebook on Google Colab

2.Keyword arguments:

In python functions,the values passed through arguments are assigned to parameters in order, by their position.
With Keyword arguments, you have full control and flexibility on the values passed as arguments.

def multiply(a,b):
  return a*b
Enter fullscreen mode Exit fullscreen mode
Enter fullscreen mode Exit fullscreen mode
3.Arbitrary Arguments

Arbitrary arguments saves the situation where you don't know how many arguments the function will take.
When defining functions ,We place an asterisk ( * ) before the parameter to denote that the function can take an arbitrary number of arguments.
Example:Create a function that multiply numbers

def feat(*args):
    mult = 1
    for arg in args:
       mult = mult * arg
    return mult

Enter fullscreen mode Exit fullscreen mode
Enter fullscreen mode Exit fullscreen mode

Example 4

def Funt(*args):
    for arg in args:
        print (arg)
Funt('Hello', 'Welcome', 'to', 'Kenya')
Enter fullscreen mode Exit fullscreen mode
Enter fullscreen mode Exit fullscreen mode

However, there are Arbitrary Keyword Arguments which will help in situation where you don't know how many keyword arguments that will be passed into your function.In such situation we add two asterisk ** before parameter name in the function definition.

def fun_1(**kwargs):
    for key,value  in kwargs.items():
fun_1(name='titus' ,home='Costa Rica') 

Enter fullscreen mode Exit fullscreen mode

You will realize that *args and **kwargs both take uncounted number of arguments. However, the two differ in the following way:

  • *args- use it when iterating through a list
  • **Kwargs - use it when iterating through a dictionary

2. Anonymous functions AKA Lambda function

Lambda function is a function that take any number of arguments, but can only have one expression.We don't assign names to lambda functions and can be used inside other functions.
lambda arguments : expression

multiply_by10 = lambda x: x *10
Enter fullscreen mode Exit fullscreen mode


By now,you should understand how to write function and using different arguments.But wait, do you know that these arguments follow some orders ?
Yes they do,I will list their order below:

  1. Default Argument
  2. Keyword Argument
  3. Arbitrary Argument
  4. Arbitrary Keyword Argument Example:
def cup(t = 10,a,b,*args,**kwargs):
Enter fullscreen mode Exit fullscreen mode

Follow for more post

Top comments (4)

digital_hub profile image

many thanks for this awesome introduction - it is very helpful

syedjafor profile image
Syed Jafor ⚡

I think the last code will be:
multiply_by10 = lambda x: x *10

titusnjuguna profile image

Thank you for the correction.

aatmaj profile image

Nice! You all might also like my learning python Course