Hello.
Welcome back.
So in this lesson we going to see how to create functions and in Python.
So this was our last project.
Let's create a new project or a new script from our click file and come to New file then our press control
is to save and I'll save this is number six under school functions like this.
Once that is done we can close this now.
So as we saw earlier Python doesn't use the curly braces we find in our programming languages like C
actually Python uses blocks and it's able to use blocks through indentation and its pacing is the same
with functions functions over here on surrounded by Kelly braces to indicate a block but rather indented
in a particular way.
So let's start with the first example.
I'm going to put a comment up here.
I'll call this example one example one and let's give it a name.
This is going to be a simple function to create a function.
We start with the keyword the F death and then space and then we type the function name so we can call
this simple on the school function like this and we open and close the braces.
Normal stuff here.
So the function has no argument.
So we open and close just like this.
If we were to write this function in C we might put a void here or something.
But in Python no argument to just open and close.
Once that is done we put a call on here and then we hit enter when we hit enter it to automatically
indent for us.
So once this comes once we've defined the function.
Now we can put the contents of the function what we want is function to do we want this function to
print a simple statement we can say print.
And over here we can say this is a simple function and we can press control as to save and then click
right here to run and see what we have
so nothing is happening.
It's because we've not code to function.
So it's quite a function we can just bring the function name here.
Simple on the school function like this
just like in any other language.
This the way we call functions once that's done we hit enter and function as run.
This is a simple function.
If we had to add more content to the function we have to follow this indentation line this line we have
to go vertically like this.
But if we want to come out of the function we just come out like this would come out of the indentation
as well so we can come out and say simple functions such that when we run it would automatically call
the function like this and then we press control to save the click over her and a click here to run
the module and I can see this is a simple function it's called the function automatically.
Right.
So we can clean this too let's go to example number two let's see a function that takes arguments to
just bring this here I'll put a comment to your example number two and I'll call this function arguments.
So we do the same way.
We start by defining it de F and we can give this function a name such as a school in this country do
for country on the score for let's say we want this function to perform its simple task we want to pass
it to argument concrete and Copy tool and we want the function to print the capital of this country
is this so we can just bring it here we can see the first argument is country and in the same way we
don't need to give that type Yeah in other languages we might need to see if the argument number one
is a string an integer a float but as we saw earlier in python you declare variables without assigning
them any types so we can say come talk over here and put a semicolon here hit enter and we can come
down here and a print and then we open our quotation marks the capital of percentage s is percent yes
how about this we can come out of the sentence and put a percentage sign here and then open braces and
say column tree comma copy capital like this and then let's see how it runs so let's call this function
down here way to hit enter and hit enter again and come out of the function and see concrete.
Now let's just go call it from the shell so right.
Control as to save we run here
and it's run so we can call our new function and see a country on the school info.
And then we can see it over here let's say we pass it a country like Ghana and then capital of Accra.
Okay.
So what I just did is I did pass a string I've just passed you know a different type to pass this our
function accepts strings although we didn't define country string country string capital.
It does accept strings.
So we have to pass it country on underscore underscore info and then put a country here and then comma
put a copy to her and then we hit enter and I can see the capital of colonized Agra.
And then we can do another one.
We can say country strikes again.
You can destroy it with any country.
Just pull countries UK capital is London.
Let's see.
And indeed the capital of U.K. is London.
So this this how to write functions with arguments.
So let's see the third type of function you would see.
We've seen the simple function.
We've seen a function that accepts argument.
The third function will be a function that returns values or returns something.
If you may.
So I'll just come down here and call this example number three
quarters function with return value function with a return value like this
and we can come here and define a function.
So we say the F like always and this function we can see and underscore with this assuming we want to
function that adds to numbers so we can pass the first number.
And then the second number and then we'll come out here we put some called on hit enter and we can't
see a return.
A plus B.
Right.
So we'll press control as to save click Run.
And then we run the module and we can see out of this.
That's the name of the function.
What we want to artless you want about 20 comma 30 let's hit enter.
And of course the answer is 50.
So this how to create functions in Python.
Very simple very straightforward.
And this all there is for this very short lesson if you have any questions at all do send me a message
or leave it in the questions area.