# Functions

A function is reusable code that can be called anywhere in your program. Functions improve readability of your code: it’s easier for someone to understand code using functions instead of long lists of instructions.

On top of that, functions can be reused or modified which also improve testability and extensibility.

Related Course:
Python Crash Course: Introduction to Python Programming

## Function definition

We use this syntax to define as function:

```def function(parameters): instructions return value```

The def keyword tells Python we have a piece of reusable code (A function). A program can have many functions.

## Practical Example

We can call the function using function(parameters).

```#!/usr/bin/python   def f(x): return(x*x)   print(f(3))```

Output:

`9`

The function has one parameter, x. The return value is the value the function returns. Not all functions have to return something.

## Parameters

We can pass multiple variables:

```#!/usr/bin/python   def f(x,y): print('You called f(x,y) with the value x = ' + str(x) + ' and y = ' + str(y)) print('x * y = ' + str(x*y))   f(3,2)```

Output:

```You called f(x,y) with the value x = 3 and y = 2 x * y = 6```

## 24 thoughts on “Functions”

1. - February 22, 2018
```def f(x,y): print('you called f(x,y) with the value x =' + str(x)+ ' and y = ' + str(y)) print('x*y = ' + str(x*y)) f(int(raw_input(" san engiz x")), int(raw_input("san engiz y")))```

thanck you !!!!

2. - February 10, 2016

Hi Frank,
please let me know why it is important to keep same name of list while passing to function. here is a simple function that receives list on call and then add. if i change the name of list “a” in method call then it gives me error. why it is not allowed to change. in java while passing arguments we can change name.

```def method(a): print(a+a)   method(a=[1,2,4])```
1. - February 10, 2016

In your example you give the name in the function call. Try calling it with method([1,2,3])

3. - December 24, 2015

Dear Frank,
i am trying this code and it is giving me error: method() missing 1 required positional argument: ‘a’
please guide me what i am doing wrong in this code.

```def method(self,a): return (self.a)   b=method(7) print("the value of a is", a)```
1. - December 26, 2015

Hi Sadia, you are mixing OOP with functional programming
Try this:

```def method(a): return a   b = method(7) print("the value of b is " + str(b))```
4. - September 9, 2015

Hi Frank,

When I run below program in https://repl.it/BG8w/2

```def f(x,y): print ('You called f(x,y) with the value x = ' + str(x) + ' and y = ' + str(y)) print ('x * y = ' + str(x*y)) print('local variable in function, z = ' + str(z))   z = 3 f(3,2) print('z outside function = ' + str(z))```

I do get this result: local variable in function, z = 3. z is accessible in f(x,y). Why?

1. - September 9, 2015

You can print the variable inside f(x,y), but if you try z=2 inside the function it throws an exception. The Python interpreter executes all code at the 0th level of indention, which is why you can print z. The value of z cannot be changed inside the function because it is not a global variable nor parameter.

5. - August 16, 2015

Please correct me if i wrong, but in this example:

```#!/usr/bin/python     def f(x,y): print 'You called f(x,y) with the value x = ' + str(x) + ' and y = ' + str(y) print 'x * y = ' + str(x*y) z = 4 # cannot reach z, so THIS WON'T WORK   z = 3 f(3,2)```

we declared global variable z and then trying to use it inside function. This variable is global and should be reached everywhere in the code, isn’t it? So, why cannot we change a value? and what i have to do if i want to use the same name inside the function, but for local variable? is it impossible?

1. - August 16, 2015

Hi Petr, It is global if we define ‘global z’ inside the function. If not, Python creates a local variable z and sets that to 4. printing z after f(3,2) will show z is still 3.

```#!/usr/bin/python   def f(x,y): print 'You called f(x,y) with the value x = ' + str(x) + ' and y = ' + str(y) print 'x * y = ' + str(x*y) z = 4 # cannot reach z, so THIS WON'T WORK print('local variable in function, z = ' + str(z))   z = 3 f(3,2) print('z outside function = ' + str(z))```

Thus we have two variables ‘z’ in this case, one outside the function and one inside. To use the global z, we must define ‘global z’ on top of the function. To use a global variable z (no locals):

```#!/usr/bin/python   def f(x,y): global z print 'You called f(x,y) with the value x = ' + str(x) + ' and y = ' + str(y) print 'x * y = ' + str(x*y) z = 4 # cannot reach z, so THIS WON'T WORK print('local variable in function, z = ' + str(z))   z = 3 f(3,2) print('z outside function = ' + str(z))```

To summarize, if we do not define ‘global z’, the global variable is out of the scope of the function. I hope you enjoy the site!

6. - August 10, 2015

Does pointers exist in Python?
I’m not sure if ‘pointers’ is the right word … I mean the direction of a variable in memory.
Can you use that as a parameter in order to modify the content of a variable as you can do in C programming?

1. - August 10, 2015

If you mean accessing the memory address directly, this is not a standard practice in Python. The language abstracts that for the programmer, in a similar way that C abstracts assembly or assembly abstracts machine language.

In Python 2.7 you can get the memory address, but this does not work with Jython, PyPy and Iron Python. It’s a little tricky but here goes:

```#!/usr/bin/env python   x = 3 print hex(id(x))```

Python functions can return multiple return values, unlike C or C++, which you can then use to set your output variables. Variables (or objects) can be passed as parameters to a function.