1
00:00:01 --> 00:00:04
Hello friends and welcome to the tutorial 'Getting started with functions'.
2
00:00:05 --> 00:00:15
At the end of this tutorial, you will be able to,
Define a function.
Define functions with arguments.
Learn about docstrings.
Learn about function return value.
Read code.
3
00:00:16 --> 00:00:21
Before beginning this tutorial,we would suggest you to complete the tutorial on "Conditionals" and "Loops".
4
00:00:22 --> 00:00:31
While writing code, we always want to reduce the number of lines of code, and functions is a way of reusing the code.
5
00:00:32 --> 00:00:34
Thus the same lines of code can be used as many times as needed.
6
00:00:35 --> 00:00:42
A function is a portion of code within a larger program that performs a specific task and is relatively independent of the remaining code.
7
00:00:43 --> 00:00:47
Now let us get more familiar with functions,
8
00:00:48 --> 00:00:52
Consider a mathematical function f of x = x square.
9
00:00:53 --> 00:00:57
Here x is a variable and with different values of x the value of function will change.
10
00:00:58 --> 00:01:04
When x is one, f(1) will return the value 1 and f(2) will return us the value 4.
11
00:01:05 --> 00:01:09
Let us now see how to define the function f of x in python.
12
00:01:10 --> 00:01:16
Start your ipython interpreter by typing,ipython in command line.
13
00:01:17 --> 00:01:18
Let us define our function f of x
14
00:01:19 --> 00:01:28
So type def f within bracket x colon return x star x
15
00:01:29 --> 00:01:33
Star denotes multiplication
16
00:01:34 --> 00:01:44
Well that defined the function, so before learning what we did let us see if it returns the expected values, try,
17
00:01:45 --> 00:01:51
f(1) f(2)
18
00:01:52 --> 00:01:54
Yes, it returned 1 and 4 respectively.
19
00:01:55 --> 00:01:57
And now let us see what we did.
20
00:01:58 --> 00:02:11
We wrote two lines: The first line def f of x is used to define the name and the parameters to the function, and the second line is used to fix what the function is supposed to return.
21
00:02:12 --> 00:02:18
def is a keyword and f is the name of the function and x the parameter of the function.
22
00:02:19 --> 00:02:23
Pause the video here, try out the following exercise and resume.
23
00:02:24 --> 00:02:30
Write a python function named cube which computes the cube of a given number n.
24
00:02:31 --> 00:02:32
Switch to your terminal for solution.
25
00:02:33 --> 00:02:35
The problem can be solved as,
26
00:02:36 --> 00:02:47
In the terminal type def cube within bracket n colon return n star star 3
27
00:02:48 --> 00:02:52
Let us check whether our function returns the cube of a number or not
28
00:02:53 --> 00:02:59
So type cube within bracket 2 and hit enter.
29
00:03:00 --> 00:03:04
It returned 8,which means we have defined our function,the right way.
30
00:03:05 --> 00:03:08
And now let us see how to write functions without arguments.
31
00:03:09 --> 00:03:14
let us define a new function called greet which will print Hello World .
32
00:03:15 --> 00:03:25
So type def greet() colon and hit enter.
33
00:03:26 --> 00:03:38
Then type print within double quotes Hello World exclamation
34
00:03:39 --> 00:03:44
now we call the function as,greet() and hit enter.
35
00:03:45 --> 00:03:48
Well that is a function which takes no arguments.
36
00:03:49 --> 00:03:52
Also note that it is not mandatory for a function to return values.
37
00:03:53 --> 00:03:56
The function greet neither takes any argument nor returns any value.
38
00:03:57 --> 00:04:02
Now let us see how to write functions with more than one argument.
39
00:04:03 --> 00:04:07
Pause the video here, try out the following exercise and resume the video.
40
00:04:08 --> 00:04:15
Write a python function named avg which computes the average of a and b .
41
00:04:16 --> 00:04:18
Switch to terminal for solution.
42
00:04:19 --> 00:04:35
Type def avg within bracket a comma b colon return within bracket a + b divided by 2
43
00:04:36 --> 00:04:41
For division we use slash.
44
00:04:42 --> 00:04:43
Let us test our function,
45
00:04:44 --> 00:04:52
Type on terminal avg within bracket 20 comma 30 and hit enter.
46
00:04:53 --> 00:04:55
We get the correct average, 25.
47
00:04:56 --> 00:05:05
Thus if we want a function to accept more arguments, we just list them separated with a comma between the parenthesis after the function's name in the def line.
48
00:05:06 --> 00:05:18
It is always a good practice to document the code that we write, and for a function we define, we should write an abstract of what the function does, and that is called a docstring.
49
00:05:19 --> 00:05:23
Let us modify the function avg and add docstring to it.
50
00:05:24 --> 00:05:37
Do the following,So now in the terminal type def avg within bracket a comma b colon
51
00:05:38 --> 00:05:49
Then in triple double quote you can type avg takes two numbers as input (a & b), and returns the average of a and b
52
00:05:50 --> 00:06:01
Then type return within bracket a+b and hit enter
53
00:06:02 --> 00:06:08
Note that we have a syntax error.
54
00:06:09 --> 00:06:14
The error that we saw here is because of indentation error in return(a+b)/2
55
00:06:15 --> 00:06:24
So just input that command again.
56
00:06:25 --> 00:06:44
So type def avg within bracket a comma b colon """ avg takes two numbers as input (a & b), and returns the average of a and b""" return within bracket a+b divided by 2
57
00:06:45 --> 00:06:54
Note that docstrings are entered in the immediate line after the function definition and put as a triple quoted string.
58
00:06:55 --> 00:06:59
And here as far as the code functionality is concerned, we didn't do anything.
59
00:07:00 --> 00:07:02
We just added an abstract of what the function does.
60
00:07:03 --> 00:07:06
Now try this in the ipython interpreter.
61
00:07:07 --> 00:07:11
Type avg and question mark.
62
00:07:12 --> 00:07:15
It displays the docstring as we gave it.
63
00:07:16 --> 00:07:20
Thus doc string is a good way of documenting the function we write.
64
00:07:21 --> 00:07:28
Now type f question mark and hit enter.
65
00:07:29 --> 00:07:36
It does have a doc string associated with it.
66
00:07:37 --> 00:07:39
Sorry It does not have a doc string associated with it.
67
00:07:40 --> 00:07:47
Also we cannot infer anything from the function name, and thus we are forced to read the code to understand about the function.
68
00:07:48 --> 00:07:53
Pause the video here, try out the following exercise and resume the video.
69
00:07:54 --> 00:07:58
Add doc string to the function f.
70
00:07:59 --> 00:08:05
We need to define the function again to add doc string to the function f and we do it as,
71
00:08:06 --> 00:08:12
def f within bracket x colon
72
00:08:13 --> 00:08:23
"""Accepts a number x as argument and, returns the square of the number x."""
73
00:08:24 --> 00:08:31
Then type return x star x
74
00:08:32 --> 00:08:33
Let us solve one more exercise
75
00:08:34 --> 00:08:40
Pause the video here, try out the exercise and resume the video.
76
00:08:41 --> 00:08:51
Write a python function named circle which returns the area and perimeter of a circle given radius r .
77
00:08:52 --> 00:08:56
Switch to the terminal for solution.
78
00:08:57 --> 00:09:02
The problem requires us to return two values instead of one which we were doing till now.
79
00:09:03 --> 00:09:04
We can solve the problem as,
80
00:09:05 --> 00:10:03
So now we can type in terminal def circle within bracket r colon """returns area and perimeter of a circle given radius r"""
pi = 3.14
area = pi star r star r
perimeter = 2 star pi star r
return area comma perimeter and hit enter.
81
00:10:04 --> 00:10:06
A python function can return any number of values.
82
00:10:07 --> 00:10:08
There is no restriction for it.
83
00:10:09 --> 00:10:11
Let us call the function circle as,
84
00:10:12 --> 00:10:38
Typing it on terminal a comma p = circle within bracket 6. print a, print p
85
00:10:39 --> 00:10:45
Now we have done enough coding, let us do some code reading exercise,
86
00:10:46 --> 00:10:53
Pause the video here and try to figure out what the function what does.
87
00:10:54 --> 00:10:57
def what within bracket n colon
88
00:10:58 --> 00:11:07
if n less than 0 colon n = -n while n greater than 0 colon
89
00:11:08 --> 00:11:11
if n modulo 2 == 1 colon
90
00:11:12 --> 00:11:13
return False
91
00:11:14 --> 00:11:18
n slash = 10
92
00:11:19 --> 00:11:22
And the next line is return True
93
00:11:23 --> 00:11:35
continue from paused state It will return true if n modulo 2 is not equal to 1 and will return false, otherwise.
94
00:11:36 --> 00:11:44
The function here returns True if all the digits of the number n are even, otherwise it returns False .
95
00:11:45 --> 00:11:50
Now one more code reading exercise,
96
00:11:51 --> 00:12:12
So it is given def even underscore digits within bracket n colon """returns True if all the digits in the number n are even, returns False if all the digits in the number n are not even"""
97
00:12:13 --> 00:12:23
Then next line if n less than 0 colon n = -n while n greater than 0 colon
98
00:12:24 --> 00:12:39
The next line if n modulo 2 == 1 colon return False n slash= 10 return True
99
00:12:40 --> 00:12:47
Pause here and figure out what the function what does.
100
00:12:48 --> 00:12:51
def what within bracket n colon
101
00:12:52 --> 00:12:58
i = 1 while i star i is less than n colon
102
00:12:59 --> 00:13:01
i += 1
103
00:13:02 --> 00:13:06
return i star i == n comma i
104
00:13:07 --> 00:13:10
continue the video The function returns two values.
105
00:13:11 --> 00:13:22
One it returns the result of the while statement whether true of false, and second it prints the value that ii` currently holds.
106
00:13:23 --> 00:13:36
Here, the function True returns and the square root of n if n is a perfect square, otherwise it returns False and the square root of the next perfect square.
107
00:13:37 --> 00:13:39
So we look at that
108
00:13:40 --> 00:14:13
def is underscore perfect underscore square within bracket n colon """returns True and square root of n, if n is a perfect square, otherwise returns False and the square root of the next perfect square""" i = 1 while i star i less than n colon i += 1 return i star i == n comma i
109
00:14:14 --> 00:14:16
This brings us to the end of this tutorial.
110
00:14:17 --> 00:14:21
In this tutorial, we have learnt to, Define functions in Python by using the keyword def .
111
00:14:22 --> 00:14:24
Call the function by specifying the function name.
112
00:14:25 --> 00:14:32
Assign a doc string to a function by putting it as a triple quoted string.
113
00:14:33 --> 00:14:36
Pass parameters to a function.
114
00:14:37 --> 00:14:38
Then Return values from a function.
115
00:14:39 --> 00:14:41
Here are some self assessment questions for you to solve
116
00:14:42 --> 00:14:45
What will the function do?
117
00:14:46 --> 00:14:47
def what(x)
118
00:14:48 --> 00:14:49
return x star x
119
00:14:50 --> 00:14:51
Returns the square of x
120
00:14:52 --> 00:14:53
Returns x
121
00:14:54 --> 00:14:56
Function doesn't have doc string
122
00:14:57 --> 00:14:58
Error
123
00:14:59 --> 00:15:06
How many arguments can be passed to a python function?
None
One
Two
Any
124
00:15:07 --> 00:15:11
Write a function which calculates the area of a rectangle.
125
00:15:12 --> 00:15:13
Now we look at the the answers,
126
00:15:14 --> 00:15:26
The function will result into an error due to the use of wrong syntax in defining the function.
127
00:15:27 --> 00:15:31
The function line should always end with a colon
128
00:15:32 --> 00:15:36
Any number of arguments can be passed to a python function.
129
00:15:37 --> 00:15:40
As we know, area of a rectangle is product of it's length and breadth.
130
00:15:41 --> 00:15:46
Hence, we define our function as, def area within bracket l comma b colon
131
00:15:47 --> 00:15:50
return l star b
132
00:15:51 --> 00:15:54
Hope you have enjoyed this tutorial and found it useful.
133
00:15:55 --> 00:16:00
Thank you!