Python Functions | Complete Python Tutorial for Beginners #1
Learn how to write your own functions in Python to break down your code into smaller, maintainable chunks for better organization and reusability. Follow step-by-step guide on defining custom functions with best practices on naming and structuring. Dive into the world of Python functions starting with the essential DEF keyword and indentation. Perfect for absolute beginners in Python programming.
Video Summary & Chapters
No chapters for this video generated yet.
Video Transcript
so far you have learned how to use some
of the built-in functions in Python such
as print round and so on in this section
you're gonna learn how to write your own
functions now you might ask but why do
you even need to write our own functions
well when you build a real program that
program is going to consist hundreds or
thousands of lines of code you shouldn't
write all that code in one file like we
have done so far you should break that
code into smaller more maintainable and
potentially more reusable chunks we
refer to these chunks as functions so
let me show you how to create your own
custom functions we start with the DEF
keyword which is short for define next
we need to give our function and name so
let's call this greet all the best
practices you'll learn about naming your
variables also apply to naming your
functions so make sure your function
names are meaningful descriptive use
lowercase letters to name your functions
and an underscore to separate multiple
words now after the name we need to add
parentheses you will see why shortly and
then we'll add a colon now what is going
to happen you know it you're gonna get
indentation which means the following
statements will belong to this function
so here I'm gonna add two statements hi
there and welcome aboard both designs
belong to this function because they're
indented now we're done with this
function we need to call it so we remove
the indentation and we add two line
breaks after this function this is what
Papp 8 recommends to keep our code clean
and maintainable now if you forget to
add two line breaks don't worry as soon
as you save the changes auto-pay of
eight will automatically add these line
breaks for you let me show you so I'm
gonna remove these line breaks and call
this function create with parentheses
just like how we call the built-in
functions now save the changes there you
go so we get
two line breaks after our function now
let's run this program so we get these
two messages on a terminal
now here is a question for you what is
the difference between the greed and
trent functions the difference is that
this print function takes an input
whereas our greet function doesn't take
any inputs so let me show you how to
pass inputs like first name and last
names of this function when defining a
function in between parentheses will
list our parameters so here we add two
parameters like first under like name
and last underline name now when calling
this function we need to supply two
values for those parameters we refer to
them as arguments so maash Hamadan e
these are the arguments to the greet
function that's one of the terms that a
lot of developers out there don't know
they don't know the difference between
parameters and arguments a parameter is
the input that you define for your
function whereas an argument is the
actual value for a given parameter okay
now let's change line two and instead of
saying hi there we can greet a person by
their full name so we can convert this
to a formatted string and has two fields
here first name as well as last name
save the changes run the program and
this is what we get in terminal now this
function is more useful we can reuse it
and call it with different arguments
so let's greet John Smith as well run
the program so we get hi Muhammad on e
and hi John Smith now note that by
default all the parameters that you
define for a function are required so
here our greet function takes two
parameters if I exclude one of these
arguments and save the changes you can
see we have this red underline so pilant
is complaining and saying there is no
value for argument last name also if we
run the program we get this type error
greet missing one required positional
argument
so let's put this back now later I will
show you how to define optional