Introduction to Python Functions

Python Functions is a block of statements that return the specific task.
The idea is to put some commonly or repeatedly done tasks together and make a function so that instead of writing the same code again and again for different inputs, we can do the function calls to reuse code contained in it over and over again.
A function is a block of code which only runs when it is called.
You can pass data, known as parameters, into a function.
A function can return data as a result.

Creating a Python Function

We can create a Python function using the def keyword.

def hello():
    print("rguktweb")
Calling a Python Function

After creating a function we can call it by using the name of the function followed by parenthesis containing parameters of that particular function.

def hello():
    print("rguktweb")
hello()
Arguments of a Python Function

Information can be passed into functions as arguments.
Arguments are the values passed inside the parenthesis of the function. A function can have any number of arguments separated by a comma.

def number(x):
    print(x*4)

number(3)
number(5)
Parameters and Arguments
The terms parameter and argument can be used for the same thing: information that are passed into a function.
From a function's perspective :
  • A parameter is the variable listed inside the parentheses in the function definition.
  • An argument is the value that is sent to the function when it is called.
Types of Arguments

Python supports various types of arguments that can be passed at the time of the function call.

Default Arguments

A default argument is a parameter that assumes a default value if a value is not provided in the function call for that argument.

def default(a,b=4):
    print(a+b)
default(a = 13)
Keyword Arguments

The idea is to allow the caller to specify the argument name with values so that caller does not need to remember the order of parameters.

def default(b,a):
    print(a,b)
    print(a+b)
default(a = 13,b = 4)
Variable-length Arguments

In Python, we can pass a variable number of arguments to a function using special symbols. There are two special symbols.
By default, a function must be called with the correct number of arguments. Meaning that if your function expects 2 arguments, you have to call the function with 2 arguments, not more, and not less.

def default(b,a,c):
    print(a,b)
    print(c)
default(a = 13,b = 4,c = 23)
default(a = 13,b = 4) ## error
Arbitrary Arguments, *args

If you do not know how many arguments that will be passed into your function, add a * before the parameter name in the function definition.
This way the function will receive a tuple of arguments, and can access the items accordingly.

def default(*a):
    print(a[0])
    print(a[1])
default(13,4,23)
Arbitrary Keyword Arguments, **kwargs

If you do not know how many keyword arguments that will be passed into your function, add two asterisk: ** before the parameter name in the function definition.
This way the function will receive a dictionary of arguments, and can access the items accordingly.

def default(**a):
    print(a["hello"])
    print(a["welcome_to"])
default(hello = "world",welcome_to = "rguktweb")
Return Values

The function return statement is used to exit from a function and go back to the function caller and return the specified value or data item to the caller.
The return statement can consist of a variable, an expression, or a constant which is returned to the end of the function execution. If none of the above is present with the return statement a None object is returned.

def default(a):
    print(a)
    return default(a)  ## infinite loop
default(a = "rguktweb")
# Example 2:
def default2(a,b=1):
    print(a)
    if b > 10:
        return 0
    else:
        return default(a,b=b+1)
default2(a = "rguktweb")
The Pass Statement

function definitions cannot be empty, but if you for some reason have a function definition with no content, put in the pass statement to avoid getting an error.

def default(a):
    pass
default(a = "rguktweb")
Docstring

The first string after the function is called the Document string or Docstring in short. This is used to describe the functionality of the function.
The use of docstring in functions is optional but it is considered a good practice.

def default(a):
    "welcome to rguktweb"
print(default.__doc__)
Nested Functions

A function that is defined inside another function is known as the inner function or nested function. Nested functions are able to access variables of the enclosing scope.
Inner functions are used so that they can be protected from everything happening outside the function.

def default():
    print("hello world")
    def main():
        print("rguktweb")
    def main2():
        print("nested string")
    main()
default()