Welcome to another video of our pipeline listeners today we're going to talk about functions and python
functions or just a repeatable blocks of code, meaning you can have a block of code and repeat the
block whenever you want by calling the function name a function can accept inputs or arguments inside
parentheses.
You can also return values to the calling program.
Let's create a very simple function that takes no arguments.
We start our function by the word def.
It's short for define followed by the name of the function you want to create.
Now let's print something.
Now, if we want to call this function from our program, we simply type its name.
Right now, if we want our function to return something to us, we type return followed by whatever
we want to return or just using the word return and we'll return nothing.
But all of that has to happen inside the function itself, not from the main program.
Let's create a variable in our main program now to store the value of X. We get from the function.
Good.
Now let's see how we pass arguments to dysfunction, let's create a function that does a simple addition
to two numbers.
See, here we are passing X and Y to the function, then we can we can we can create a variable called
result and we saw the addition of X and Y into it.
Now, let's call this function, but we need to pass a value for X and Y, let's pass five, four,
six and ten for Y.
Notice that when passing an argument, they have to be in the same quarter as the function.
So now if we pass 10 first and then five, 10 will be stored in X and five will be stored in Y, but
in this example, five will be stored in X and 10 will be stored in one.
Of course, since we're adding them, it doesn't matter.
But maybe if you're dividing it will.
Now, what happens if we call a function that takes two arguments, but we only pass one of them?
Let's see.
As you noticed, it didn't like that the function takes at least two arguments, but only one given.
Is there a way around this?
Well, yeah, we can have an optional argument, but it must be specified.
Let's let's see how we accomplish this.
Now, whether we pass S. or not, it will still use the default value of C, which is 10.
So even if we passed only two arguments still here, we'll use the default value for.
But if we want to change the default value of C, we can pass a different value for it.
Good.
Now let's create our first script that will have three functions add, subtract and multiply.
Like the best script, the first line is our Python interpreter, keep in mind that while our variable
name inside all functions are the same, which is result, but they are only accessible from inside
the functionally.
So you can add the same variable inside each function, but that variable belongs only to that function.
Now let's run the script.
Now, we have reached the end of this lesson, thank you and see you in the next one.