Python Working With Functions

Python Working With Functions

A function in python defined as a a group of statements that required to perform a task which takes required inputs and returns the output. We have two types of functions 1. built-in functions and 2. user defined functions. We have many built-in functions in python. You can see the list of python built-in functions in in python documentation.

Syntax of python function:

def function_name(arg1, arg2):
    # statement 1
    # statement 2
    return None

"def" is a keyword in python which is used to define the function in python. A python function may take or may not take arguments. In above syntax we have used return statement. The "return" statement is used to return the result of a function. If return statement is not included in the function code then the python function returns the "None" value by default.

Python funciton to find the factorial of a number

def factorial(number):
    result = 1
    for i in range(1, number+1):
        result = result * i
    return result
# call the function
# Output: 24
# Output: 120

Above function take a number and returns the factorial of that number. In above we have called the function twice. Each time we call the function we used difference input.