How to use Python Functions

Published: 14 June 2020
on channel: Practical Python Solutions
78
6

https://github.com/pmahon2016/lists.git
https://docs.python.org/3/tutorial/


How to use Python Functions. In this video we're going to look at functions and how they can help you write better and more efficient code.
Functions in Python, or any programming language, are specific blocks of code that are typically designed to do one job. We're going to learn how to define functions in Python and how to call the functions we define. We’ll also see how to send variables or “arguments” to those functions and get return values as well.

Every function in Python begins with the “def” command which is short for define. This is followed by the name of the function. There are some guidelines for naming functions but basically, the name should be in lower case with words separated by an underscore. The name will need to be followed by parentheses. In our example here the parentheses will be empty, but will see later how to put parameters for the function in these parentheses. Finally, you will need to end the line with a colon. The following line will be indented and contain the code we want to execute when the function is called. We will simply type the print statement to print out “Hello there”

If we run this program, nothing will happen because there's no code calling the function.
So, we’ll call the function down here - you must use the parentheses after the function name. When we run the program, now, it calls the function and executes it's one little task.
This is a very basic example of a function. Now, let’s make this function a little more interesting and give it two parameters: a name parameter and a program language. We’ll change the print statement to say hi my name is “name” and this is my favorite programming language.
This function now expects two arguments to work correctly. The variables here are called parameters – as in, the function takes two parameters.

When calling this function, you must send two “arguments” - the arguments go here. These terms are often used interchangeably, but best to stick with these definitions for clarity.

When the function is called with the arguments “Paul” and “Python” they are assigned to the function parameters “Name” and “language.” The print statement in the function is then executed and the following is printed out: My name is Paul and my favorite programming language is Python.

When change the arguments to “John” and “Java” It’ll substitute these arguments into the function parameters and print them in string.

So this is how you can send multiple arguments to a function. These multiple arguments
are called positional arguments and the reason obviously is that they need to be in
line with what the function expects. If we change the order here, for example, and put
the programming language first and the name second, we're not going to get what
we expect in the print output.

You can get around this by defining the variables in your function call. For example, if we say “language = java” and “name = Joe” the print statement will work correctly even though these arguments are not in the incorrect order. We have the programming language first and the name second.

One final way to receive arguments to a function is using default values. In our example we could have the programming language defaulting to Python, which means that in our function call we don't need to specifiy a program language. The function has two parameters but if we only send one the function will still work because it is a
default the programming language value to Python. If we do include two arguments with a language value other than the default, it will override the default and work fine.

So the three ways to send arguments to a function: positional arguments, keyword arguments and finally default parameters


Watch video How to use Python Functions online without registration, duration hours minute second in high quality. This video was added by user Practical Python Solutions 14 June 2020, don't forget to share it with your friends and acquaintances, it has been viewed on our site 78 once and liked it 6 people.