Tutorials
Search…
Lesson 14. Functions
In this tutorial, we are not going to get into object-oriented programming. It is a complex topic that deserves a tutorial dedicated to that subject.
When you are learning business analytics you can get pretty far just writing functions without having to go full blown objected oriented, so let’s focus on that.
After a while, you will start to notice that you write the same block of code over and over. It might be the case that other blocks of code use this code, so you cut and paste the lines into other blocks. This is a classic example of code that needs to be turned into a function.
In software engineering, we have a concept called DRY. Don’t repeat yourself. In other words, do not do the thing I just described.
Modern programming languages have all kinds of tools and techniques that you can use to follow the DRY principle. The function is the most common and easiest to implement.
Any block of code that you can write that is valid Python syntax can be placed inside of a function. There are practical limitations here, but, again, we will cover that in a more advanced tutorial.
You can design your function in a manner such that it just does stuff without any outside input and the function does not produce any output. However, most likely, your function needs to do different things based on information passed to it by the line of code that activated it.
You pass in information to functions through function parameters which are frequently referred to as arguments. You can send back output using the return keyword.
When you are writing Python as a series of scripts and not as an object-oriented program, the code executes from top to bottom. That means functions HAVE to be defined before they can be used.

Function Syntax

def function_name(arg1, arg2,...,argN):
#do stuff
#return stuff
#call the function
function_name(arg1, arg2,...,argN)

Examples

Example #1 Hello World! Part II. This Time It’s Personal!
We are going to pull out this old dog and teach it a new trick! Instead of saying hello to every human on the planet, most of whom are jerks, and don’t deserve a hello, we’re going to personalize our greeting by passing in an argument.
The argument will be the name of a specific person. That string value will be assigned to the variable we use as a name of the argument in the function. That variable can be referenced in the function.
def hello_world(name):
print('Hello ' + name + '!')
hello_world('Bob')
Example #2 Passing In More Than One Argument
Passing in arguments to functions can get CRAZY in Python. You can do all kinds of things like set up default values, or assign arguments based on position or keyword.
As a new Python engineer who is only just using the built-in functionality of Python, you will find this kind of flexibility to be useful when you are calling the native functions of Python or Python modules. It is less fun when you are the person that has to implement things that others will use.
Before that happens, there are a thousand things that have to happen in order. We are on number eight. Developing modules is number six hundred and ninety-two so we’re going to keep it simple here.
The example below has three arguments. You pass in the arguments based on their position in the function call. In other words, the first argument in the function all will get assigned to the first argument in the function definition and so on.
def viper(greeting, temperature, units):
print(greeting + 'The temperature is ' + temperature + ' ' + units + '.')
viper('Good morning gentlemen.', '110', 'degrees')
Example #3 Returning Values
Ok here is where I stop screwing around and grim up. Returning values is where functions start to earn their pay. While you can make a function do whatever, usually what you want done is the function to do some repetitive work on some arguments.
Below is a trivial example. As we move through this tutorial, you will start to see the value of functions returning values.
def divide(numerator, denominator):
quotient = numerator / denominator
return int(quotient)
print(divide(10,5))yy

Now you try it!

Don't copy and past. Type the code yourself!
Copy link
On this page
Function Syntax
Examples
Now you try it!