Functions

code guest
Python Journey
Published in
5 min readSep 11, 2019

Functions are indispensable in our coding journey — they are like the programmers’ equivalent of a magician’s spells.

For example, Python provides a built in print() function — whenever we print something to the Python console, we just type in print(something) and it appears on screen — but it takes a lot of code to implement the print feature!

See how long the code for the implementation of the print function is!

source code for Python’s print function (credits: Python Software Foundation)

Basically, a function comprises two things.

1) a name — like print

2) a sequence of statements that together perform some task(s) — like displaying something on the screen such as the source code above

Because a function has a name eg. print, and a function’s name just refers back to the function, we can use a function’s name in our code to execute it.

The sequence of statements contained within a function is called its function body and it is the function body that performs the magic when we call a function eg. print(“hello world”).

Both a function’s name and function body contribute to the cool features of functions listed below:

Functions are short and sweet

  • all it takes to use a function is to “call it” by writing the name of the function (eg. print) and supplying arguments that it needs (eg. the string “hello world”) and voila! — print(“hello world”)
  • supplementing the point above — a function’s name abstracts or hides the details of its implementation. We don’t need to know about the complexities of the function body’s Python statements

Functions are reusable

  • we can reuse functions like print() over and over

Functions from libraries are very useful

  • there are many talented developers who have worked on libraries that provide useful functions which we can use without needing to know how the function was implemented in code

Functions are very transferable

  • we can easily share our code in one file with other files or with other people by packaging our code in functions for others to call

Defining functions

Before we create a function, we need a name for it and some code for its function body.

Then, to code for the creation of a function (also called defining a function), we will need to provide Python with five key things:

  1. def keyword
  2. Function name
  3. brackets indicate whether the function takes any parameters
  4. colon indicates end of function declaration
  5. indentation to show where code block containing the function body code starts

The code above is called function definition and here are four things to note about it:

Firstly, function definition associates a name with the function which Python will remember for future reference — the name of the function comes after the def keyword.

Secondly, function definition tells Python whether the function takes any parameters. Parameters let us supply data to a function eg. “hello world” to print() function that lets us customise a function’s output — a function’s parameters are in parentheses right next to its name.

Thirdly, function definition tells Python where the function body starts and ends — the function body is in the next line after the colon symbol and is indented. The function definition ends where the indentation ends.

Fourthly, function definition doesn’t run the function. Instead, function definition tells Python to remember the function’s name and know how to execute the function when it is called.

Function definition does a couple of things — associating a name to the function and telling Python about its parameters and code body (credits: undraw.co)

Executing a function

To execute a function, we need to call it or invoke it by writing the function’s name followed by parentheses.

The parentheses tell Python to execute the named function.

On the other hand, when we just write the function’s name in our code, we are just referencing the function object.

Function Object

During function definition, Python creates a function object, stores it in our computer memory and assigns it to a name.

A function object contains the logic of the function and the interesting thing is we can assign different names to the same function object.

For example, the print() function already has a name called print. But we can give it an additional name too:

  • Firstly, the name print references the print() function object
  • assigning print to variable x means the name x now refers to the print() function object
  • calling x(“hello”) is the same as calling print(“hello”)

Variables can refer to function objects the same way they refer to data values:

  • Let us start with a variable called x being assigned the value of 10 → x = 10
  • and then we assign the function object of built-in function print to x → x = print
  • then the variable x now refers to the print() function

Summary

  • Functions can have names and comprise of a sequence of related statements
  • Benefits of using functions include simply using their names for invocation, availability of awesome libraries, reusability and transferability
  • Function definition requires five things — def keyword, function name, parentheses, colon and indented code body
  • Function definition results in creation of named function object
  • To call a function, the function name and parentheses are required. The function name alone refers to its function object

References

Quiz

Which of the following options are accurate about functions?

a) Functions carry out operations and computational stuff behind the features of apps that we use.

b) Functions cannot contain other functions within them

c) Good programmers write their own functions instead of relying on code libraries in their programs

d) Functions help coders write less code

Quiz explanation

a) Functions are heavily utilised in the apps that we use/build because of the benefits they bring

b) We can define functions inside of functions — so it is possible to have one large function containing functions defined within its function body!

c) Good programmers see the value in using libraries and make good use of them. Some good programmers even contribute to improving these libraries through open source efforts!

d) Functions can be imported from libraries and are reusable — these features help us to save on code writing

Quiz answers: a & d

--

--