Skip watching entire videos - get the full transcript, search for keywords, and copy with one click.
Share:
Video Transcript
Video Summary
Summary
Core Theme
This content introduces Python functions as reusable blocks of code that perform specific tasks, emphasizing their role in making code modular, readable, and efficient by avoiding repetition.
Mind Map
Click to expand
Click to explore the full interactive mind map • Zoom, pan, and navigate
today we are going to talk about
functions in Python so let's begin with
function introduction functions are
nothing but a block of code that
performs a specific task think about
them as being a dishwasher so the way
this works is you give dirty dishes as
an input to the dishwasher when you
press the button internally it will
perform some tasks such as adding
detergent and water cleaning the dishes
and then drying them out in the end what
you get is clean dishes as an output so
functions are also similar they take
something as an input then depending on
what kind of code you write within a
function it will perform some task and
in the end you get something else back
as an output now why functions are
needed functions makes your code more
modular and more readable let me explain
this by giving a real-life example so if
you have two lists and you want to
perform summation of these two
individual lists how do you do that
if you are not using functions I'm going
to initialize or these two lists and I'm
just going to call this two lists as
Tom's expense lists and Joe's expense
lists so these are the expense lists
from two different people and what we
are trying to do here is find the total
of each individual list okay so without
using the functions what you will do is
you will create a variable called total
and then you will write a for loop to go
or each of these items and in the end
your will print a total I am just
copying and pasting the code from a
different document just to save time on
recording so
up here what you just did is you went
over each of the items from Tom's
expense list and you added it to this
variable okay now to find the total of
Joe's expense list you will do the same
thing so I will just copy paste this
code and I will replace this guy with
Joe's expense list because the second
thing you are doing is printing the list
of total expense of Joe's list and you
will say these are total expenses
incurred by Joe okay let's run the
program excellent so when you ran it you
got the total expenses from for both the
people the problem with this code is
that you are repeating these three lines
of code at two places imagine if you had
100 such list you will have to repeat
these three lines 100 times and that's
very cumbersome we can encapsulate this
particular code into a function so now
let me write the same code using a
function so I am going to remove this
thing here we will still keep our list
and I will start writing a function so
the way you write the function is first
you type d e f DF is a special keyword
that tells python that i am going to
write a function so calculate total is
the function name and exp is input here
you will create local variable called
total and you will I trip through the
items in exp and by the way these lists
we are when we call this function we are
going to pass these two lists as an
input so exp variable will have a list
here you will say total equal to total
in the end you return a total okay the
way you call this function is you create
another variable called Tom's total and
you call this function with Tom's list
as an input then for juice total you
create one more variable called juice
total and your game call this function
calculate total and you pass the juice
expense list as an input now it's the
time to print the total
so Tom's total X and this is Tom's total
and I can just copy paste this code and
I can say juice total expenses is juice
total okay let me run it and see what
happens excellent I got the same result
okay now let me explain the way this
worked is when you call calculate total
here by passing this list as an input
what happens is this list gets past or
into exp exp is a local variable for
this function okay and this much is a
function body and the body of the
function is defined by this indentation
you see this extra tab here so whenever
that ends or whenever you return the
function body will end here you will
perform some operations you will create
a local variable you will write it to
the items and in the end you will return
so this thing total here is called a
return value and you return a value from
a function by using using this return
keyword okay so these are the basic
components to remember number one is a
function argument number two is a function
function
a return value and this whole thing is
called function okay so if you compare
with this again with a dishwasher
example just imagine this is a
dishwasher then this is an input to the
dishwasher which is your dirty dishes
and these three lines is the set of
steps that dishwasher is performing like
adding water and detergent washing this
is tying them out etc in the end this
return statement returns you the clean
dishes which is an output okay so that
was a quick comparison with a dishwasher
now let's work on another example and
this one is going to be very very simple
let's say you want to do sum of two
numbers you can do it using a function
so I can write a function let me erase
the score I can write one more function
call sum and a comma B so a and B are
the arguments to this function here you
create a local variable total equal to
zero and a rather total equal to a plus
B and you can just return it return
total okay and you can say is some PI
and six okay now tell me what will be
so when I run it as you all expect I'm
going to get 11 because 5 plus 6 is 11
what happened here is let me debug the
program and then I'll explain how it
worked I will put a breakpoint at do
these two places I will say debug okay
and I will say okay next so it first
define a function it we have not called
the function yet that's why it didn't go
here okay we are calling function here
so when you say go inside the function
let me see which one this one is tap
into so stab into next as you see here a
has value Phi and B has value 6 whatever
you pass from here this guy will get
those values and when you say next total
is 11 and now we are returning this
total so this total will now get placed
into this and variable next so as you
see M still doesn't have any value when
it's annexed at that time n gets 11 okay
if you check console we got total 11 now
the way we are passing function
arguments here is by order so if you
want to pass something in a you put that
value here as a first value and B you
put that as a second value let me print
those values here I will say print a is
a let me just copy paste couple of time
B is my B and I will also print total
before returning
so my total is dota okay let me turn it
so fair on it is five is six total is
eleven and total inside function inside
function this is total outside okay now
what if I want to pass B first and then
a a second so you can use something
called named arguments so if you say D
explicitly say B equal to five and equal
to six what's gonna happen is then it
will not go by order these are called
named arguments and the other ones we're
not they're not named arguments they
were basically using the order that's on
this voila
so a a six B is five if you have long
list of arguments and you want to be
specific about which argument has which
value you can use these named arguments
otherwise you can just use plain order
cool okay next item is global versus
local variables so until now all these
variables that we created here a B total
these are local to this function if you
try to access this variable here for
example let me print total so we have
defined total variable here all I'm
trying to do is print total but you see
this red line it says okay let me run it
it says name error name total is not
defined which means total is not visible
outside the function body total is only
visible inside the function body and my
function body is this much okay so what
happens if I have one more variable
called total outside if I have total
equal to zero and if I say total outside
the function is total okay let's see
what happens
total was zero here and here we assigned
a plus B value we are printing total
the function and then outside the
function let's run it great so total
inside the function was 11 but outside
it is zero so you see this variable and
this variable although their names are
different their names are same their
values are different because those
variables essentially are different this
one is called a global variable because
it is outside any function it can be
accessed anywhere
this one we created locally so it is a
local variable okay let's now talk about
default arguments I will just revert
this back to what it was originally here
we are passing two arguments to the
function what if I don't want to pass
the second argument if you don't want to
pass the second argument and if you run
the program it won't run because it will
say it is missing the argument okay but
sometimes you may have a default value
for the second argument so I'm making an
assumption in my code that if my
function caller doesn't pass me an
argument assume it to be zero so when
you say this inside reference function
argument when you assign a specific
argument of value that becomes a default
value for that argument
so now if you run it it will work fine
what this is saying is if you don't pass
me the second argument then I'm going to
assume it to be zero but hey you are
free to pass the second argument if you
pass this imagine what's gonna happen
it's not gonna make that zero let me run
it and prove it
excellent so you see here B is 8 and
this didn't have any effect so this will
have any effect only if you don't pass
the argument okay we want to conclude
our session by covering document strings
a real fast documentation strings are
nothing but a way to document your
function this function was very
simple it was just doing a sum of two
numbers but you might have a big
complicated function in that case you
want to write some documentation to
explain the color of that function on
what that function is doing what does it
expect as an input and what does it
expect as an output so the way you write
documentation string is you use triple
quotes like this and by default py charm
is giving these default documentation
but if you're not using py charm if
you're using either or something you
have to manually type all these things
in okay
so let me just remove these things and
manually type it in so this function
takes two arguments which are integer
numbers and it will return some of them
as an output so when you do this if
someone is calling a function if he
reads these lines he will he will get an
ID on what this function is doing so
this is called a documentation strings
and it is a multi-line strings started
and ended with triple quotes all right
we cover pretty much everything about
functions don't forget to work on these
sample exercises at the end of my every
video I give these sample exercises too
for you to work upon and these are
extremely useful and it will help you a
Click on any text or timestamp to jump to that moment in the video
Share:
Most transcripts ready in under 5 seconds
One-Click Copy125+ LanguagesSearch ContentJump to Timestamps
Paste YouTube URL
Enter any YouTube video link to get the full transcript
Transcript Extraction Form
Most transcripts ready in under 5 seconds
Get Our Chrome Extension
Get transcripts instantly without leaving YouTube. Install our Chrome extension for one-click access to any video's transcript directly on the watch page.