In this post, we are going to learn about Python functions. Functions are a very very useful and important thing to make a successful program. Functions are mostly used to store a number of functions and execute them all when you want. With functions, you can put an unlimited number of child functions and commands. Let's talk more with examples
How to make a Function in Python
We can make a Python function using the def syntax. after the def, you can put a function name. But keep in mind that Python Functions do not support names with whitespaces. After the name, you can put parameters. I'll talk about parameters in the next section.
We are using functionName() calling to run the function. You might need to put this calling else the tasks inside functions won't work. But this calling only works under the function you have created. However, there are some special times that you can call your function anywhere even above or under the function. We'll talk about that in a future post.
Function Arguments and Parameters.
Arguments and parameters are a bit different. Arguments are the values assign to a parameter and parameter is the variable that getting value from agument
Arguments
You can put any number of arguments inside a function. Also, you can assign a value or another variable as the value of the argument.
This is a simple function with a one argument
This is how to work with different arguments in a one function. In there you can use two ways,
- You can put values to calling according to the order of your arguments in function head
- You can specify the value by equaling each argument to value you want
Parameters
We can set a default parameter to our functions. That parameter value is assigning to output when you don't set a argument (value) in the calling function.
Using Return
This statement using to return a value again and again. It means call multiple function callings . This is mostly using to iterate a number calculation.
Tags:
Python