Course Content

Functions And Docstrings

 “Functions in Python can be defined as lines of codes that are built to create a specific task and can be used again and again in a program when called.”

There are two types of functions in the Python language:

  • Built-in functions
  • User-defined functions

We have used a lot of built-in functions in our code till now, these functions include print(), or sum(), etc. So, we have a good idea about how to call a function. Built-in functions are already present in our python program, and we just have to call them whenever we need them to execute. Being familiar with built-in functions we will now look into User-defined function mostly in this tutorial.

We must know how to define a function in Python. In order to create one ourselves, we have to use the def keyword in order to define a function accompanied by the function's name with a pair of parentheses. The function of parenthesis is to send arguments or parameters to a function. In simple words, parameters can be defined as values that are sent in the parenthesis. For example, if a function is used to add two numbers then both the numbers will be passed as parameters in the parenthesis. After parenthesis, a colon is used to get in the body of the function. Some functions may return a value to the caller, so in order to get the value, a return statement is put at the end of the body of the function that gives the value that has been calculated by the function.

Calling a function is very simple, we just have to write the name of the function along with the closing parenthesis. If the function requires some arguments then we write those in the parenthesis, but if it does not return anything, then we leave them empty.

We have discussed a lot about what functions are and how to use them, now let us move to the advantages of using a function:

  • If we are working on a big project then we will prefer to make as many functions as possible, so every other member of our team could use that.
  • By using functions, we can avoid the repetition of code to an extent. As we have discussed in the previous tutorial i.e. Tutorial #22, more lines of code mean less efficiency. Also repeating the same code at different places will just make the code more crowded than required.
  • The reusability of code is ensured by using functions. We can even use a function inside another function or in any part of our code.
  • By making a function of code that we are going to use again and again, we can save a lot of time.


Docstring is a short form of documentation string. Its purpose is to give the programmer a brief knowledge about the functionality of the function. It must be the first string in a function, and it is also an optional string but always good to have it while working on programs having multiple functions. The syntax for writing a docstring is very simple as it is just a string written in between three double quotes placed three times (""" """) on either side of the string. But it has to be the first line of code in the function’s body. To call a docstring we write the name of the function followed by ._doc_.

Code file as described in the video

# a = 9
# b = 8
# c = sum((a, b)) # built in function

def function1(a, b):
    print("Hello you are in function 1", a+b)

def function2(a, b):
    """This is a function which will calculate average of two numbers
    this function doesnt work for three numbers"""
    average = (a+b)/2
    # print(average)
    return average

# v = function2(5, 7)
# print(v)


sahib007 1 year, 6 months ago
Harry Bhai Source code to change kro
ahmedraza 1 year ago
where is function source code
ahmedraza 1 year ago
where is function source code
ahmedraza 1 year ago
harry bahi source code 
saurav@123 11 months ago
bhai source code ko update karo
Vivek-Mishra 9 months, 3 weeks ago
Thanks, Harry Bhai for Awesome Tutorials |:::::::::::::::::::::::::::::::::::::| Full Notes and Source Code ::::::::::::::::::::::::::::::}
jit87 6 months, 3 weeks ago
Thank you Vivek :)
himanshu9 8 months, 3 weeks ago
I like it very much!!
shellstar 8 months, 2 weeks ago
yar video me dekh ke source code likh lo na kyu sir ko pareshan kar rahe ho


No downloadable resources for this video. If you think you need anything, please post it in the QnA!

Course Announcements

Any Course related announcements will be posted here