1 / 12

Functions … return values

Functions … return values. # sample code def gross_pay ( pay_per_hour , hours_worked ): pay = pay_per_hour * hours_worked line of code pretax_pay = gross_pay (10.50, 10.0) print ( pretax_pay ) What if line of code is: What is returned? Type? return pay return

dean-hart
Download Presentation

Functions … return values

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Functions … return values # sample code def gross_pay (pay_per_hour, hours_worked): pay = pay_per_hour * hours_worked line of code pretax_pay = gross_pay(10.50, 10.0) print (pretax_pay) What if line of code is: What is returned?Type? return pay return is empty,i.e. no return

  2. Variable Scope …where do your names live? • variable scope – where the variable is created, changed, or accessed • when we only had a main program, • there was only one namespace • all variable were in that one namespace • when we create a function, • the function has its own namespace • variable assigned in the function can only be seen by the code in that function

  3. Variable Scope …where do your names live? def cube (x):      value = x * x * x      return value value = 25 cubed = cube(2) print (cubed, value)

  4. Variable Scope …where do your names live? def clear_count(count): count = 0 return order_num = 15 clear_count(order_num) print(order_num)

  5. Variable Scope …where do your names live? def clear_count(count): count = 0 return count = 15 clear_count(count) print(count)

  6. Variable Scope …where do your names live? topping = "whipped cream“ def order_ice_cream(flavour): topping = flavour return order_ice_cream("chocolate") print (topping)

  7. Variable Scope …where do your names live? topping = "whipped cream“ def order_ice_cream(flavour): print("Mmm,", flavour, "with", topping) return order_ice_cream("chocolate")

  8. Variable Scope …where do your names live? If main and functions in same file: If main and functions in different files: variable inside function is local to the function variables inside the main are not visible to the functions separate files make good fences (enforces no peeking) • variable inside function is local to the function • variables inside the main are visible to the functions • be a good neighbour, do not peek.

  9. Calculate area and volume of cylinder [A3, Q1] PI = 3.1416 radius = float(input(“Enter cylinder radius: “)) height = float(input(“Enter cylinder height: “)) area = PI * radius**2 volume = area * height print(“Cylinder area = {:.2f}”.format(area)) print(“Cylinder volume = {:.2f}”.format(volume))

  10. PI = 3.1416 radius = float(input(“Enter cylinder radius: “)) height = float(input(“Enter cylinder height: “)) print(“Cylinder area = {:.2f}”.format(area)) print(“Cylinder volume = {:.2f}”.format(volume))

  11. Lumens given bulb wattage [A6, Q1] • watts = int(input(“Enter watts: “)) • if watts == 15: • lumens = 125 • elif watts == 25: • lumens = 215 • elif watts == 40: • lumens = 500 • elif watts == 60: • lumens = 880 • elif watts == 75: • lumens = 1000 • elif watts == 100: • lumens = 1675 • else: • lumens = None • print() • if lumens == None: • print(“Invalid wattage”) • else: • print(“For a bulb wattage of {} watts, brightness is {} lumens” .format(watts, lumens))

  12. Lumens given bulb wattage [A6, Q1] • if watts == 15: • lumens = 125 • elif watts == 25: • lumens = 215 • elif watts == 40: • lumens = 500 • elif watts == 60: • lumens = 880 • elif watts == 75: • lumens = 1000 • elif watts == 100: • lumens = 1675 • else: • lumens = None • watts = int(input(“Enter watts: “)) • print() • if lumens == None: • print(“Invalid wattage”) • else: • print(“For a bulb wattage of {} watts, brightness is {} lumens” .format(watts, lumens))

More Related