DEV Community

Tito
Tito

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
      print()
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')
Greetings()  
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
    print(num)
#calling the function
Multiply_By10(7)
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}'
Say_Name()
#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.
Example:

def multiply(a,b):
  return a*b
e=multiply(b=10,a=19)
d=multiply(a=19,b=10)  
print(e)
print(d)
Enter fullscreen mode Exit fullscreen mode
#Result
190
190
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
print(feat(9,8,5,12,16))   

Enter fullscreen mode Exit fullscreen mode
#Result
69120
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
#Result
Hello
Welcome
to
Kenya
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.
Example

def fun_1(**kwargs):
    for key,value  in kwargs.items():
        print("%s==%s"%(key,value))
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.
Syntax
lambda arguments : expression
Example

multiply_by10 = lambda x: x *10
print(multiply_by10(69))
#Results
690
Enter fullscreen mode Exit fullscreen mode

SUMMARY

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):
    pass
Enter fullscreen mode Exit fullscreen mode

THANK YOU READING.
Follow for more post

Top comments (4)

Collapse
 
digital_hub profile image
hub

many thanks for this awesome introduction - it is very helpful

Collapse
 
syedjafor profile image
Syed Jafor ⚡

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

Collapse
 
titusnjuguna profile image
Tito

Thank you for the correction.

Collapse
 
aatmaj profile image
Aatmaj

Nice! You all might also like my learning python Course