How to Create Functions in Python! Beginner Lesson

LeMaster Tech
182 followers
2 Video Views·Apr 22, 2025
This video covers how to group common code together inside of functions in the python programming language!
Functions are the best way to rapidly reuse portions of code, and python has fairly simple syntax for creating functions as well as calling functions inside your program!
This is a great beginner tutorial and if you're looking to use beginner python skills in creating applications, games and fully functionality utilities be sure to check out the rest of the channel!
https://www.youtube.com/channel/UCV5Ab39YnXvTZ6Grar9URxQ
Let us know with any questions, comments, concerns or ideas for a future video!
Thanks for watching and be sure to like and subscribe for more great content!
what's going on you guys welcome back to
the channel and today's video is going
to be taking a look at how to define and
call functions in python
[Music]
so the basic idea of a function in
python is that you are grouping a
certain amount of code that you may want
to reuse over time with different
variables leading to a different result
and so whereas in python say you just
had two numbers and you wanted to add
them together it you're allowed to say
like sum
equals
one plus two okay and if you run this
code it's going to store the value of
three
in the sum and you can test that by
using print and saying well just print
out the sum
okay and so if i run this you can see i
get three and that's great
a lot of you who are familiar with
variables even know that you could make
two variables like number one equals
five
and number two
oops number two equals ten and then you
could set sum equal to
number one
and plus
number two
um
so
there we go and then if i print some now
you'll see it's 15. i can update the
value of sum just by updating number one
and number two in these variables
but what you've done here is is you have
technically written a function but you
haven't grouped it in any sort of way
where it's can be
called from the outside world and so
we're going to take a look at how to do
that using a few different examples in
python in this video
and one of them will actually we'll
start with some so let's go ahead and
just take a look at how you create a
function first thing is you do d e f
which is define and essentially that's
python shorthand for define a function
and we'll call it sum so we'll say
define sum
and then you always put these
parentheses when you're creating a
function
and what goes inside those parentheses
is what variables are getting passed in
so there are a couple ways to write
almost every function a lot of the time
you can create some of the code inside
of it without needing to pass any
variables in we'll take a look at two
different ways to do this sum so let's
say just like we've had it written here
let's say you're passing in two numbers
so we're gonna pass two numbers in
and then you put a colon and
tabbing is very important in python so
if you're unfamiliar with
object-oriented programming or python
specifically
you want to make sure that your one tab
level in side of any function inside any
if or while loop or anything like that
that's just general python knowledge
but then what you do to get data back
from python inside of a function is you
use the return
and so in the case of a sum function
we're going to do number one
plus number two
and that's basically saying what we want
to return is the sum of these two
numbers
you could also use an intermediate
variable if you wanted to you could say
you know sum equals just like we did on
the outside
and sum equals this number one plus
number two
and then you could return the sum uh
this maybe is a little bit cleaner to
look at it may be um
for you
as a beginner programmer maybe this just
helps you order your brain a little bit
better
um but i'm gonna go ahead and put it
back the other way
because this
is um just the shortest fastest way to
return the sum of two numbers
now of course you wouldn't probably
write a function uh just for adding two
numbers together you would add some more
levels to it which we'll take a look a
little bit later in the uh a little bit
later in the video and how to kind of
create some more
useful examples with these functions but
okay so now
the thing that um really people
i think want to know is how do you call
a function once you've created it so
we've created this sum function that's
great but it it hasn't really um
it hasn't really
done anything you'll see if i
if i run this it actually won't give me
anything because just writing the
function doesn't do anything it's not
being called so what you have to do to
call a function
is you have to type in the name of the
function and then the parentheses and
then you put some variables inside of it
so let's just start with 2 and 3 as our
two numbers
and
we'll see it runs but we're not printing
anything because
there's no display inside of the
function so if we print
the sum
then now when it returns the number one
plus number two that's what's going to
be executed here and then we're going to
print that onto the console so there you
see we run it we get five
and that's pretty cool this can be
coupled essentially like let's say we
want to write this entire program just
for the purpose of summing two numbers
you could
you could say
num1 equals integer
input and if you're completely confused
by this input
print formatting i suggest you go back
to the learn python for beginners
tutorial series that's on this channel
and it'll go from everything from
downloading and installing python to how
to get user input how to use type
conversions and things that you're
seeing in this video
so i'm trying to go kind of slow and
explain everything as i do it but there
are concepts that you can get elsewhere
that will help you understand what's
going on here
but essentially if we said
num
integer input enter first number
and what we're doing right now is we're
prompting the user for
those numbers and so this way you can
see every time we run this function
we are going to have the option
as the user of
actually generating a different sum
based on our input
so what i just did is i set up two
inputs that are asking for the first and
second number i'm storing those in these
variables num1 and num2
we pass that into the function and you
can see i intentionally gave them
different names here in the outside
world versus inside the function that is
typically the best practice because if i
were to use
num1 in here
it's even gonna recommend
hey this shadows the name from the
outside scope essentially like you named
it something in here it will work in
here if you want it to but it could get
really confusing if you look at your
program or your debugging and you have
num1 num2
in multiple locations so
i recommend using different names for
your variables when you create them in
the outside world versus inside of your
function
but let's go ahead and run this
so enter first number i'm gonna go with
five enter second number go with another
five and it prints a 10 out on the
screen
so right there that's just kind of a
crash course in how to call a function
now let's say we put this prompting
inside of the function okay because i
think i mentioned earlier you can create
a function that doesn't have any
variables
so let's say we put
when you call summation we also want to
gather the user info inside of that
function
then all we would do when we call this
function now is we just say sum and then
empty brackets
and now it should operate exactly like
the way we just ran it so you just saw
us run the function it asked you for two
numbers and then
it returned the sum
this is probably actually the better way
to create this program where if you were
to design a function and then hand it
off to someone else on your team and say
all you have to do is run the function
and we'll handle gathering user input
everything else it's it's a complete
solution just in its own this is
probably the better way to do it because
now you'll see when we run it
enter first number five enter second
number 12
and we get 17 back but the difference is
all someone that you gave this to would
have to do is run
you know some
and they wouldn't have to know that they
also have to add a few input lines of
code and so one more thing because i put
the function inside of print for that
purpose
of displaying the result
but typically if you want
to get the result of something back so
let's say we ran the sum function we
wanted to put a local variable that was
like
um i'll just call it local sum
okay
so
we could then use local sum
as any as anything um
we want and modify it and and increment
it and use it later on in our program as
much as we want so we could say local
sum plus five
uh for for whatever reason and let's go
ahead and just uh print that out so now
what we're gonna do
is when i run this program
it's going to come down here it's gonna
see oh i have to run the sum function
to put a number inside local sum but
then what we're actually going to
display on the screen
is that result plus 5 that we modified
um
so so this will be kind of an
interesting explanation of how functions
can get called but let's say right now
we're running the sum function so we did
12 plus 6 which is 18 but then we came
down and we
added 5 to it so another another way of
maybe explaining that is if we did a
local sum 2 that was also equal to the
sum function
and then what we're going to print out
is
local
sum 2 plus local sum 1. so this is kind
of
this may be kind of silly maybe what
would make sense is if we had done
subtraction or something there but okay
we did 12 plus 9 it stored that value in
the variable local sum and we're running
it again so let's do 90 plus
1. and what it just did is it added 21
which was the first local sum to 91 for
112 so that's just a way of showing how
you can create and call functions as
well as how to store the
return value from those functions inside
of a variable as well as how you can use
and manipulate that variable later on in
your program
so this was a basic python tutorial i
hope you found it useful
certainly a lot of the videos
on this channel do build on these
concepts and kind of expand it to more
cool things
one of the first videos i did using a
lot of
functions was creating a four function
calculator in python where we gather
what operation a user wants to do
whether it's division multiplication
subtraction
and
processes your inputs based on what
you've entered and that uses a lot of
calling functions and returning outputs
based on
those functions so
that would kind of be the logical next
step in your learning i recommend
checking that video out if you have more
interest
if you got confused about anything or
there's something specific you want to
see on the channel just let me know
about in the comments below i'll get
back to you as soon as i can or possibly
make a video on it
i hope you found this useful i really
appreciate a like to the video subscribe
to the channel if you did
it helps me out a ton with the youtube
algorithm
and as always good luck with your code
and thanks for watching thanks bye
