Wednesday, February 2, 2022

Functions and Recursion in Python

A function is block of reusable code that is defined to carry out one particular task. Once you understand the concept of function, you will learn about scenarios where a problem can be solved by a function making call to itself. This is known as Recursion.

Functions form the most important aspect of programming in Python and is the core topic for any object-oriented programming language. Functions are important because:

  1. They provide better readability and modularity*.
  2. Functions help save time and effort in designing and executing the code.
  3. Functions reduces duplication of code.
  4. They make code reusable.
  5. They Make code easy to maintain.
  6. With functions it becomes easier to understand how the code works.
  7. Functions help in information hiding.

In Python programming, functions can be classified into two types:

Built-in functions

Built-in functions are functions that are provided by Python. They are readily available. All the functions that we have worked with till now are all built-in functions such as max(), min(), len(), and so on.

User-defined functions

This chapter is actually all about user-defined functions. These functions are not provided by Python, but are created by programmers to perform a particular task.

Now let us see how to create functions. We know that Functions make code reusable. If there is a block of code that you need to execute, again and again, you can place that block of code inside a function, and call that function whenever you need to execute that specific task. There is a software development practice by the acronym ‘DRY’ that stands for “Don’t Repeat Yourself”. Functions help in keeping the code DRY. This is opposite to another coding acronym called WET, which stands for “Write Everything Twice”. Now we will create our first function.

For creating a function, we will have to follow certain rules regarding:

How to define a function

1. The definition of a function starts with the ‘def’ keyword.

2. The def keyword is followed by the name of the function.

3. The name of the function is followed by parenthesis ().

4. After the parenthesis comes the colon : which marks the beginning of the function’s block of code.

def addNumbers():

The function body

The block of code that comes after the function definition should be indented one level to the right, which is four spaces as per PEP-8.

def addNumbers(a,b):

    c=a+b

    print(c)

Calling a function

You can call the function anytime by its name followed by parenthesis.

addNumbers(a,b):

The function does not execute as long it is called explicitly.

def addNumbers(a,b):

print('value after addition is')

addNumbers(10,20)

The function has only one print statement. There are some points, which are not mandatory but are best practices to follow:

1. Use lowercase letters for function name, words can be separated by underscore. Some also prefer to use camel case.

2. It is recommended to have a docstring as the first part of the function. The docstring must emphasize on what the function does, and not on how it does it.

3. Place the code after docstring.

The output of the code is as follows:

value after addition is 30


Share:

0 comments:

Post a Comment