06.09.2021 Views

How to Think Like a Computer Scientist - Learning with Python, 2008a

How to Think Like a Computer Scientist - Learning with Python, 2008a

How to Think Like a Computer Scientist - Learning with Python, 2008a

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 3<br />

Functions<br />

3.1 Function calls<br />

You have already seen one example of a function call:<br />

>>> type("32")<br />

<br />

The name of the function is type, and it displays the type of a value or variable.<br />

The value or variable, which is called the argument of the function, has <strong>to</strong> be<br />

enclosed in parentheses. It is common <strong>to</strong> say that a function “takes” an argument<br />

and “returns” a result. The result is called the return value.<br />

Instead of printing the return value, we could assign it <strong>to</strong> a variable:<br />

>>> betty = type("32")<br />

>>> print betty<br />

<br />

As another example, the id function takes a value or a variable and returns an<br />

integer that acts as a unique identifier for the value:<br />

>>> id(3)<br />

134882108<br />

>>> betty = 3<br />

>>> id(betty)<br />

134882108<br />

Every value has an id, which is a unique number related <strong>to</strong> where it is s<strong>to</strong>red in<br />

the memory of the computer. The id of a variable is the id of the value <strong>to</strong> which<br />

it refers.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!