Session 18

Calling and Defining Functions


CS 1510
Introduction to Computing


Opening Exercise: Parameter Challenge

What does this program print?

    def mystery(x, z, y):
        x = x * y
        y = y * z
        z = z * x
        return z

    x = 2
    y = 3
    z = 4
    x = mystery(y, z, x)
    print(x, y, z)

This looks confusing, but remember what we learned last time: mystery() doesn't know anything about the code that calls it. This includes the names of the variables or the destination of the return value. The reverse is true, too. The calling code doesn't know anything about the function's code, including the names of its variables.

All communication between the two consists of the values passed as arguments and the value returned as the result.

If you just have to know the answer now, here it is.



Function Calls and Definitions

The anatomy of a function call:

name and list of arguments

The anatomy of a function definition:

a function definition

The parameters are the names used by the function to refer to the values passed as arguments by the caller. They are like variables known only to the function. Their initial values are the values of the arguments, in the same order they were passed.

The return statement is the mechanism by which the function sends its value back to the caller. You have noticed that some functions don't have a return statement. We use those functions (only) to execute an action, such as print something.

Let's trace what happens when the function is called, expanding on the steps we discussed last time.

1. Python evaluates the arguments.

This simply means that it determines the value of each argument.

In this case, all three arguments are variables, so Python looks up the current value of each:

a function definition
2a. The values are given to the function...
a function definition
2b. ... and assigned to the function's parameters.

These arguments are bound to the parameters in order. The function doesn't know anything about where these values came from, only the order in which they are sent.

a function definition
3. The function uses its parameters to perform its operation.

The computation doesn't have anything to with the variables in the calling code, or their values!

the function performs its operation
4. Python inserts the value of the function in place of the function call.

So, x becomes 24.

the function performs its operation

We now know all the parts of the program's output:

    >>>
    24 3 4
    >>>

Learning this process for the first time takes... time. Study the steps, and apply them over and over until you begin to see the process happen automatically. The good news is that this effort pays off. Once this process enters your mind's "muscle memory", it will happen automatically for you.

Quick exercise::

What happens if we rename mystery's parameters to foo, bar, and baz?

If we do so consistently throughout, nothing changes in how the function does its job. But we may be a lot less confused when reading the code -- even with the silly names. Try this sometime if you find yourself struggling to understand how a piece of code and think that the variable names are part of the reason.



Lessons from the Lab

... walk through the tasks...

Following the process outlined above makes all of the challenge programs doable in a straightforward way. Tedious, perhaps, but straightforward.

... computing means.

... hashing strings.

Too many of you still struggle (1) to recognize the need or (2) to use the basic layout when you write code. There is only one way to make this problem go away.

... last week's lab: cat_to_file. With a function, we can : make the duplication go away. ... errors, modifications. Clearer: one place for each piece of knowledge.



A Quick Word on the Yik Yak Controversy

Computing is often in the news, but we don't talk much about current events in class. That's not the focus of this course, and we have plenty to do...

But the recent news story in the Northern Iowan about Yik Yak has been on my mind. Yik Yak is a social media app that lets people make comments anonymously and vote on other people's comments. This kind of app has many possible uses, some of which are positive. Many people live under conditions where they need to be able to communicate anonymously.

Unfortunately, some people in the UNI area have been using it to post hurtful comments about various groups. This behavior is simply mean.

Yik Yak is a social app, and the controversy is about people and how they behave. At that level, my reaction has been similar to so many others' reactions. I am always sad to be reminded that people actually think such things, and sadder to know that they feel compelled to say them out loud. To do so anonymously is an act of cowardice.

But this controversy is also about what we do, because Yik Yak is a program. We call it an "app", but that's just the term du jour. It is a computer program. Programmers wrote it.

We could have an interesting discussion about apps like this: their uses, the good and bad they enable, how to grow and guide communities of users, and so on. I do not use Yik Yak and so am not a member of its community, so I don't know much beyond what has been reported about the recent threads. But I have been part of Internet-based communities since I was in school, and they all have many features in common.

Let me just say this.

When you learn to program, you inherit power to affect the world. You can make things, programs and apps and services that real people use. You can choose to use your power to do good things, to make the world better. Or you can not choose to. Not choosing may mean creating something whose effects you did not consider, or whose community behaves in ways you did not intend.

Please take this power seriously. Think about the effects of what you do when you write a program. Choose wisely.



The Answer, For the Impatient

24. As much as this function seems to do, it computes the product of its arguments. Comment out this line:

    y = y * z

... and see that it adds nothing to the result.     (return to session)



Wrap Up



Eugene Wallingford ..... wallingf@cs.uni.edu ..... October 23, 2014