Share @ LinkedIn Facebook  python, basics
Python Programming Guide For Beginners - Part 5

Python Programming Guide For Beginners - Part 5

Table Of Content

  1. Learning About Python Funcions
  2. What Is The Use Of Function In Python?
  3. Syntax of functions in Python
  4. What Are The Different Types Of Functions In Python?
  5. Default Arguments In Function
  6. Python Recursion
  7. Why Do We Need To Use Recursion in Programming?
  8. What Are The Pros And Cons Of Recursion In Programming?

Learning About Python Functions

This tutorial will help the python beginners to learn functions in python.

Definition:

A function is a block of code that contains one or more Python statements and used for performing a specific tasks.

What Is The Use Of function In Python?

As I mentioned above, a function is a block of code that performs a specific task. Lets discuss what we can achieve in Python by using functions in our code:

1. Code re-usability:

Lets say we are writing an application in Python where we need to perform a specific task in several places of our code, assume that we need to write 10 lines of code to do that specific task. It would be better to write those 10 lines of code in a function and just call the function wherever needed, because writing those 10 lines every time you perform that task is tedious, it would make your code lengthy, less-readable and increase the chances of human errors.

2. Improves Readability:

By using functions for frequent tasks you make your code structured and readable. It would be easier for anyone to look at the code and be able to understand the flow and purpose of the code.

3. Avoid redundancy:

When you no longer repeat the same lines of code throughout the code and use functions in places of those, you actually avoiding the redundancy that you may have created by not using functions.

Syntax of functions in Python

Function declaration:

def function_name(function_parameters):
    function_body # Set of Python statements
        return # optional return statement

Syntax For Calling The Function:

when function doesn't return anything

function_name(parameters)

OR

# when function returns something
# variable is to store the returned value
variable = function_name(parameters)

Python Function Example

Here we have a function add() that adds two numbers passed to it as parameters. Later after function declaration we are calling the function twice in our program to perform the addition.

In [1]:
def add(num1, num2):
    return num1 + num2


sum1 = add(100, 200)
sum2 = add(8, 9)
print(sum1)
print(sum2)
300
17

What Are The Different Types Of Functions In Python?

There are two types of functions in Python:

  1. Built-in functions: These functions are predefined in Python and we need not to declare these functions before calling them. We can freely invoke them as and when needed.

  2. User defined functions: The functions which we create in our code are user-defined functions. The add() function that we have created in above examples is a user-defined function.

Default Arguments In Function

We have alredy learn about the functions and its types. Now, it is important to learn how to declare a call a function. Now we will have to see how can we use the default arguments.

By using default arguments we can avoid the errors that may arise while calling a function without passing all the parameters. Lets take an example to understand this:

In this example we have provided the default argument for the second parameter, this default argument would be used when we do not provide the second parameter while calling this function.

In [3]:
# default argument for second parameter
def add(num1, num2=1):
    return num1 + num2


sum1 = add(100, 200)
sum2 = add(8)  # used default argument for second param
sum3 = add(100)  # used default argument for second param
print(sum1)
print(sum2)
print(sum3)
300
9
101

Python Recursion

A function is said to be a recursive if it calls itself. For example, lets say we have a function abc() and in the body of abc() there is a call to the abc().

Python Example Of Recursion

In this example we are defining a user-defined function factorial(). This function finds the factorial of a number by calling itself repeatedly until the base case(We will discuss more about base case later, after this example) is reached.

In [5]:
# Example of recursion in Python to
# find the factorial of a given number

def factorial(num):
    """This function calls itself to find
    the factorial of a number"""

    if num == 1:
        return 1
    else:
        return (num * factorial(num - 1))


num = 5
print("Factorial of", num, "is: ", factorial(num))
Factorial of 5 is:  120

Why Do We Need To Use Recursion in Programming?

Explaination: We use recursion to break a big problem in small problems and those small problems into further smaller problems and so on. At the end the solutions of all the smaller subproblems are collectively helps in finding the solution of the big main problem.

What Are The Pros And Cons Of Recursion In Programming?

Pros of Recursion Recursion makes our program:

  1. Easier to write.
  2. Readable – Code is easier to read and understand.
  3. Reduce the lines of code – It takes less lines of code to solve a problem using recursion.

Cons of Recursion

  1. Not all problems can be solved using recursion.
  2. If you don’t define the base case then the code would run indefinitely.
  3. Debugging is difficult in recursive functions as the function is calling itself in a loop and it is hard to understand which call is causing the issue.
  4. Memory overhead – Call to the recursive function is not memory efficient.

Dolly Solanki  Dolly Solanki