Coding Practice 3

Ace your homework & exams now with Quizwiz!

When creating a function using the def statement,

you can specify what the return value should be with a return statement.

When your program calls a function, Python creates a frame object on the top of the call stack.

Frame objects store the line number of the original function call so that Python can remember where to return. If another function call is made, Python puts another frame object on the call stack above the other one.

1. Why are functions advantageous to have in your programs?

Functions reduce the need for duplicate code. This makes programs shorter, easier to read, and easier to update.

8. If a function does not have a return statement, what is the return value of a call to that function?

If there is no return statement for a function, its return value is None.

In a function, a variable will either always be global or always be local.

If you ever want to modify the value stored in a global variable from in a function, you must use a global statement on that variable.

Code: print('Hello', end='') print('World')

Output: HelloWorld

Code: print('cats', 'dogs', 'mice')

Output: cats dogs mice

Code: print('cats', 'dogs', 'mice', sep=',')

Output: cats,dogs,mice

12. How can you prevent a program from crashing when it gets an error?

Place the line of code that might cause an error in a try clause.

11. What does the import areallyourpetsnamederic statement do?

That import statement imports a module named areallyourpetsnamederic.

2. When does the code in a function execute: when the function is defined or when the function is called?

The code in a function executes when the function is called, not when the function is defined.

13. What goes in the try clause? What goes in the except clause?

The code that could potentially cause an error goes in the try clause. The code that executes if an error happens goes in the except clause.

3. What statement creates a function?

The def statement defines (creates) a function.

Parameters and variables that are assigned in a called function are said to exist in that function's local scope.

Variables that are assigned outside all functions are said to exist in the global scope.

6. What happens to variables in a local scope when the function call returns?

When a function returns, the local scope is destroyed, and all the variables in it are forgotten.

In Python, there is a value called None, which represents the absence of a value.

Other programming languages might call this value null, nil, or undefined.

Code: print('Hello') print('World')

Output: Hello World

Keyword arguments are identified by the keyword put before them in the function call.

Keyword arguments are often used for optional parameters. For example, the print() function has the optional parameters end and sep to specify what should be printed at the end of its arguments and between its arguments (separating them), respectively.

10. What is the data type of None?

NoneType

A return statement consists of the following:

1. the return keyword 2. the value or expression that the function should return

Scopes matter for several reasons:

- Code in the global scope, outside of all functions, cannot use any local variables. - However, code in a local scope can access global variables. - Code in a function's local scope cannot use variables in any other local scope. - You can use the same name for different variables if they are in different scopes. There can be a local variable named spam and a global variable also named spam.

There are four rules to tell whether a variable is in a local scope or global scope:

1. If a variable is being used in the global scope (that is, outside of all functions), then it is always a global variable. 2. If there is a global statement for that variable in a function, it is a global variable. 3. Otherwise, if the variable is used in an assignment statement in the function, it is a local variable. 4. But if the variable is not used in an assignment statement, it is a global variable.

4. What is the difference between a function and a function call?

A function consists of the def statement and the code in its def clause. A function call is what moves the program execution into the function, and the function call evaluates to the function's return value.

9. How can you force a variable in a function to refer to the global variable?

A global statement will force a variable in a function to refer to the global variable.

Think of a scope as a container for variables. There is only one global scope, and it is created when your program begins. When your program terminates, the global scope is destroyed, and all its variables are forgotten.

A local scope is created whenever a function is called. Any variables assigned in the function exist within the function's local scope. When the function returns, the local scope is destroyed, and these variables are forgotten. The next time you call the function, the local variables will not remember the values stored in them from the last time the function was called. Local variables are also stored in frame objects on the call stack.

7. What is a return value? Can a return value be part of an expression?

A return value is the value that a function call evaluates to. Like any value, a return value can be used as part of an expression.

A variable that exists in a local scope is called a local variable, while a variable that exists in the global scope is called a global variable.

A variable must be one or the other; it cannot be both local and global.

Python adds return None to the end of any function definition with no return statement. This is similar to how a while or for loop implicitly ends with a continue statement.

Also, if you use a return statement without a value (that is, just the return keyword by itself), then None is returned.

5. How many global scopes are there in a Python program? How many local scopes?

There is one global scope, and a local scope is created whenever a function is called.

Python has different scopes because that way it narrows down the number of lines of code that may be causing a bug.

Therefore, while using global variables in small programs is fine, it is a bad habit to rely on global variables as your programs get larger and larger.

Often, all you need to know about a function are its inputs (the parameters) and output value; you don't always have to burden yourself with how the function's code actually works. When you think about functions in this high-level way, it's common to say that you're treating a function as a "black box."

This idea is fundamental to modern programming. Because writing functions without global variables is encouraged, you usually don't have to worry about the function's code interacting with the rest of your program.

➊ def sayHello(name): print('Hello, ' + name) ➋ sayHello('Al')

To define a function is to create it, just like an assignment statement like spam = 42 creates the spam variable. The def statement defines the sayHello() function ➊. The sayHello('Al') line ➋ calls the now-created function, sending the execution to the top of the function's code. This function call is also known as passing the string value 'Al' to the function. A value being passed to a function in a function call is an argument. The argument 'Al' is assigned to a local variable named name. Variables that have arguments assigned to them are parameters.


Related study sets

2.04 Quiz: Review of Equations 2

View Set

Phlebotomy Essentials 6th edition. ALL quizzes, ALL ch. tests, GRADED work, NOT guesses. PLUS, the FULL NAHP study guide

View Set

social studies Chapter 5 - Section 3 and 4 8th grade

View Set

ANTH 1101 Peoples and Cultures (Section 1: Understanding & Studying Culture)

View Set

Gerontology Dynamic quiz practice questions

View Set

Sonidos en Contexto 20 [f] 21 [t͡ʃ] and 22 [m] [ɱ] [n̪] [n] [ɲ] [ŋ]

View Set

Fundamentals of Anatomy & Physiology, Chapter 6: Osseous Tissue and Bone Structure

View Set

PMP-RMC Super Exam-2 Failed questions

View Set