INTRODUCTION TO FUNCTIONS

In this tutorial we will learn about functions , what do we mean by it , how  can they made, operations etc.

functions

reference :https://robogarden.ca

Functions

A function is a self-contained block containing a block of code that performs a specific task.

This means function performs the same task when called, which avoids the need of rewriting the same code again and again.

A function can be defined as the organized block of reusable code which can be called whenever required.

Python allows us to divide a large program into the basic building blocks known as function. The function contains the set of programming statements enclosed by {}.

A function  provide reusability and modularity to the python program.

Advantages of functions

  • It  avoid writing same code again and again in a program.
  • We can call python functions any number of times in a program and from any place in a program.
  • We can track a large program easily when divided in multiple functions.
  • Re-usability is the main achievement of python functions.
  • However, Function calling is always overhead in a python program.

How to create a function

We can create a function using def keyword .

Syntax:

def my_function():
  function-suite
  return <expression>

The function block is started with the colon (:) and all the same level block statements remain at the same indentation.

A function can accept any number of parameters that must be the same in the definition and function calling.

Function call

function should be define before  function calling . Once the function is defined, we can call it from another function or the python prompt. To call the function, use the function name followed by the parentheses.

Example :

def hello_world():
    print(“hello world”)
   hello_world()

Output:

Parameters in function

The information in the functions are passed as  parameters. The parameters are specified in the parentheses. We can give any number of parameters, but we have to separate them with a comma.

Example:

def sum (a,b):   #defining the function
 return a+b;
a = int(input(“Enter a: “))
b = int(input(“Enter b: “))
print(“Sum = “,sum(a,b))

Output:

Scope and types of the variable

The scope of a variable tells us about how can we access it in a program. there are two kinds of variables in Python.

  • Local Variables:

Global variables are the one that are defined and declared outside a function and we need to use them inside a function.

Example:

def f():
print s s = “I love toys”       # Global scope f()

Output:

  • Global Variables

a variable type that can be access through out program.

Example:

def f():
global s
print s s = ”  Python “
print s s = “Python is great!”          # Global Scope
f()
print s

Output:

Function  arguments

values that are passed to the function at run-time so that the function can do the designated task using these values.

Example:

Output:

Types of function arguments

There are three types of Python function arguments using which we can call a function.

  1. Default Arguments
  2. Keyword Arguments
  3. Variable-length Arguments
  • Default Arguments:

default arguments which assumes a default value if a value is not supplied as an argument while calling the function. In parameters list, we can give default values to one or more parameters.

Example:

Output:

  • keyword Arguments:

In function, the values passed through arguments are assigned to parameters in order, by their position.

With Keyword arguments, we can use the name of the parameter irrespective of its position while calling the function to supply the values. All the keyword arguments must match one of the arguments accepted by the function.

Example:

Output:

  • Variable-length Arguments:

If we don’t know in advance about the arguments needed in function, we can use variable-length arguments also called arbitrary arguments.

Example:

Output:

Lambda Functions

anonymous functions are declared by using lambda keyword. However, Lambda functions can accept any number of arguments, but they can return only one value in the form of expression.

The anonymous function contains a small piece of code. It simulates inline functions of C and C++, but it is not exactly an inline function.

Syntax:

Example:

x = lambda a:a+10
print(“sum = “,x(20))

Output:

Advantages of lambda function

  1.  code is simple and clear.
  2. No extra variables are added.

Use of lambda() with map()

The map() function in Python takes in a function and a list as argument. function  called with a lambda function and list ,a new list is returned which contains all the lambda modified items returned by that function for each item.

Example:

li
=
[
5
,
7
,
22
,
97
,
54
,
62
,
77
,
23
,
73
,
61
]
final_list
=
list
(
map
(
lambda
x: x
*
2
, li))
print
(final_list)

Output:

Use of lambda() with reduce()

The reduce() function in Python takes in a function and a list as argument.

from
functools
import
reduce
li
=
[
5
,
8
,
10
,
20
,
50
,
100
]
sum
=
reduce
((
lambda
x, y: x
+
y), li)
print
(
sum
)

Output:

Use of lambda() with filter()

The filter() function in Python takes in a function and a list as arguments.  its a way to filter out all the elements of a sequence “sequence”, for which the function returns True.

Example:

li
=
[
5
,
7
,
22
,
97
,
54
,
62
,
77
,
23
,
73
,
61
]
final_list
=
list
(
filter
(
lambda
x: (x
%
2
!
=
0
) , li))
print
(final_list)

Output:

[5, 7, 97, 77, 23, 73, 61]

Note: function,creation of it ,types of argument,scope, variable,local and global variable,lambda function, lambda function with map() function, lambda function with reduce() function, lambda function with filter() function, docs-string,parameter, function call, function argument, default argument, parameter argument, variable length arguments,advantages of function and advantages of lambda function,function call, etc.

This is all we have covered in this tutorial ,for more concepts of python you can refer our all articles , with various new topics and program of codes.

http://www.geeksforgeeks.org

%d bloggers like this: