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:30pi = 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:57None 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!