Functions in Python - Types and Python Function Arguments

Here, we have learnt about the Python functions, types of python functions and arguments of functions. Python function is a group or block of ...

Hey folks! We have brought you a new topic in which we will discuss about the Python functions, types of python functions and arguments of functions.

Python functions, types of python functions and arguments of functions.

What is Python Function?

Python function is a group or block of related statements which are used to perform a particular task like computation, evaluation, etc. In python, function executes or runs only when they are called. Functions are use to break down the program into manageable and organised modular chunks. 

Programmers generally use functions in python so that they don't have to write a code again and again and they can reuse codes whenever they want. Functions in python makes a program smaller and non repetitive. And as a result, function returns the data.

Syntax of Python Function;

def function_name(parameters):

    """docstring"""

    statement(s)

    return expression

Above, the function header starts with the followed by the names of function to uniquely identify it. Parameters or arguments are added through which a value passes to a particular function. Then, to mark the end of function header  is used. In the next line, is used to tell what that function do. Statement ends up the body of function and it can be one or more than one valid statement. And it is optional but to the value from function, return statement is also added.

Docstring

Docstring or Document string is the string after the function header in the body of function. Docstring generally tells us the functionality of a particular function or simply, we can say that it describes us what a function does. The use of Docstring is optional but it is considered as good programming practice.

Syntax;

print(function_name.__doc__)

Docstring Example:

def statement(x):

    """Welcome to Answersjet and this is python Docstring example"""

    return

#access doc string

print(statement.__doc__)

Output

Welcome to Answersjet and this is python Docstring example

The Return Statement

statement is used to end the body of function and to get the data as a result from the function. Return statement ends the execution of the function and give result from the function where the function is called.

Syntax;

return [expression_list]

The syntax of return statement includes the expression which will be evaluated and it returns the value to the called function.

Example of return statement:

def sum():  

    a =  

    b = 15  

    c = a+b  

  return c  

print("The sum of a and b is:",sum()) 

Output

The sum of a and b is: 20

Types of Functions in Python

In python, there are two types of functions which are explained as follows;

Built-in function

Built-in functions are those functions which comes as a part with the python programming language such as, , etc.

User-defined function

User-defined functions are those functions created by the users to help them out by using keyword.

How to create Functions in Python 

As we read above, user-defined functions are those functions which are created by users by using the keyword. 

def statement():
  print("Welcome to Answersjet")

Calling Python Function

After creating a function, we can use that function in any program or another function just by calling that function by its name followed by the parenthesis with appropriate argument or parameter.

Python Calling Function Example:

def statement():

  print("Welcome to Answersjet")

statement()

Output

Welcome to Answersjet

Arguments of Python Functions

Arguments are type of information which a value passes to a particular function. And these arguments are specified inside the parentheses after the name of function. The programmers can add as many as arguments as they want, just separate them with the help of a comma.

Python Function with arguments Example:

def yesNo(x):

    if (x % 2 == 0):

        print("yes")

    else:

        print("no")

yesNo(2)

yesNo(3)

Output

yes
no

Types of Arguments

There are 4 types of arguments through which we can pass the value to the function when it is called. 

Required Arguments 

Required arguments are those arguments which are required to be passed to a function in a correct positional manner during the time of function call and if it fails to provide the argument or the argument position changes then it will result in an error.

Python Required Arguments Example:

def func(name):    

    message = "Hey! "+name  

    return message  

name = input("Please enter the name:")    

print(func(name)) 

Output

Please enter the name: Deepak
Hey! Deepak

Keyword Arguments

Keyword arguments are those arguments which allows us to call a function and it passes the argument in a random manner. The argument names serve as keyword and identifies by the caller with the help of parameter names. 

Python keyword Arguments Example:

def multiply(x, y):

    return x*y

print(multiply(x = 5, y = 2))

print(multiply(y = 10, x = 7))

Output

10
70

Default Arguments 

Default arguments are those arguments which gives a default value to an arguments in function call when no value is passed to that argument during the time of function calling.

Python Default Arguments Example:

def me(name,age=23):    

    print("My name is",name,"and my age is",age)  

me(name = "Deepak")  

Output

My name is Deepak and my age is 23

Variable-length Arguments

Variable-length arguments are those arguments which allows us to pass arguments in any number when we don't know the exact number of arguments. Variable-length arguments are defined using the as variable name at the function definition.

Python Variable-length Arguments Example:

def addition(*numbers):

    total = 0

    for no in numbers:

        total = total + no

    print("Sum is:", total)

addition()

addition(1, 5, 2, 5, 4)

addition(7, 68, 2.5)

Output

Sum is: 0

Sum is: 17

Sum is: 77.5

Conclusion

From above we have learnt about the Python functions, types of python functions and arguments of functions. Python function is a group or block of related statements which are used to perform a particular task like computation, evaluation, etc. In python, there are two types of functions i.e., Built-in function and User-defined function. Arguments are type of information through which a value passes to a particular function.