Legacy - Writing Functions
- 01:51
Learn how to build your own functions in Python.
Downloads
No associated resources to download.
Transcript
We shall now look at how to write our own functions. In Python. A function is a block of code which only runs when it is called. You can pass data known as parameters into a function, a function can return data. As a result, it is really easy to understand the structure of writing the function. Once we look at an example, when defining a function in Python, we need to follow syntax rules, it is compulsory to start the function with a def, which simply reads define the function, then we need to name the function as we need to have a name for the function to call it in the future. The function name can also have parameters within them. For example, in the example of the screen, the function my function would print a statement, I work dash to predict the parameter. So if we call the function and include M&A in the parameter section, we are going to get a statement printed, I work at M&A day, the function would print similar items as long as the data passed within the function is a string. In this case, this is because you can concatenate two strings together, but you cannot concatenate a string and an integer or a float. Again, some key points to note when writing a function, we need to start the function with a def DEF. We did it to the function date, so that we can call the function in the future a colon needs to follow the function name, we need to include proper indentation. And finally, we need to call the function using the function name when necessary, as this would perform the required action