1 of 18

Functions

2 of 18

  • A function is a block of code which only runs when it is called. You can pass data in to a function is known as parameters/ arguments.
  • A function can return data as a result.
  • Types of Functions
  • Basically, we can divide functions into the following two types:
  • Built-in functions - Functions that are built into Python.
  • User-defined functions - Functions defined by the users themselves.

3 of 18

User-defined functions :

  • Creating or defining a Function
  • In Python a function is defined using the “def” keyword:

1. To create a function without parameters:

Syntax:

def function name():

………………..

………………..

Block of statements

…………………..

2. To create a function with parameters:

Syntax:

def function name(arg1, arg2, arg3,………………,argn):

………………..

………………..

Block of statements

…………………..

4 of 18

Calling a Function: (To invoke or access a function)

To call a function, use the function name followed by parenthesis

Syntax: To call a function without parameters

function name()

Syntax: To call a function with parameters

function name(arg1, arg2, arg3,………, argn)

5 of 18

Example 1: Without Parameters:

6 of 18

7 of 18

Parameters to Functions :

  • Information can be passed into functions as arguments. Arguments are specified after the function name, inside the parentheses.
  • You can add as many arguments as you want, just separate them with a comma.
  • The terms parameter and argument can be used for the same thing: information that are passed into a function.
  • A parameter is the variable listed inside the parentheses in the function definition.
  • An argument is the value that is sent to the function when it is called.

8 of 18

9 of 18

  • In Python there are other ways to define a function which can take variable number of arguments.
  • Default Arguments
  • Positional Arguments
  • Keyword Arguments
  • Arbitrary Arguments

10 of 18

  • Default Arguments:
  • We can provide a default value to an argument by using the assignment operator (=).

11 of 18

  • In this function, the parameter name does not have a default value and is required (mandatory) during a call.
  • On the other hand, the parameter msg has a default value of "Good morning!".
  • So, it is optional during a call. If a value is provided, it will overwrite the default value.
  • Any number of arguments in a function can have a default value. But once we have a default argument, all the arguments to its right must also have default values.
  • This means to say, non-default arguments cannot follow default arguments.
  • Example:
  • def wish(msg = "Good morning!", name):
  • We would get an error as:
  • Syntax Error: non-default argument follows default argument

12 of 18

Positional & Keyword Arguments:

  • When we call a function with some values, these values get assigned to the arguments according to their position.
  • For example, in the above function wish(), when we called it as wish(“Zara”, "How do you do?"), the value “Zara" gets assigned to the argument name and similarly "How do you do?" to msg.
  • Python allows functions to be called using keyword arguments. When we call functions in this way, the order (position) of the arguments can be changed.

13 of 18

  • # 2 keyword arguments
  • wish(name = “Zara", msg = "How do you do?")
  • # 2 keyword arguments (out of order)
  • wish(msg = "How do you do?",name = “Zohara")
  • # 1 positional, 1 keyword argument
  • wish(“Zara", msg = "How do you do?")
  • we can mix positional arguments with keyword arguments during a function call.
  • But we must keep in mind that keyword arguments must follow positional arguments.
  • Having a positional argument after keyword arguments will result into errors.
  • For example the function call as follows:
  • wish(name=“Zara","How do you do?")
  • Will result into error as:
  • Syntax Error: non-keyword arg after keyword arg

14 of 18

  • Arbitrary Arguments:
  • Sometimes, we do not know in advance the number of arguments that will be passed into a function. Python allows us to handle this kind of situation through function calls with arbitrary number of arguments.
  • In the function definition we use an asterisk (*) before the parameter name to denote this kind of argument.

15 of 18

  • Recursive Functions :
  • Recursion is the process of defining something in terms of itself.
  • return statement
  • The return statement is used to exit a function and go back to the place from where it was called.
  • Syntax of return
  • return [expression_list]
  • This statement can contain expression which gets evaluated and the value is returned. If there is no expression in the statement or the return statement itself is not present inside a function, then the function will return the None object.

16 of 18

17 of 18

  • Lambda or Anonymous Function :

  • In Python, anonymous function is a function that is defined without a name.
  • While normal functions are defined using the def keyword, in python anonymous
  • functions are defined using the lambda keyword.
  • Hence, anonymous functions are also called lambda functions.

Syntax:

lambda arguments: expression

Lambda functions can have any number of arguments but only one expression.

The expression is evaluated and returned. Lambda functions can be used wherever function

objects are required.

18 of 18

Example

A lambda function that adds 10 to the number passed in as an argument, and print the result:

x = lambda a : a + 10print(x(5))

Example

A lambda function that multiplies argument a with argument b and print the result:

x = lambda a, b : a * b�print(x(5, 6))

Example

A lambda function that sums argument a, b, and c and print the result:

x = lambda a, b, c : a + b + c�print(x(5, 6, 2))

Use of Lambda Function in python

We use lambda functions when we require a nameless function for a short period of time.

In Python, we generally use it as an argument to a higher-order function

(a function that takes in other functions as arguments). Lambda functions are used along

with built-in functions like filter(), map() etc.