Chapter 6 Programming

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

def max(num1, num2): if num1 > num2: result = num1 else: result = num2 return result 2. What are the formal parameters?

num1 and num2

def max(num1, num2): if num1 > num2: result = num1 else: result = num2 return result 3. What is the parameter list?

num1, num2

Arguments to functions always appear within __________.

parentheses

What will be displayed by the following code? x = 1 def f1(): global x x = x + 2 print(x) f1() print(x)

3 3 this is because a global variable is created and then used outside the scope

def max(num1, num2): if num1 > num2: result = num1 else: result = num2 return result How do you simplify the max function using the conditional expression?

return num1 if (num1 > num2) else num2

What is the return value from invoking p(27)? def p(n): for i in range(2, n): if n % i == 0: return True return False

False

def max(num1, num2): if num1 > num2: result = num1 else: result = num2 return result 6. Where does the function return a value?

The return statement returns the value

Can the following code run? If so, what is the output? x = 10 if x < 0: y = -1 else: y = 1 print("y is", y)

Yes. The output is y is 1

A variable defined outside a function is referred to as __________.

a global variable

A variable defined inside a function is referred to as __________.

a local variable

Each time a function is invoked, the system stores parameters and local variables in an area of memory, known as _______, which stores elements in last-in first-out fashion.

a stack

Given the following function, what will be displayed by the call nPrint('a', 4)? def nPrint(message, n): while n > 0: print(message, end = '') n -= 1

aaaa

def max(num1, num2): if num1 > num2: result = num1 else: result = num2 return result 4. What is the function header?

def max(num1, num2)

def max(num1, num2): if num1 > num2: result = num1 else: result = num2 return result 1. What is the function name?

max

What is wrong in the following code? 1 def function(): 2 x = 4.5 3 y = 3.4 4 print(x) 5 print(y) 6 7 function() 8 print(x) 9 print(y)

x and y are not defined outside the function.

What are the benefits of using a function?

(1) Reuse code; (2) Reduce complexity; (3) Easy to maintain.

A function _________.

may have no parameters

What is the output of the following code? (a) def function(x): print(x) x = 4.5 y = 3.4 print(y) x = 2 y = 4 function(x) print(x) print(y) (b) def f(x, y = 1, z = 2): return x + y + z print(f(1, 1, 1)) print(f(y = 1, x = 2, z = 3)) print(f(1, z = 3))

(a) 2 3.4 2 4 (b) 3 6 5

What will be displayed by the following code? x = 1 def f1(): print(x, end = " ") f1() print(x)

1 1

What will be displayed by the following code? x = 1 def f1(): x = 3 # hint: displays 3 print(x, end = " ") f1() print(x) # hint: displays 1

3 1

What will be displayed by the following code? x = 1 def f1(): y = x + 2 print(y, end = " ") f1() print(x)

3 1

Can you have a return statement in a None function? Does the return statement in the following function cause syntax errors? def xFunction(x, y): print(x + y) return

A syntax error occurs if a return statement is not used to return a value in a value-returning function. You can have a return statement in a None function, which simply exits the function. So, this function is OK, although the return statement is not needed at all.

Given the following function header, which of the following is correct to invoke it? def f(p1, p2, p3, p4)

A. f(1, 2, 3, 4) D. f(p1 = 1, p2 = 2, p3 = 3, p4 = 4) E. f(1, 2, 3, p4 = 4)

Write function headers for the following functions (and indicate whether the function returns a value): (a) Computing a sales commission, given the sales amount and the commission rate. (b) Printing the calendar for a month, given the month and year. (c) Computing a square root. (d) Testing whether a number is even, and returning true if it is. (e) Printing a message a specified number of times. (f) Computing the monthly payment, given the loan amount, number of years, and annual interest rate. (g) Finding the corresponding uppercase letter, given a lowercase letter.

Computing a sales commission given the sales amount and the commission rate def getCommission(salesAmount, commissionRate): returns a value Printing a calendar for a month def printCalendar(month, year): Computing a square root def sqrt(value): returns a value Testing whether a number is even and return true if it is def isEven(value): returns a value Printing a message for a specified number of times def printMessage(message, times): Computing the monthly payment, given the loan amount, number of years, and annual interest rate. def monthlyPayment(loanAmount, numberOfYears, annualInterestRate): returns a value Finding the corresponding uppercase letter given a lowercase letter. def getUpperCase(char letter): returns a value

Does the function call main() in the following code cause syntax errors? import mathdef main( ):return math.sin(math.pi) main( )

No Hint: A value-returning function can also be invoked as a statement. In this case, the caller simply ignores the return value. This is rare, but permissible if the caller is not interested in the return value.

A function with no return statement returns ______.

None

def max(num1, num2): if num1 > num2: result = num1 else: result = num2 return result 5. What is the function body?

The function body implements the function

Show the output of the following code: 1 def main(): 2 print(min(5, 6)) 3 4 def min(n1, n2): 5 smallest = n1 6 if n2 < smallest: 7 smallest = n2 8 9 main() # Call the main function

The min function mistakenly deos not return smallest. By default, it returns None. So the output is None from Line 2.

What error will occur when you run the following code? def main(): print(min(min(5, 6), min(51, 6))) def min(n1, n2): smallest = n1 if n2 < smallest: smallest = n2 main() # Call the main function

The min function should return a value.

What is the return value from invoking p(77)? def p(n): for i in range(2, n): if n % i == 0: return True return False

True

True or false? A call to a function with a None return type is always a statement itself, but a call to a value-returning function cannot be a statement by itself.

True: a call to a function with a None return type is always a statement itself. False: a call to a value-returning function is always a component of an expression.

Compare positional arguments and keyword arguments.

Using positional arguments requires the arguments be passed in the same order as their respective parameters in the function header. You can also call a function using keyword arguments, passing each argument in the form name=value.

Which of the following should be defined as a None function?

Write a function that prints integers from 1 to 100. Hint: A function that performs some actions and does not return any value should be a None function.

Suppose a function header is as follows: def f(p1, p2, p3, p4): Which of the following calls are correct?

f(1, p2 = 3, p3 = 4, p4 = 4) # Correct f(1, p2 = 3, 4, p4 = 4) # Not Correct f(p1 = 1, p2 = 3, 4, p4 = 4) # Not Correct f(p1 = 1, p2 = 3, p3 = 4, p4 = 4) # Correct f(p4 = 1, p2 = 3, p3 = 4, p1 = 4) # Correct

Whenever possible, you should avoid using __________.

global variables

Given the following function, what will be displayed by the call nPrint('a', 4)? def nPrint(message, n): while n > 0: print(message, end = '') n -= 1

infinite loop Hint: Note that n -= 1 is outside of the loop.

Consider the following incomplete code: def f(number): # Missing function body # return the value number print(f(5)) The missing function body should be ________.

return number Hint: print(f(5)) is to print a value returned from f(5). (B) and (C) are not appropriate. (A) returns a string "number". (D) is correct to return 5.

The header of a function consists of ____________.

the keyword def, followed by the function?s name and parameters, and ends with a colon.


Ensembles d'études connexes

Chapter 9 - Conducting Marketing Experiments

View Set

4: Section 3: Business Types (All Practice Checkpoints)

View Set

Chapter 6: Saving, Investment, and the Financial System

View Set

Ch 4 - Managing the MIS Function

View Set

Ch 6 Communication Prep U, CHAP 6 PSYC, Prep U Ch. 6: Therapeutic Communication - ML5

View Set

LAW 231- Comprehensive Exam 2 (Part 2)

View Set

The Four Mechanisms By Which Heat is Lost

View Set

Intro to Psych Final Exam Study Set

View Set