Setup Menus in Admin Panel

Introduction To Python Functions

The Basics Of Python Functions

Introduction To Python Functions

In this Pro Python Programmer tutorial section, we will be providing an introduction to Python functions. In the previous, one hundred or so Python tutorials we worked with some functions in our statements which were created by other programmers. In this section, we will show you how to create your own functions. You will soon learn how important Python functions are in everyday programming. Before you dive into this Python tutorial. I want you to enter this tutorial with an open mind. Functions may look intimidating at first but in fact they are very simple.

What Is A Function In Python?

A function is a block of code that contains other Python code that can be called throughout a Python program and may take parameters to compute a result. These parameters may change each time the code is ran which would provide different results.

Python is a great supporter of the DRY concept which means “Don’t Repeat Yourself”. Functions help us perform a task in our Python programs without repeating our code over and over. When writing a program in Python, if you are required to write the same code more once then you are probably better off putting the code in a function and then calling the function by the functions name to use that code. Remember DRY plays an important role in programming. If you are repeating yourself, you probably are doing something wrong.

Why Functions?

The main reason we use functions in Python is code reusability. If you have a strict coding structure which means you code with a strict pattern of rules you will never need to rewrite the same function over again. When I write a function I have a tendency to put them in a directory on my computer and if I ever need to rewrite that function again I can just grab that function and through it into my current project. This is utilizing reusability to its fullest.

Now that we understand that functions are a critical tool for the lazy programmer like myself. Functions also enable us to break our code down into a procedural process of functions. For example, if we were to a program that changed light bulbs our code structure would probably go in this order. First function would turn off the light switch, next function would twist the light bulb out of the socket, third function would get a new bulb, fourth function would tighten a new light bulb in and the last function would turn the light switch on.

With the procedure programming in mind I have a tendency to list out my functions ahead of time. This enables me to plan my course of action when approaching a coding issue. If you think of each function of as a step to complete a programming task your code will be well structured and you will avoid headaches down the road.

The Function Break Down

Once you understand the structure of a function it will not only help code faster but bring you into the world of writing reusable code. Functions may look overwhelming at first but they are rather simple. I like to think of functions as a variable for a block of code that runs multiple statements to return an object. When we call a function it is very close to calling a variable. The only difference is functions can take arguments.

If you have been following a long in our tutorial series you have used functions on many occasions and did not even notice. Now you will start to understand the whole picture as we walk through the parts of the Python function.

Defining A Function

When we create a function in Python, we use the def statement. The def statement indicates to Python that we are created a new function. The format of a basic function is similar to the one below.

The structure can be written in two ways. The above example is the preferred method and the most widely used. There is also a shorthand method to write functions. *Note: this is sloppy and confusing.

The def line can be thought of as the header and the indented lines of code below is considered the functions body.

A function is not required to have any arguments. We can easily write functions to have no arguments. Look at our example below.

It is important to understand that a function will not run till the program reaches the def statement. When Python reaches the def statement then and only then will the function be ran.

Functions can be nested as where a statement can be nested. We can insert functions into if statementsfor loops, while loops, in other functions and so on. Normally, in larger programs we have a tendency to put our functions in another Python file and then import the functions as needed. This procedure helps ensure that the programs code neat and well organized.

The Arguments

Functional arguments are just parameters that are to be sent to function to help execute the task in hand. In most instance Python functional arguments are positional. Which means if you have several arguments then the arguments will be inserted left to right. The first argument would be the most left argument, and so on.

We need to look at positional aspect of the Python functional arguments so you can better understand what I mean by positional arguments.

In the above function you can see it does depend on the position of our arguments. Let’s take a look at another example, to sure up your understanding of positional arguments in Python functions.

In the example, above we flip flop the position of the arguments to show you, when you call a function you need to add your arguments in the position based on the function’s parameter positioning. In the body, it does not matter which order that you call them in.

A function does not need to have any arguments or parameters. Let’s take a look at a function that does not require an argument or have any parameters.

The above example contains a function that takes no arguments. There is no need for an argument because this function does one thing and that is return a string of content.

Functions Create An Object

Functions are similar to a variable. Functions create an object and that object is allocated to the function name. Just like a variable. The difference is that functions make an object by running some sort of coding statement or statements where a variable is assigned an object.

Check out the example of how we call a function here. We will first create a function and then we will call that function.

In the example, above you can see we can call the created object via the function name just like we call a variable the only difference is with this function we need to add two arguments.

Make this point clearer let’s call the type built-in function on our function and on a variable that contains a similar object.

Now that you see calling a variable is pretty close to calling a function. The only difference is the case that functions can take arguments. Functions also can run multiple statements to create an object where variables can only run one statement and represent that object.

The Return

Another part of a Python function is the return. The return represents the data(object) we want to send back to who called the function. When a function is called, run through of all the code will be halted till the response from the function is received. If no object is returned then Python will return None. You will probably see this happen on numerous occasions till you’re comfortable with Python functions.

We tend to normally see a return statement at the end of functions but by rule they can be placed anywhere in the function. We could have more than one return in a function. Need to examine our example below.

The return statement is totally optional. We can functions all day long without ever using the return statement. If you omit the return statement then in theory Python will return None but this is often ignored at run time. Look at an example of a function without an return statement.

Notice we are printed back the value without a return statement. how about what happens if we do not use the print statement. Let’s take a look at return of None.

Conclusion

In this Python tutorial, you have learned some the basics to Python functions. This is just a starting point to our journey through Python functions. It is important to take away from this lesson that functions are not difficult even though they may look difficult. We use functions to structure our code in matter where we do not need to repeat ourselves all while keeping our code need and organized.

If you have any questions about functions feel free to ask.

SEE ALL Add a note
YOU
Add your Comment
 
top
©Master Code Online
X