Chapter 5: Functions

¡Supera tus tareas y exámenes ahora con Quizwiz!

Write the definition of a function typing_speed, that receives two parameters. The first is the number of words that a person has typed (an int greater than or equal to zero) in a particular time interval. The second is the length of the time interval in seconds (an int greater than zero). The function returns the typing speed of that person in words per minute (a float).

def typing_speed(num_words,time_interval): num_words>=0 time_interval>0 result=float(60*num_words/time_interval ) return result

Write the definition of a function absoluteValue that recieves a parameter containing an integer value and returns the absolute value of that parameter.

def absoluteValue(x): return abs(x)

Write the definition of a function add which recieves two parameters containing integer values and returns their sum.

def add(int1,int2): return int1 + int2

Write the definition of a function add, that receives two int parameters and returns their sum.

def add(int1,int2): return int1+int2

Write the definition of a function half which recieves a variable containing an integer as a parameter, and returns another variable containing an integer, whose value is closest to half that of the parameter. (Use integer division!)

def half(x): return int(x/2)

Define a function called hasRealSolution that takes three parameters containing integer values: a, b, and c. If "b squared" minus 4ac is negative, the function returns False otherwise, it returns True.

def hasRealSolution(a,b,c): if b*2-4*a*c<0: return False else: return True

Define a function called isEven that takes a parameter containing an integer value and returns True if the parameter is even, and False otherwise.

def isEven(int): if int%2==0: return True else: return False

Define a function called isPositive that takes a parameter containing an integer value and returns True if the paramter is positive or False if the parameter is negative or 0.

def isPositive(x): return(x>0)

Define a function called isSenior that takes a parameter containing an integer value and returns True if the parameter is greater than or equal to 65, and False otherwise.

def isSenior(age): if age>=65: return True else: return False

Write the definition of a function max that has three int parameters and returns the largest.

def largest(num1,num2,num3): max(num1,num2,num3)

Define a function called mymin that takes two parameters containing integer values and returns the smaller integer of the two. If they have equal value, return either one.

def mymin(int1,int2): if int1<int2: return int1 elif int2<int1: return int2 else: return int1 or int2

Write a function mymin that has two str parameters and returns the smallest. (Smallest in the sense of coming first alphabetically, not in the sense of "shortest".)

def mymin(str1,str2): if str1 < str2: return str1 else: return str2

Write the definition of a function oneLess which recieves a parameter containing an integer value and returns an integer whose value is one less than the value of the parameter.

def oneLess(int): return int-1

Write the definition of a function oneMore which recieves a parameter containing an integer value and returns an integer that is one more than the value of the parameter.

def oneMore(int): return int+1

Write a definition of the function printDottedLine, which has no parameters and doesn't return anything. The function prints to standard output a single line consisting of 5 periods (".").

def printDottedLine(): print(".....")

Write the definition of a function printGrade, which takes one parameter containing a string value and returns nothing. The function prints "Grade: " followed by the string parameter.

def printGrade(grade): print("Grade:",grade)

Define a function called signOf that takes a parameter containing an integer value and returns a 1 if the parameter is positive, 0 if the parameter is 0, and -1 if the parameter is negative.

def signOf(x): if x>0: return 1 elif x == 0: return 0 else: return -1

Write the definition of a function square which recieves a parameter containing an integer value and returns the square of the value of the parameter.

def square(int): return int*int

Write the definition of a function twice, that receives an int parameter and returns an int that is twice the value of the parameter.

def twice(int): return 2*int

Given print_larger, a function that expects two parameters and returns no value and given two variables, sales1 and sales2, that have already been defined, write a statement that calls print_larger, passing it sales1 and sales2.

print_larger(sales1, sales2)

Assume that print_todays_date is a function that uses no parameters and returns no value. Write a statement that calls (invokes) this function.

print_todays_date()

Assume that to_the_power_of is a function that expects two int parameters and returns the value of the first parameter raised to the power of the second parameter. Write a statement that calls to_the_power_of to compute the value of cube_side raised to the power of 3 and that associates this value with cube_volume.

cube_volume = to_the_power_of(cube_side, 3)

Given that add, a function that expects two int parameters and returns their sum, and given that two variables, euro_sales and asia_sales, have already been defined: Write a statement that calls add to compute the sum of euro_sales and asia_sales and that associates this value with a variable named eurasia_sales.

eurasia_sales=add(euro_sales,asia_sales)

Assume that max2 is a function that expects two int parameters and returns the value of the larger one. Also assume that four variables, population1, population2, population3, and population4 have already been defined and associated with int values. Write an expression (not a statement!) whose value is the largest of population1, population2, population3, and population4 by calling max2.

max(max(population1,population2),max(population3,population4))

max is a function that expects two int parameters and returns the value of the larger one. Two variables, population1 and population2, have already been defined and associated with int values. Write an expression (not a statement!) whose value is the larger of population1 and population2 by calling max.

max(population1,population2)

Assume that print_error_description is a function that expects one int parameter and returns no value. Write a statement that invokes the function print_error_description, passing it the value 14.

print_error_description(14)

Write the code to call a function whose name is send_number. There is one argument for this function, which is an int. Send 5 as an argument to the function.

send_number(5)

Write the code to call a function named send_object and that expects one parameter, of type Customer. Suppose there is an object of type Customer, referred to by John_Doe. Use this object as an argument to the function.

send_object(John_Doe)

Write the code to call the function named send_signal. There are no parameters for this function.

send_signal()

Write the code to call a function named send_two and that expects two parameters: a float and an int. Invoke this function with 15.955 and 133 as arguments.

send_two(15.955, 133)

Write the code to call a function named send_variable and that expects a single int parameter. Suppose a variable called x refers to an int. Pass this variable as an argument to send_variable.

send_variable(x)


Conjuntos de estudio relacionados

Diffuse Parenchymal Lung Diseases

View Set

Network+ Guide to Networks Chapter 5 - RQ

View Set

AP Psychology Test 1: Motivation, Emotion, and Personality

View Set

Gross 1: Brachial Plexus, shoulder and arm

View Set

Chapter 12: The Age of the Renaissance

View Set