00:00:00
what's up guys i'm punit from
00:00:01
programmers and welcome back to this
00:00:03
series on python
00:00:04
in the last few videos we learned about
00:00:07
decision making and loops
00:00:09
we will start a new section today we
00:00:11
have been using the term
00:00:12
function a few times in our videos
00:00:14
without explaining what they are
00:00:16
in this video we will look into
00:00:17
functions in detail and learn how to
00:00:19
create them
00:00:20
and why they are used so let's get
00:00:22
started
00:00:29
in programming a function is a group of
00:00:31
related statements
00:00:33
that performs a specific task they help
00:00:35
us divide a large program
00:00:37
into smaller chunks so that it's easier
00:00:39
to understand
00:00:40
and change suppose you need to create a
00:00:43
program
00:00:43
that draws circles and rectangles of
00:00:46
different colors
00:00:47
based on user input instead of creating
00:00:49
one big chunk of statements to create
00:00:51
this program
00:00:52
we can create three functions to solve
00:00:54
this first
00:00:55
create a circle function then a
00:00:58
rectangle function
00:00:59
and then the color the shape function
00:01:02
this helps us to divide complexity
00:01:04
and we can focus on only a small part of
00:01:07
the problem at one time
00:01:09
now let's see how we can create a
00:01:10
function in python
00:01:12
to create a function we use the def
00:01:14
keyword which stands for the function
00:01:16
definition
00:01:17
followed by the function name then i'll
00:01:20
use the empty parenthesis
00:01:21
followed by a column so i'll say def def
00:01:25
greet empty parenthesis and column
00:01:29
here we have defined a function name
00:01:31
greet however
00:01:33
this code will give us an error because
00:01:35
the function body is missing
00:01:37
let's fix that for now i'll only add two
00:01:40
print statements as its body
00:01:42
so i'll say print hello
00:01:45
and then let me add another print
00:01:47
statement print
00:01:49
how do you do remember we need to use
00:01:53
indentation to specify that this is the
00:01:55
body of the function
00:01:57
we have successfully created a function
00:01:59
named greet now let me run this code
00:02:05
and we don't really see anything this is
00:02:07
because
00:02:08
defining a function won't do anything in
00:02:10
itself to bring the function into action
00:02:13
we need to call it our function name is
00:02:15
greet
00:02:16
with empty parenthesis so to call it i
00:02:19
will just use greet with empty
00:02:20
parentheses
00:02:21
so here i'll say greet
00:02:26
and now when i press run then you can
00:02:28
see that hello
00:02:29
and how do we do are printed here's how
00:02:32
this code works
00:02:33
when we call the function the control of
00:02:36
the program jumps to the function header
00:02:39
then the statements inside the function
00:02:41
are executed
00:02:43
when this task is completed the control
00:02:46
of the program
00:02:46
goes back to the function call and the
00:02:49
next statement
00:02:50
after the function call is executed one
00:02:53
thing with functions
00:02:54
is that once we define a function we can
00:02:56
call it any number of times
00:02:58
let's call our greet function three
00:03:00
times so here i'll say greet
00:03:03
and one more greet and now if i press
00:03:06
run
00:03:08
as you can see the function has run
00:03:09
three times that's why these two
00:03:11
statements were executed
00:03:13
one two and three times
00:03:18
so if we need to perform a task again
00:03:21
and again we can wrap the codes inside a
00:03:23
function
00:03:24
and use the function any number of times
00:03:26
we just need to call the function again
00:03:28
and again
00:03:29
one thing to remember when we create a
00:03:31
function is that we need to define a
00:03:33
function
00:03:34
first before we can call it this code
00:03:36
for instance will not work
00:03:38
let me run this code to show you
00:03:41
here when the grade function is called
00:03:44
python doesn't know that this function
00:03:46
exists
00:03:47
because it's defined after the function
00:03:48
call so always remember to
00:03:50
define a function before you use it
00:03:54
before moving to the next section of the
00:03:55
video i'd like to mention that the
00:03:57
program is team has created an app that
00:03:59
allows you to learn python from your
00:04:00
phone
00:04:01
the app contains bit size lessons that
00:04:03
are easy to understand
00:04:04
a built-in interpreter so that you can
00:04:06
run python on your phone
00:04:08
quizzes and many more features the app
00:04:10
is available on both ios and android the
00:04:12
links are in the video description below
00:04:16
now let's talk about python arguments
00:04:18
and how our greet function from the
00:04:19
previous example
00:04:21
can be changed to allow arguments
00:04:23
suppose we want to make our greet
00:04:25
function a bit more personal
00:04:26
so instead of printing hello we want to
00:04:28
print something like
00:04:29
hello jack or whatever the person's name
00:04:32
is
00:04:32
in such cases we can pass values to a
00:04:35
function
00:04:36
these values we pass to a function are
00:04:38
called arguments
00:04:39
so i'll go back to the code that i
00:04:41
started with i'll remove these two greet
00:04:43
functions because i want only one
00:04:45
and here inside the grid function i'll
00:04:48
pass the jack string like this
00:04:51
this value that we passed during a
00:04:53
function call is called an
00:04:54
argument so jack here is an argument
00:04:57
and in the function definition i'll add
00:04:59
a variable in the greet function
00:05:01
i'll call it name
00:05:05
this name variable accepts whatever
00:05:07
value is sent as an argument during the
00:05:09
function
00:05:10
call in this case jack is transferred to
00:05:13
the variable name
00:05:15
now i can use the name parameter inside
00:05:17
the function
00:05:18
so now i can say hello comma
00:05:22
name and when i press run then i get
00:05:25
hello
00:05:25
jack let's see step by step how this
00:05:28
function works
00:05:30
when we call the greet function with the
00:05:31
argument jack
00:05:33
then this is passed to the name variable
00:05:35
inside the function definition then
00:05:38
the statements inside the function are
00:05:40
executed we can use the name parameter
00:05:43
inside the body of the function
00:05:46
when this task is completed the control
00:05:48
of the program comes
00:05:49
back to the place from where the
00:05:51
function was called
00:05:53
and the next statement is executed in
00:05:55
this case there's nothing here
00:05:58
it's also possible to pass multiple
00:06:00
arguments to a function
00:06:01
as per our needs if we need to pass
00:06:04
multiple arguments to a function
00:06:05
we can separate them by commas let's see
00:06:08
this in action by creating a function to
00:06:10
add two numbers
00:06:11
so i'll remove the old code and i'll
00:06:13
create a new function called add numbers
00:06:16
so i'll say def
00:06:18
add underscore numbers obviously i'll
00:06:20
need two parameters
00:06:22
n1 and n2 and then inside the function
00:06:25
i'll say result
00:06:27
equals n1 plus n2
00:06:31
print the sum
00:06:34
is and here i'll say result
00:06:39
now outside the function i'll say number
00:06:43
1
00:06:44
equals 5.4 number two
00:06:47
equals six point seven and then i can
00:06:51
call
00:06:51
add underscore numbers number one
00:06:55
comma number two in this program
00:06:58
we have passed number one and number two
00:07:01
as arguments to the add numbers function
00:07:04
these arguments will be accepted as n1
00:07:06
and n2
00:07:08
once they are passed to the add numbers
00:07:09
function so inside the function
00:07:12
n1 will be 5.4 and n2 will be 6.7
00:07:16
then we have added the numbers and
00:07:18
printed them inside the function itself
00:07:22
let's run this code and see the output
00:07:24
as you can see
00:07:25
i now have the sum of those two numbers
00:07:31
in our program to add two numbers we are
00:07:34
finding the sum of numbers
00:07:35
and printing it it's working fine
00:07:37
however
00:07:38
it's better just to find the sum inside
00:07:40
the function
00:07:41
and print the result somewhere else we
00:07:43
can achieve that by using the return
00:07:46
statement
00:07:47
inside the function i'll remove this
00:07:48
print statement
00:07:50
and i'll say return
00:07:54
result when we return a value
00:07:58
it comes back to the function and we can
00:08:00
assign this return value to a variable
00:08:02
like this
00:08:03
so here i can say result equals
00:08:07
add numbers number one comma number two
00:08:10
and i can print the result as
00:08:11
print the sum is
00:08:15
and then i can say result let's see step
00:08:18
by step how this program works
00:08:21
we call the add numbers function with
00:08:23
two arguments
00:08:24
number one and number two which are
00:08:26
accepted by the function definition
00:08:29
as n1 and n2 the sum of n1
00:08:32
and n2 is calculated and the result
00:08:35
is returned to the function call this
00:08:38
return value
00:08:39
is assigned to the result variable
00:08:42
and in the next line we just print the
00:08:44
result variable
00:08:45
outside the add numbers function
00:08:48
whenever a return statement is
00:08:50
encountered inside a function
00:08:51
the control of the program goes back to
00:08:53
the place from where it's called
00:08:55
let's see an example of this let's get
00:08:57
back to our grid function that we wrote
00:08:59
before
00:08:59
i'm pasting it here
00:09:03
when i press run then we get hello jack
00:09:07
and how do you do here now let me modify
00:09:09
this program a little bit
00:09:10
i'll add a return statement after this
00:09:13
line and let's see what happens
00:09:15
now when i press run then you can see
00:09:18
that only hello jack is printed
00:09:20
this is because when the return
00:09:22
statement is encountered
00:09:24
immediately the function terminates and
00:09:26
control of the program goes back to the
00:09:28
place from where the function is called
00:09:31
at this point we have covered all the
00:09:33
basics to create a function
00:09:35
these functions we created ourselves are
00:09:38
known as user defined functions
00:09:40
actually we have already used functions
00:09:42
numerous times
00:09:44
in all of our videos remember print it
00:09:46
is also a function
00:09:48
the function definition of this print
00:09:49
statement is defined somewhere inside
00:09:52
the python programming language
00:09:54
that's why we can just call the function
00:09:56
and it just works
00:09:57
these functions defined inside of python
00:09:59
are called built-in functions
00:10:01
we have also used other built-in
00:10:03
functions like float
00:10:05
int input and so on in our videos
00:10:08
there are numerous built-in functions
00:10:09
available in python that make our life a
00:10:12
whole lot easier
00:10:13
for example suppose we have a list like
00:10:16
this
00:10:18
now if we need to find the length of
00:10:19
this list we can use the len function
00:10:23
so here i can say length
00:10:27
equals len max
00:10:30
and i can print the length of the list
00:10:32
as print
00:10:34
length is comma
00:10:37
length let's run this code and see the
00:10:40
output when i press run
00:10:42
then as you can see it says length is 5
00:10:45
which is the length of the number of
00:10:46
items in this
00:10:48
marks list if we instead needed to find
00:10:51
the sum of the items of the list
00:10:53
we don't need to manually write the code
00:10:55
ourselves we can use the sum function
00:10:57
that's available in python
00:10:59
so here i can say marks underscore sum
00:11:02
equals sum of marks
00:11:07
and then i can print the sum as print
00:11:09
the
00:11:10
total marks you
00:11:14
got is and then i can say
00:11:17
marks underscore sum now when i press
00:11:20
run
00:11:20
then it says the length is 5 and the
00:11:22
total marks you got is 308
00:11:25
which is the sum of these marks if
00:11:28
you're interested
00:11:29
you can find all the built-in functions
00:11:30
available in python in the
00:11:32
programming.com website
00:11:34
the link will be in the description
00:11:35
below let's put all the things
00:11:37
we have learnt in this video in action
00:11:40
suppose you have just attended a
00:11:42
university examination
00:11:43
the marks you obtained in various
00:11:45
subjects are stored in a list like this
00:11:48
you want to find the average marks you
00:11:50
obtained in the exam
00:11:51
and based on the average marks you want
00:11:53
to find your grade
00:11:55
the grading rule is like this you will
00:11:57
get grade a
00:11:58
if the average marks is equal to or
00:12:00
above 80
00:12:01
you will get grade b if the average max
00:12:04
is equal to or above 60
00:12:06
and less than 80 you will get grade c if
00:12:08
the average max is equal to or above
00:12:11
50 and less than 60 and if the average
00:12:13
marks is less than 50
00:12:15
you will get great f to solve this
00:12:17
problem
00:12:18
we will create two functions one to find
00:12:21
the average marks
00:12:22
and another to compute the grade so
00:12:24
let's get started
00:12:25
i'll start with the function definition
00:12:27
but before that let me add a little
00:12:29
comment i'll say
00:12:30
function to find average marks
00:12:34
now let me define my function as def
00:12:37
fine average marks now the argument to
00:12:41
this
00:12:42
will be a list of marks and inside i'll
00:12:45
say
00:12:46
sum of marks equals
00:12:49
some marks i also need to find the
00:12:51
number of subjects or the number of
00:12:53
marks i have so i'll say total
00:12:56
underscore subjects
00:12:59
equals the length of this marks list
00:13:03
and then now i can calculate the average
00:13:05
as average
00:13:06
underscore marks equals sum of
00:13:09
marks divided by total subjects
00:13:13
let me return this average marks
00:13:15
variable
00:13:17
and now outside the function i can say
00:13:20
average
00:13:21
marks equals find average
00:13:25
marks i'll pass in the marks list and
00:13:27
here i'll say
00:13:29
print your average marks
00:13:32
is and i'll say average underscore marks
00:13:36
when i press the run button i can see
00:13:38
that my average marks is 67.8
00:13:41
which is the average of these five
00:13:43
numbers which is calculated as sum of
00:13:45
these numbers
00:13:46
divided by the total number of subjects
00:13:48
which is 5 in this case
00:13:51
now we need to create another function
00:13:54
to calculate the grade based on the
00:13:55
average marks
00:13:56
let's do that now i'll create another
00:13:59
function but before that let me add a
00:14:00
little comment
00:14:01
i'll say calculate the grade and
00:14:04
return it and then i'll say def
00:14:08
compute underscore grade now the
00:14:11
parameter or the argument to this will
00:14:13
be the average marks
00:14:14
so i'll say average marks and inside
00:14:17
i'll say
00:14:18
if average marks greater than
00:14:22
equals 80 then grade
00:14:25
is a alif
00:14:29
average marks greater
00:14:32
equals 60
00:14:35
then the grade is b
00:14:39
alif average max
00:14:42
greater than equals 50 in this case
00:14:45
the grade is c and the else clause
00:14:49
i'll say grade equals f
00:14:52
and outside the if block i'll say return
00:14:56
grade now i can use this compute grade
00:14:59
function as
00:15:01
grade equals compute
00:15:05
underscore grade average
00:15:08
marks now let me print
00:15:12
this grade as your grade is
00:15:16
and the grade variable now when i press
00:15:19
it on
00:15:20
i can see that my average marks is 67.8
00:15:23
and my grade is b which is not the best
00:15:25
result but at least i now know what my
00:15:28
grade is
00:15:30
before we end this video here's a
00:15:32
programming task for you can you create
00:15:34
a program to add and multiply two
00:15:36
numbers
00:15:37
for this create two functions add
00:15:39
underscore numbers
00:15:40
and multiply underscore numbers these
00:15:42
functions should compute the result and
00:15:45
return them to the function call
00:15:47
and the results should be printed from
00:15:49
outside the function
00:15:50
you'll find the answer to this question
00:15:52
in our github repository
00:15:54
also visit our website programmies.com
00:15:56
for more tutorials and examples
00:15:58
the links will be in the description
00:16:00
below now let's recap what we learned
00:16:02
a function is a block of code that
00:16:04
performs a specific task
00:16:07
we use the def keyword to define a
00:16:09
function
00:16:10
to bring the function into action we
00:16:12
need to call the function
00:16:14
we can call the same function as many
00:16:16
times as we want as per our needs
00:16:19
we can pass values to a function these
00:16:21
values
00:16:22
passed to functions are called arguments
00:16:24
or parameters
00:16:26
the return statement can be used inside
00:16:28
a function the return statement returns
00:16:30
a value
00:16:31
from a function and exits the function
00:16:33
as well that's it for this video
00:16:35
i hope you learned something if you're
00:16:36
just watching the video without writing
00:16:38
any code
00:16:39
i highly encourage you to try the
00:16:40
programs in this video on your own
00:16:42
the only way you can be a good
00:16:43
programmer is by trying
00:16:45
by the way you can find all the programs
00:16:47
from this video on github
00:16:49
i've provided the link in the
00:16:50
description below feel free to copy the
00:16:52
program and edit them as you please
00:16:54
and if you have any questions and
00:16:56
feedback use the comment section below
00:16:58
in the next video we will learn about
00:17:00
different types of function arguments in
00:17:02
python
00:17:03
join me in this video series and let's
00:17:05
explore the exciting world of
00:17:06
programming together
00:17:07
if you like this video hit the like
00:17:09
button now and also don't forget to
00:17:11
subscribe to our channel
00:17:12
and ring that bell icon so that you
00:17:13
don't miss the next video thanks for
00:17:15
watching and i'll see you in the next
00:17:17
one
00:17:17
happy programming