Function in Python


Function in Python

The most important aspect of an application is its functions. A function is an organized block of reusable code that may be invoked anytime it is needed. Python has a number of tools for defining and invoking functions.

The Python programming language provides the Python standard library to define and execute functions. A function is defined by placing a def statement in a module. The function can be called by passing the arguments to it as an argument list, or by using the function name as a string.

Python functions are defined by def statement. This statement defines the function by defining its name and arguments. In addition to this, we have the optional arguments after the function name.

Function in Python
Function In Python


There are mainly two types of functions.

1.User-define functions -User-defined functions can be defined by using the def statement. In this statement, we have the name of the function, the arguments and the body of the function. The def statement also allows to have arguments, which are passed to the function.

2.Built-in functions-The built-in functions are provided by the python interpreter itself, while user-defined functions are user-defined functions.

Creating a Function

Using the def statement

A function is defined by placing a def statement in a module. This statement defines the function by defining its name and arguments. In addition to this, we have the optional arguments after the function name.

syntax of functions definition.

def function_name(argument_list):

{code}

In this statement, we define a function by name function_name. We have arguments argument_list as argument for this function.

def is used to define the functions.

Let’s create a function using the def statement.

def add(x,y):
  return x+y

This is the function definition statement. In this statement, we have named the function add. We have used argument list as a parameter for the function. This function has two parameters, one is the argument x and the other is y. This statement will return the sum of x and y.

Function Calling

Python provides various tools for defining and calling functions. We can call a function by passing the arguments to it as an argument list. In this statement, we pass the argument list to the function.

In the following example, we pass the argument list to the function add.

add(2,3)

This is the way to call a function. We have passed the argument list to the function.

Arguments in function

In the function, we have passed the arguments to the function as an argument list. We have used the argument list to pass the arguments to the function.

def add(x,y):
  return x+y

add(2,3)

5

Types of arguments

Required arguments -

The required arguments are the arguments, which we have passed to the function. We have not passed the arguments in the function, then it will return an error.

def add(x,y):
  return x+y

add(3)

Output:

Traceback (most recent call last):
File "", line 1, in
add(3)

NameError: name 'x' is not defined


Keyword arguments-

The keyword arguments are the arguments, which are not passed to the function as an argument list. This is an optional argument. We can pass keyword arguments to the function.

def add(x,y,z=3):
  return x+y+z

add(3)

output:
6


Default arguments -

Default arguments are the arguments, which are passed to the function as an argument list, but we have not passed the arguments in the function. This is an optional argument. We can pass default arguments to the function.

def add(x,y,z=3):
  return x+y+z

add(3)

output:
6


Variable-length arguments-

We may not know the quantity of arguments to be passed in advance in huge projects. In such circumstances, Python allows us to give comma-separated values that are internally handled as tuples at the function call. We may pass any number of arguments by using variable-length parameters.

However, in the function definition, we specify the variable-length parameter as *variable - name > using the *args (star).

def myFun(*argv):
    for arg in argv:
        print (arg)
   
myFun('Hello', 'Welcome', 'to', 'World')

Output:

Hello
Welcome
to
World


The Anonymous Functions

The anonymous functions are used in Python to represent an anonymous function. They are not required to be defined by using the def statement. They are called by using the lambda keyword.

We can create an anonymous function using the lambda keyword.

a = lambda x: x*x

a(5)

Output:

25


The lambda Keyword -

  • The lambda keyword is used to create an anonymous function. It has three parameters.
  • The first parameter is the name of the function.
  • The second parameter is the body of the function.
  • The third parameter is the argument list.

In the following example, we have created an anonymous function using the lambda keyword.

a = lambda x: x*x

a(5)

Output:

25

Using the lambda keyword

a = lambda x, y: x+y

a(3, 5)

Output:

8

Function definition with lambda keyword -

We can create a function using the lambda keyword and also define the arguments.

def f(x,y):
 return x+y

f(5,2)

Output:

7

We can pass arguments to the function.

Advantage of Functions in Python

  • We can create functions by defining a name and arguments and defining the body of the function.
  • We can call the function by passing the arguments to it.
  • We can use any number of arguments in the function definition.
  • We can define the default arguments in the function definition.
  • We can pass the keyword arguments to the function.
  • We can pass the variable-length arguments to the function.

What is the difference between a function and a method?

Method-

  • Methods definitions are always present inside a class.
  • Methods are associated with the objects of the class they belong to.
  • A method is called ‘on’ an object. We cannot invoke it just by its name
  • Methods can operate on the data of the object they associate with
  • Methods are dependent on the class they belong to.
  • A method requires to have ‘self’ as its first argument.

Function-

  • We don’t need a class to define a function.
  • Functions are not associated with any object.
  • We can invoke a function just by its name.
  • Functions operate on the data you pass to them as arguments.
  • Functions are independent entities in a program.
  • Functions do not require any ‘self’ argument. They can have zero or more arguments.

Tech Smart

Hello, my name is Sam and I am the blog's author. I enjoy blogging since I am passionate about it and I am an enthusiastic learner. Blogging provides me a greater platform to share all of my knowledge and experiences with my readers.

Post a Comment

if you have any doubts, Please let me know

Previous Post Next Post