# Function Calls¶

Python can compute new values with function calls. You are familiar with the idea of functions from high school algebra. There you might define a function f by specifying how it transforms an input into an output, f(x) = 3x + 2. Then, you might write f(5) and expect to get the value 17.

Python adopts a similar syntax for invoking functions. If there is a named function foo that takes a single input, we can invoke foo on the value 5 by writing foo(5).

There are many built-in functions available in python. You’ll be seeing some in this chapter and the next couple of chapters.

It is also possible for programmers to define new functions in their programs. You will learn how to do that later in the course. For now, you just need to learn how to invoke, or call, a function, and understand that the execution of the function returns a computed value.

We’ve defined two functions above. The code is hidden so as not to bother you (yet) with how functions are defined. square takes a single input parameter, and returns that input multiplied by itself. sub takes two input parameters and returns the result of subtracting the second from the first. Obviously, these functions are not particularly useful, since we have the operators * and - available. But they illustrate how functions work.

Notice that when a function takes more than one input parameter, the inputs are separated by a comma. Also notice that the order of the inputs matters. The value before the comma is treated as the first input, the value after it as the second input.

Again, remember that when python performs computations, the results are only show in the output window if there’s a print statement that says to do that.

Remember the note that some kinds of python objects don’t have a nice printed representation? Functions are themselves just objects. If you tell python to print the function object, rather than printing the results of invoking the function object, you’ll get one of those not-so-nice printed representations. Just stating the name of the function refers to the function. The name of the function followed by parentheses () invokes the function.

rec-5-39: What will the output be from this code?

print sub(5, 8)

• (A) sub(5, 8)
• The result of executing the function call will print out
• (B) -3
• The second is subtracted from the first
• (C) 3
• The second is subtracted from the first
• (D) nothing will print
• The print statement makes the results print

rec-5-40: What will the output be from this code?

print "sub(5, 8)"

• (A) sub(5, 8)
• The character sting is treated as a literal and printed out, without executing.
• (B) -3
• The character sting is treated as a literal and printed out, without executing.
• (C) 3
• The character sting is treated as a literal and printed out, without executing.
• (D) nothing will print
• The character sting is treated as a literal and printed out, without executing.

rec-5-41: What will the output be from this code?

sub(5, 8)

• (A) sub(5, 8)
• There is no print statement
• (B) -3
• There is no print statement
• (C) 3
• There is no print statement
• (D) nothing will print
• There is no print statement

rec-5-42: What will the output be from this code?

"sub(5, 8)"

• (A) sub(5, 8)
• There is no print statement
• (B) -3
• There is no print statement
• (C) 3
• There is no print statement
• (D) nothing will print
• There is no print statement
Next Section - Data Types