Every Fresher Must Apply - Register Now

Function In Python||Recursion Using Python

Register yourself to get hired soon:- Register Now

Hey Guys. Welcome to W3hiring.com.

In this Article. we will learn functions in python in detail with coding examples. This article is followed by the previous article. Please visit the previous article first to understand this concept easily. Funtion in Python

What is Funtion In Python?

A function is generally a block of code that comprises the general programming of a programming statement. it can provide an easy way for reusing a programming statement it can also decrease the complexity of the program.

Types of funtion in Python:-

Generally. There are two types of functions in python

  1. Pre_Defined or System Defined Funtions.
  2. User Defined Funtions.

User Defined Funtions:-

A user-defined function is defined by a user for a particular set of operations. A user-defined function has been classified into four categories,

  1. Function with no argument and no return value.
  2. Function with argument but no return value.
  3. Function with argument and return value
  4. funtion with no argument but return value.

How to define user defined Function?:

Syntex->

def funtion_Name(parameters):
    "docsstring"
    statements

Function with argument and no return value.

For Example:-

def sum(x,y):
    "Going to add x and y"
    s=x+y
    print("sum of two numbers is")
    print(s)

#Now calling sum Funtion
sum(10,5)

#Output:- 15

In the above code, we defined a function named sum. and we gave two parameters to the sum functions x and y. After that, we add two parameter values using the plus operator. We call the sum function in the last line of code with two values of the parameter by using the call by values method. we got a result of 15 in the output (10+5=15)

What is Return Statement?

Return (Expression) is used to send back the control to the caller with the expression. In case no expression is given after return. It will return none.

In other words, a return statement is used to exit the function definition

For Example:-

def sum(a,b):
    "Adding the two values"
    print("printing within function")
    print(a+b)
    return(a+b)  #for returning value.

A return function is used to stop the execution and returning the value. if we want to return something we can use the return function. In the above code, we return the addition of two values. whose store in two variables named a and b. And in the output, we will get an addition of values as a return.

Let’s See an Example for No argument and no return value:-

def msg():
    print("hello")
    return  #for funtion existing
total= sum(10,20)
print("Printing outside:",total)
msg()
print("Rest of code")

#Output:- Printing within functoion 30
Printing outside 30
hello
rest of code

in the above code, We define a function using the def keyword name msg(). To print something we used print funtion. As we have defined the sum function in the earlier code. Now we are going to use that function here in the above code. We store the value of some function in a variable named Total After we called the msg function to run it. in the last we will get three lines of output.

Passing argument and Paramenter:-

Python supports the following types of formal argument:

  1. Positional argument (required argument)
  2. Default argument
  3. Keyword Argument(named argument

Example of Positional arguments:-

Now we are going to learn about positional arguments. The positional argument is nothing a depends on a particular position.

def addition(x,y):
    print(x+y)
x=15
addition(x,10)  #15+10
addition(x,x)   #15+15
y=20
addition(y,x)   #20+15

#Output:-25
30
35

As we have done above code. we define a function using the def keyword as we typically define in python. we provide two parameters to addition function. In the first line of instruction, we used a plus operator to add the values and print them. after that we store some constant values (x=15) in the x variable and then call the addition function two times. when we call the addition to function the first time we provide the first parameter as variable and the second as constant. And in the second calling function, we provide both parameters as the same variable. and in the last one calling which is the third one we provide both parameters with a different variable.

Default argument:-

Default values indicate that the argument of funtion will take that value if no argument value is passed during the function call

def msg(Id,name, age=30):
    "Printing the passed value"
     print("Id is:", Id)
     print("Name is:", name)
     print("Age is:",age)
     return

funtion call
msg(100,"Aman",20)
msg(101, "Deepak")

above code is simple we define a function using a def keyword with three arguments. and we already have been defined the third parameter name age=30. and then we print the value of all the parameters variable.

Keyword Argument:

Using the keyword argument, the argument passed in the function call is matched the name of the parameters.

for example:-

def msg(id,name):
    "Printing passed value"
    print("Id is:" , id)
    print("Name is:" , id)
    return()

#function call
msg(id=100, name="raj")
msg(name="Rahul", id=101)

we defined a function msg here using the def keyword. And we are using a keyword argument to pass the value in the function. it will match the value based on the Name of the argument.

Scope of varible:-

Scope of the variable are of two types:-

  1. Local variable
  2. Global variable

Example of local Variable:-

def msg():
    a=10  #here a is a local variable it can be access inside msg function only
    print("value of a is :",a)
    return
msg()
print(a) #it will show an error since variable is local.

Note:- if we define a variable inside a function that is called a local variable. here variable a is a local variable it can be accessed inside the msg function only, We can not access ‘variable a’ outside msg function. if we access variable an outside then it will give us an error in response.

Example of Global variable:

b=20   #here b is a globle varible.
def msg():
    a=10
    print("value of a is:",a)
    print("value of b is:",b)
    return
msg()
print(b)

Note:-Variable b is a Global variable. because It is defined globally and it is not defined inside a particular function like the above msg function. So ‘variable b’ can access everywhere inside a program.

Python Recursion

We Know that in Python, a function can call other functions. It is even possible for the function to call itself. these types of constructs are termed recursion.

Example of Recursion Function:-

def rec_fact(x):
    "This is a recursion function to find the factorial of an integer"
    if(x==1):
        return 1
    else:
        return(x*rec_fact(x-1))
num=int(input("Enter a no:"))
if num>=1:
    print("the factorial of", num, "is", rec_fact(num))

Lamda Function Or Anonymous Function:

In Python, Anonymous Function is a function. That is defined without a name Anonymous functions are created by using the keyword “lambda”. A Keyword “lambda” can take any number of arguments and returns an evaluated expression.

Lambda is created without using the def keyword. We use lambda functions when we require them.

A nameless Function for a short period of time. Lamda function are used along with built-in function like filter(), map() , reduce() etc.

Function definition:-

#Normal function to Square a number

def square(x):
    return(x*x)

#Lambda Function to square a number

Square=lambda x1: x1 *x1
#Calling square as a function
print("Square of number is :", Square(10))

#Output: 100.

Leave a Comment