Functions overview

New:

  • function
  • function definition
  • function name
  • function call
  • built-in functions
  • function pow

It's time to learn about a more complex structure of code: function. What comes to mind when you hear that word? Perhaps, the notion of functionality, a measure of usefulness of some device. Or, since we're in an engineering mood, the notion of a math function, like cosine or factorial. Functions in programming do resemble both ideas in some ways. They are, after all, tools designed to simplify programmer's work and increase usefulness.

Finding similarities between programming and other fields or real world is difficult. Many ideas are unique to computers. At the same time, analogies can be extremely useful and lead to a deeper understanding of difficult concepts. We use analogies in these courses, but always limit their scope. And we hope you'll appreciate them over time.

That said, we want to compare functions to machines. It makes sense because functions have properties we can all find in real world machines:

  • Each function has a purpose.
  • Functions must be built before they can be used.
  • They might use some material.
  • They perform work.
  • They are built to produce results.

Looking at two objects — a simple washing machine and an unknown machine — you'd recognize the first by its familiar drum and buttons, and from experience you'd know to put laundry in it, but not paper, for example. Without experience, it's impossible to say anything so specific about the second machine, besides those abstract ideas from the list above.

The use of a physical machine, however intricate, is inseparable from the device itself. In other words, you can't use a washing machine without interacting with it directly, without seeing and touching it. Functions in programming are different: you use them without seeing. Kind of remotely. Because of this, their many properties remain unknown until someone explains them to you or until you experiment and come to conclusions yourself. That's why it's important to draw the line here and not make further analogies between real machines and functions.

This separation of function and its use comes from code, where creating a function and using it are distinct units described in the list below. Moreover, the creating part is often hidden. That's why you'll "use it without seeing and touching". You won't create functions until Course 2, but it's not a problem right now. Many functions come already "built-in" with Python1. We'll try some of them and learn about general properties and features of functions. But first, we should introduce new terminology:

  1. Function definition is the code which "creates" the function2. Think of it as function's internals, the underlying mechanics. The definition is written only once, and is always separate from use. Although, sometimes, the definition and the use code units may appear next to one another. The definition also establishes the function name.

  2. Function name is a unique identifier, a word or a combination of words. For example, pow. The function is referenced by its name in the function call.

  3. Function call is a unit of code consisting of the function name followed by parentheses. This code launches the function, it is the use. The parentheses contain values: specific information for a particular call. The example below demonstrates a call pow(3, 2). Looking at it, imagine that machine's internals — function definition — doing their job.

The next step is to run the code containing the call by pressing 3. Remember how the interpreter reacts to it:

  1. Reads your program, line by line.
  2. Tries to understand it, constrained by syntax rules.
  3. If code is valid, does whatever the code says to do.

Run the code:

The output is empty, like it was with values from the previous lessons. Even though the interpreter had successfully done everything described in the list above, we still haven't written a special instruction for displaying values.

By looking at the call and running the code, it’s virtually impossible to understand what pow does, what those numbers mean, and what’s happening in general. We, your instructors, happen to know the answers, simply because we knew where to look: the documentation of Python. From there we can tell you the following:

  • pow is short for "power"
  • pow works with two numbers
  • pow produces the result by raising one number to the power of the other.

Why did we begin with pow and not some other function? Because pow is simple and familiar enough, it vaguely reminds of some strange, limited calculator. Other built-in functions don't remind of anything from real life, so it's harder to draw analogies between them and machines.

Functions are essential building blocks of modern code. Most of your work as a programmer will revolve around creating, modifying, and launching functions. Yet, they are not mandatory parts of programs. One can write programs without functions. In fact, during the early days of computing, programming languages didn’t even allow functions in the modern sense of the word.


Summary:

  1. Function is a sort of machine with specific functionality.
  2. Function’s name is its unique identifier, a word, or a combination of words. All built-in functions have names.
  3. Function call is a unit of code consisting of the function’s name followed by parentheses (e.g. pow(5, 4)).
  4. Some functions are already "built-in" into Python.
  5. When calling, you might not know what the function does.
  6. Function pow is "silent", it does not produce any visual output.
1

Since we’re using the version of Python written in C, most of those built-in functions are also written in C, not Python.

3

This particular mechanism of launching programs by pressing is the specific feature of REPL.it web app. The interpreter itself does not have any buttons, it’s a non-visual program. REPL.it is actually a combination of editor and interpreter, which allows you to write, edit and run code. is a handy helper button which launches the interpreter and asks it to read and run the code from the editor.

2

The whole second course is about function definitions.