functions and recursion in Python

Functions and Recursions in Python |Episode #11|

Hello and welcome to python tutorial for beginners, where we teach you python programming from scratch. We have seen a lot in our python journey. In the last class, we learned about Dictionary and Sets in Python. If you haven’t seen that class, CLICK HERE. Today we will be seeing one of the most important topics in Python Programming or any programming in general and that is “Functions and Recursions” in Python.

What are Functions?

functions in python

 A Function is a group of statements performing a specific task. 

A function can be used by the programmer any number of times. With that statement, we will be thinking, how it is different from loops. Well, it is not in a conceptual manner. We use loops in order to mitigate the repetition. In the same way, we do this by function. Syntax of function is:

 def Func1():

       print(“Hello”)

Now, this function can be called any number of times and anywhere in the program. Functions are mostly used to provide modularity and code reusability. If we want to print Hello in a program anywhere, we won’t have to do that, every time by writing or copying the same code. We do it smartly by using functions.

Function Call

Whenever we want to call a function, we put the name of the function followed by parenthesis as follows:

Func1() – This is called Function call.

Function Definition

The part containing the exact set of instructions that are executed during the function call. In laymen terms, a function definition is the code which you want to run every time you call the function.

Types of Functions

  • Built-in functions – Functions which are already present in python. Ex – range(), input(), print(), etc.

  • User-defined functions – Functions that are defined by the user. Ex – func1()

Functions with Arguments

A function can accept some values it can work with, we can put these values in the parenthesis. A function can also return values as shown below.

             def greet(name):

                     gr = “Hello” + name

                    return gr

             a = greet(“Kevin”)   

In order to understand function well. Let’s write a program that uses function name func1 and takes two arguments and return the sum of two numbers.

functions

function answer

Here we take a function func1() and we give two arguments num1 and num2, then we return the sum of both numbers. After that we ask the user to enter two numbers, we have to typecast them as int as the input function doesn’t take default arguments as int, it takes string instead. Then we call the func1 by passing two arguments a and b, which is our entered numbers, and print the result.

How this works is that the numbers you passed their copies are sent to the function, it’s like their value got stored in the arguments num1 and num2 then the operation is done of those two values and then it returns to the func1. Now when we call func1, we already have the return value and our result.

Default parameter values

We can have a value as a default argument in a function.

If we specify name = “Stranger” in the line containing def, this value is used when no arguments are passed. Ex: 

default function

default function answer

What is Recursion?

recursion in python

Recursion is a function which calls itself.

 Like in real, when a function call itself, is called Recursion. It is used to directly use a mathematical formula as a function. Ex:

             Factorial(n) = n * factorial(n-1)

The formula can also be done using loops, which is called the iterative method as we iterate through items in loops but this question is best to solve using recursion as it has its own formula, which makes it easier to implement.

Let me tell you for example how factorial works if you are not familiar. For example, we want to find out factorial of 5. 

             Fact(5) = 5 * 4* 3* 2* 1 

Which is 120. But we can also write it like this.

      Fact(5) = 5 * fact(4) and so on..

base condition

Which gives us this formula, which is mentioned above. But here’s the catch it will go only till fact 0 which is 1 or fact 1 which is also 1 and that’s very very important while using recursion. If we don’t specify that then it will go into negative numbers which we don’t want. This is called Base condition. We have to know and specify the base condition before using recursions because without a base condition, it will end up becoming an infinite loop, till the memory gets exhausted and you get a segmentation fault.

Now let’s built out the program using recursion.

recursion

recursion answer

One thing that should be noted down here is that not every question is suitable to use recursion to solve them. Some like this one, which has a straightforward formula that can be used. Plus, we have to have a base condition beforehand otherwise, it’s no good.

On that note, that’s all for today’s class. Today we saw what are functions, Types of Function, How to use them and then we take a look at Recursion or Recursive Functions. Functions are very much used in Python Programming. So start practicing them. To look more about Functions and Recursions, CLICK HERE. Till then, this is Dolores Haze, signing off, I will meet you in the next one, “Happy Coding” and Bye.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top