Hello and welcome to this video functions are an important part of programming in Python.
They help us keep our programs organized and makes it possible for us to reuse the code we write.
There is a principle in software development which all good programmers tried to follow.
It is called DRY principle D R Y which stands for don't repeat yourself.
In other words it means don't write the same code twice.
Now functions can help you avoid writing the same code twice.
So what are functions functions are basically a collection of computer code which can be executed anywhere
in your program and also use over and over again.
So they are reusable and these block of code always do something specific.
The beauty about functions is that they are reusable.
The execute or run when they are called and you usually call them by their name.
So when you create a function in order to activate it you need to run it or call it by the name you
have given it.
They can have parameters parameters are basically variables that you define inside a function parentheses
and also they can have arguments.
Arguments are the actual value that you give to the function when you call the function.
They can return data as a result.
So if you want the functions to return data they can also do that.
Functions are built in and they can also be cost Tom created.
Example of the built in functions we have use so far is print.
The print is a function which allows you to print stuff on the console.
You can also create your own custom functions which are called user defined functions.
Creating a function is fairly straightforward.
Before you create a function it's good to think of a descriptive name that you want your function to
have.
The naming convention for a function is the same as in naming convention for variables to create a function.
You start with the D If keyword which indicates you are about to create a function and then you give
the function a name followed by parentheses and then the call on the colon basically tells the Python
interpreter that you're about to start a new line of code.
So whatever code you write here is the block of code that gets executed when the function is activated.
When you define functions inside the parenthesis the parenthesis can be empty or you can passing what
is known as parameters.
Parameters are basically variables that you define inside the function.
When you activate your function you can also give it a value that matches the parameter you set.
That value is referred to as an argument.
So I've already got a directory.
I'm just gonna right click on the directory click on new and click on python file.
I'm going to call it functions and it will automatically append it with a dot P Why file extensions
are click Okay.
That was created the file called functions got P Y create a function you start with the keyword d e
f you do space and then you give the function a name.
I'm going to call my function sum and then you add the parent.
This is inside the parent theses.
You can add parameters or you can leave the parentheses empty.
So I'm going to add a couple of parameters which are variables I'm going to call this X I'll do a comma
and I'll call this Y.
Once you're done you add a call on the column.
Basically tells Python that you want to start a new line of code.
So here now is what is called the function body.
This is the code that will execute when the function is activated.
So I'm just going to say X plus why.
So that is it.
We have created a function.
Now the X and Y are known as parameters.
Okay.
So when this function activates it will print the value of x plus the value of y.
Once you have created your function the function does nothing until the function is activated.
Now the process of activating the function is called calling the function.
It also has other names you can call it execute in the function or invoking the function.
So any phrase you hear relating to call running execute in invoking they all mean the same thing.
And when you call it function you call it by its name and you add the parentheses.
Now if you have defined some parameters during the function definition then you need to parse seen some
arguments when you core the function those arguments are the values that you give for the parameters
you have set in the function definition.
So to activate or call this function that we've created we need to call it by his name and the name
is sum.
So say some and then we had the parentheses.
Now notice that we had two parameters defined inside the function.
We've got X and Y.
So now we need to supply the values for x and y those values are known as arguments.
So if I say for comma five and that basically cause the function parsing those two values for the para
methods I have set and then it will execute the function.
So let me save that and then play the file so I click on play and you can see here is return the value
of nine.
Okay.
So is added because here I've said print x plus y.
So when I call the function are posting two values for 4 x and 5 for y and then returned the value of
9.
So that's how you call a function.
Once you have defined the function it is good to know the difference between parameter and argument.
Sometimes this can confuse begin as a parameter is actually like a variable that you define inside the
functions parentheses while an argument is the actual value that you pass or give to the function when
you call the function.
So in our function x and y are the parameters while the number 4 and 5 are the actual arguments for
those parameters.
So that's it for this video.
Thanks for watching.
Bye for now.