# Functions¶

## What is Function?¶

Think of a situation where you do a particualar job over and over again. Now what if you could teach someone else to do the job and whenever you need that particular task done you can call him. Taking this situation into programming we get the concept of functions. A function is a block of reusable code that can be used to perform a particular function.

## Basics of Functions¶

### Defining a Function¶

Defining a function in python requires the use of the keyword **def** followed by the function name and arguments.

**Syntax:**

```
def function_name(argument1,argument2):
# Code for the function
return <expression>
```

```
function_name(arg1,arg2)
```

### Sample Function¶

Let's define a function to print the sum of two numbers.

```
def sum(a,b):
c=a+b
print "Sum is",c
```

```
sum(5,10)
sum(4,8)
```

```
Sum is 15
Sum is 12
```

`Sum is 15`

. And in the second function call a takes 4 and b takes 8 and output is `Sum is 12`

.
### Return Statement¶

The **return** statement can be used exit a function.It can also be used to send a value back from the function.

Syntax is :-

```
return <expression>
```

**n**natural numbers :-

```
def sum(n):
s = 0
while(n >= 0):
s += n
n -= 1
return s
```

```
su = sum(10)
print "sum is",su
```

```
sum is 55
```

**s**. The value in s is returned by the function and stored in a variable

**su**and thus we get the output mentioned above.

## More on functions¶

### Global and local variables¶

The variables declared within a function is a local variable of that particular function. And the variables outside a function is called global variable. The value of the variable depends on the scope of the variable.

Consider the following code:-

```
def func(a,b):
out = a*b
print "Output is",out
out = 50
func(6,7)
print "Output is",out
```

```
Output is 42
Output is 50
```

**out**is given the value 50. When the

`func()`

is called, the local **out**will have a value 42 since preference is given to local variables and we get

`Output is 42`

. After the function is called, the global **out**comes into scope. So for the second print

**out**will have the value 50 and thus we get

`Output is 50`

.
### Default aruments¶

You can set a default value for a particular argument of a function. So when you dont pass a certain argument to the function the function will assume the default value for that argument.

Consider the following code:-

```
def greet(name,grt = "Hi"):
print grt,name
greet("Robert")
greet("Joe","Hello")
```

```
Hi Robert
Hello Joe
```

`greet()`

, only one argument is given so grt assumes its default value `Hi`

and we get the output `Hi Robert`

. On the second call two arguments are passed to the funtion and so the `Hello`

will overwrite the defalt value of **grt**and thus we will get the output

`Hello Joe`

.
### Recursive function¶

Recursion is a tricky concept. It can be used when a certain problem can be divided into smaller sections of the same problem.Just consider calculating the factorial of a number. `6!`

is `6*5*4*3*2*1`

which can be written as `6x5!`

, similarly `5!`

can be written as `5x4!`

and so on. So as you can see calculating factorial of 5 is divided into smaller and similar task.

Now with this idea lets write a recursive function which returns the factorial of a number:-

```
def factorial(n):
if(n==0):
return 1
return n*factorial(n-1)
```

```
f = factorial(6)
print "6! is",f
```

```
6! is 720
```

`n==0`

is called ext condition. We need the recursive function call to stop at when this condtion is satisfied. And we returned **1**in that case because

`0! = 1`

. The statement `n*factorial(n-1)`

will execute the recursion process.In the above mentioned case, for

`6!`

we will get `6*factorial(5)`

, and for `5!`

we will get `5*factorial(4)`

and so on till **n**is 0, In that case that particular call will return 1 to its previous call, and that that function call will return whatever value it will get to its previous function call and after all the returning back to the previous funtion call it will return the desired value.

## Imported Functions¶

Till now we spend time defining functions to do a particular task. Now we can use pre-defined functions to do a particular task. All we have to do is import the library which contains that particular function.

The syntax to import a function from a module is :-

```
from <module> import <function>
```

`sqrt()`

from the math library
```
from math import sqrt
```

```
print sqrt(9)
print sqrt(5)
```

```
3
2.2360679775
```