Welcome back.
Now let's start with functions.
A function is a block of code that runs only when called.
You can pass data known as parameters, into a function.
A function can return data as a result.
Use def the keyword def to define a function.
Here we are creating a function called greet taking name as parameter and inside this function we have
this print statement printing hello and the passed variable.
This is the function declaration.
So here we are creating and defining a function using the def keyword.
To call a function, use the function name followed by parentheses.
Here we have this function greet and a parameter name.
In order to call the function, we need to call the the the name of the function followed by parentheses.
Inside the parenthesis we define the arguments which later becomes parameters and use them.
Okay.
So let me run this function.
And here we go.
We have hello Alice.
Another run of this function.
Another call passing Bob as a parameter will give us hello Bob.
Information can be passed into functions as arguments.
Arguments are specified after the function name inside the parenthesis.
You can add as many arguments as you want, just separate them with a comma.
So here if I want to add another argument which is of type integer if you want your age years old.
If I run the application we get an error because here we need to pass two parameters the name and age.
So pass the age of Alice 18 Run!
And here we go.
Hello, Alice.
Your age is 18 years old.
Okay.
When the function is called, we pass along the first name and the age.
The terms parameter and argument can be used for the same thing.
Information that are passed into a function.
From a function's perspective, a parameter is a variable listed inside the parentheses in the function.
Define.
An argument is the value that is sent to the function when it's called.
So this is called argument and this is called parameter.
By default, a function must be called with the correct number of arguments, meaning that if the function
expects two arguments exactly like the greet function, you have to call the function with two arguments,
not more and not less.
I can't pass here, for example, 99.56.
If I run, we get an error because this is an extra argument.
One last interesting thing about functions in Python we can pass the default parameter value.
For example, here inside this function let me create a another parameter.
It's of name email and I can give it a default value.
For example a at gmail.com okay.
And here print your email is email.
If I run the application the application runs perfectly because here we have three parameters.
And I pass two parameters and two arguments.
Why it works.
Because here we have a default value.
And if we see your email is a at gmail.com.
If I need to override the default value of the email here we can pass for example CC at com.
Run the application again and here we go.
Your email is CC at gmail.com.
So the default parameter value can be specified by the equal sign in the function.
If we call the function without argument, it uses the default value.
If we call the function with the argument, it will use the argument and override the default value.
Functions can return a value using return statement, for example.
Def square.
We are defining a new function named as square, taking into the parameters a variable x.
X return.
The return statement will return a value of this function x times x.
This is the arithmetic operation.
The product of x times x, which is x squared or x to power x or um sorry not x.
It's x squared because here we have to x times x okay.
The result here is a valuable equals to the returning type of the square.
So here I am calling the function square passing five as an argument.
The returning type will be five times five.
So it will return 25 okay.
The result will be stored inside the result variable and would be printed here using the statement.
The square of f or of five is 25.
Okay, so this is how we define functions in Python.
How to pass parameters.
How to return a resulting value of this function.