1 / 80

CSE 115

CSE 115. Introduction to Computer Science I. Announcements. Graded labs & recitations starts next week Must attend YOUR section ; system limits access Lab work must be completed in Baldy 21 during your lab

hcooper
Download Presentation

CSE 115

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. CSE 115 Introduction to Computer Science I

  2. Announcements • Graded labs & recitations starts next weekMust attend YOUR section; system limits accessLab work must be completed in Baldy 21 during your lab • Must have 3 stars on bothModule 1's Expressions&VariablesBEFORE your lab next weekEnforced by the system; TAs cannot do anything about itCannot sit in lab to complete; TAs must ask you to leave

  3. Today's Plan Review Defining functions Calling functions Function calls as expressions

  4. REVIEW REVIEW REVIEW REVIEW REVIEW REVIEW REVIEW Writing a Function Call Expression Subgoals • fc1) Write the function name • fc2) Write the argument lista) Start the argument list with an open parenthesisb) For each function input in the order they are listed, write the expression whose value is used for that inputc) Write a comma between each pair of argumentsd) End the argument list with a close parenthesis

  5. REVIEW REVIEW REVIEW REVIEW REVIEW REVIEW REVIEW Function Calls Examples pow( 3 , 2 ) min( 16/3, 4 ) x = -6abs(x) print( "Hi there!" ) fc1) Write the functionname

  6. REVIEW REVIEW REVIEW REVIEW REVIEW REVIEW REVIEW Function Calls • Examples • pow(3,2) • min(16/3,4) • x = -6abs(x) • print( "Hi there!" ) fc2)Write the argumentlist

  7. REVIEW REVIEW REVIEW REVIEW REVIEW REVIEW REVIEW Function Calls Input toabs is NOTx x's valueinput to abs Examples • x = -6y = abs(x)Starts by assigning x a value of -6Evaluates simple expression x; result of evaluation is value of -6Uses-6as input to absolute value; result of evaluation is 6Assigns y result of evaluating function call expression

  8. Which Lines Legal? 1 import math2answer = 423six = answer / 64smaller = min(answer, six * 9)5num = math.sin("180")6zero = abs(math.cos(math.pi))7print(max(max(5, zero), smaller))

  9. Which Lines Legal? Convince Your Neighbor Your Answer Is Correct 1 import math2answer = 423six = answer / 64smaller = min(answer, six * 9)5num = math.sin("180")6zero = abs(math.cos(math.pi))7print(max(max(5, zero), smaller))

  10. Which Lines Legal? 1 import math2answer = 423six = answer / 64smaller = min(answer, six * 9)5num = math.sin("180")6zero = abs(math.cos(math.pi))7print(max(max(5, zero), smaller))

  11. Which Lines Legal? 1 import math2answer = 423six = answer / 64smaller = min(answer, six * 9)5num = math.sin("180")6zero = abs(math.cos(math.pi))7print(max(max(5, zero), smaller))

  12. Which Lines Legal? 1 import math2answer = 423six = answer / 64smaller = min(answer, six * 9)5num = math.sin("180")6zero = abs(math.cos(math.pi))7print(max(max(5, zero), smaller))

  13. Which Lines Legal? 1 import math2answer = 423six = answer / 64smaller = min(answer, six * 9)5num = math.sin("180")6zero = abs(math.cos(math.pi))7print(max(max(5, zero), smaller))

  14. Which Lines Legal? 1 import math2answer = 423six = answer / 64smaller = min(answer, six * 9)5num = math.sin("180")6zero = abs(math.cos(math.pi))7print(max(max(5, zero), smaller))

  15. Which Lines Legal? 1 import math2answer = 423six = answer / 64smaller = min(answer, six * 9)5num = math.sin("180")6zero = abs(math.cos(math.pi))7print(max(max(5, zero), smaller))

  16. Which Lines Legal? 1 import math2answer = 423six = answer / 64smaller = min(answer, six * 9)5num = math.sin("180")6zero = abs(math.cos(math.pi))7print(max(max(5, zero), smaller))

  17. Which Lines Legal? 1 import math2answer = 423six = answer / 64smaller = min(answer, six * 9)5num = math.sin("180")6zero = abs(math.cos(math.pi))7print(max(max(5, zero), smaller))

  18. Which Lines Legal? 1 import math2answer = 423six = answer / 64smaller = min(answer, six * 9)5num = math.sin("180")6zero = abs(math.cos(math.pi))7print(max(max(5, zero), smaller)) π

  19. Which Lines Legal? 1 import math2answer = 423six = answer / 64smaller = min(answer, six * 9)5num = math.sin("180")6zero = abs(math.cos(math.pi))7print(max(max(5, zero), smaller)) value of typefloat

  20. Which Lines Legal? 1 import math2answer = 423six = answer / 64smaller = min(answer, six * 9)5num = math.sin("180")6zero =abs(math.cos(math.pi))7print(max(max(5, zero), smaller)) value that must be a number

  21. Which Lines Legal? 1 import math2answer = 423six = answer / 64smaller = min(answer, six * 9)5num = math.sin("180")6zero = abs(math.cos(math.pi))7print(max(max(5, zero), smaller))

  22. Which Lines Legal? 1 import math2answer = 423six = answer / 64smaller = min(answer, six * 9)5num = math.sin("180")6zero = abs(math.cos(math.pi))7print(max(max(5, zero), smaller))

  23. Which Lines Legal? 1 import math2answer = 423six = answer / 64smaller = min(answer, six * 9)5num = math.sin("180")6zero = abs(math.cos(math.pi))7print(max(max(5, zero), smaller))

  24. Which Lines Legal? 1 import math2answer = 423six = answer / 64smaller = min(answer, six * 9)5num = math.sin("180")6zero = abs(math.cos(math.pi))7print(max(max(5, zero), smaller)) some number

  25. Which Lines Legal? 1 import math2answer = 423six = answer / 64smaller = min(answer, six * 9)5num = math.sin("180")6zero = abs(math.cos(math.pi))7print(max(max(5, zero), smaller)) value that must be some number

  26. Which Lines Legal? 1 import math2answer = 423six = answer / 64smaller = min(answer, six * 9)5num = math.sin("180")6zero = abs(math.cos(math.pi))7print(max(max(5, zero), smaller))

  27. Which Lines Legal? 1 import math2answer = 423six = answer / 64smaller = min(answer, six * 9)5num = math.sin("180")6zero = abs(math.cos(math.pi))7print(max(max(5, zero), smaller))

  28. Today's Plan Review Defining functions Calling functions Function calls as expressions

  29. Defining functions Python includes lots of built-in functionsNot everything we need is already defined by functions Python allows us to define our own functionsCalled and used identically to the built-in ones

  30. Naming Key Concept • Starts with letteror _ • Then any combination of letters,numbers, or _ • Always make it meaningful

  31. Writing a Function Subgoals • fd1) Write comment describing result of evaluating function call (or what function does, if not returning a value) • fd2) Write the function header & delimitersa) Write the def keywordb) Write the function's namei) Choose name reflecting function's SINGLE purposec) Write the parameter list of the function's input(s)i) Choose name(s) expressing value of eachd) Write the function delimiter at the end of the header • fd3) Write the function bodya) Parameter(s) are assigned values BEFORE function begins; you should not reassign these valuesb) If returning a value, check that each path through the function body ends at a return whose expression evaluates to that value

  32. Defining functions Example # Calculates mean of the inputs def averageOfThree( x, y, z ) : average = (x + y + z) / 3return average

  33. Defining functions Parts of a Function comment # Calculates mean of the inputs def averageOfThree( x, y, z ) : delimiter average = (x + y + z) / 3return average header body

  34. Defining functions Comment explaining function ` fd1) Write a comment describing effect of calling this function # Calculates mean of the inputs def averageOfThree( x, y, z ) : average = (x + y + z) / 3return average

  35. Defining functions Function header & delimiter ` fd2a) Write the def keyword # Calculates mean of the inputs ( x, y, z ) averageOfThree def : average = (x + y + z) / 3return average

  36. Defining functions • Function header & delimiter ` fd2b) Write the function's name # Calculates mean of the inputs ( x, y, z ) averageOfThree def : average = (x + y + z) / 3return average

  37. Defining functions • Function header & delimiter ` fd2c) Write the parameter listing for the function's inputs # Calculates mean of the inputs ( x, y, z ) averageOfThree def : average = (x + y + z) / 3return average

  38. Defining functions • Function header & delimiter ` fd2c) Write the parameter listing for the function's inputs # Calculates the number 72 ( ) seventyTwo def : return 72 Even if function has no inputs, parentheses required

  39. Defining functions • Parameter listing ` Will need 3 arguments to call this function.Inputs used in call assigned to variables named x,y, &z # Calculates mean of the inputs ( x, y, z ) averageOfThree def : average = (x + y + z) / 3return average

  40. Defining functions Parameter listing ` Commas neededbetween parameters # Calculates mean of the inputs ( x, y, z ) averageOfThree def : average = (x + y + z) / 3return average

  41. Defining functions • Function header & delimiter ` fd2d) Write the function delimiter at the end of the header # Calculates mean of the inputs ( x, y, z ) averageOfThree def : average = (x + y + z) / 3return average

  42. Select Correct Answer(s) • Select the legal Python function headers(including delimiter) • def 12Times(x) : • Def doWork(x,y) : • def create_file() : • def foo(av,d,c) : • def no_params :

  43. Select Correct Answer(s) • Select the legal Python function headers(including delimiter) • def 12Times(x) : • Def doWork(x,y) : • def create_file() : • def foo(av,d,c) : • def no_params : Convince Your Neighbor Your Answer Is Correct

  44. Select Correct Answer(s) Select the legal Python function headers(including delimiter) def 12Times(x) : Def doWork(x,y) : def create_file() : def foo(av,d,c) : def no_params :

  45. Select Correct Answer(s) • Select the legal Python function headers(including delimiter) • def 12Times(x) : • Def doWork(x,y) : • def create_file() : • def foo(av,d,c) : • def no_params : Just like with variables, function names must begin with letteror _

  46. Select Correct Answer(s) • Select the legal Python function headers(including delimiter) • def 12Times(x) : • Def doWork(x,y) : • def create_file() : • def foo(av,d,c) : • def no_params : • Capitals matter Function headermust start with def

  47. Select Correct Answer(s) • Select the legal Python function headers(including delimiter) • def 12Times(x) : • Def doWork(x,y) : • def create_file() : • def foo(av,d,c) : • def no_params : • ParametersNOTrequired but parentheses are!

  48. Select Correct Answer(s) • Select the legal Python function headers(including delimiter) • def 12Times(x) : • Def doWork(x,y) : • def create_file() : • def foo(av,d,c) : • def no_params :

  49. Select Correct Answer(s) • Select the legal Python function headers(including delimiter) • def 12Times(x) : • Def doWork(x,y) : • def create_file() : • def foo(av,d,c) : • def no_params: • Parameters NOTrequired, but parenthesesrequired

  50. Select Correct Answer(s) • Select the legal Python function headers(including delimiter) • def 12Times(x) : • Def doWork(x,y) : • def create_file() : • def foo(av,d,c) : • def no_params: (Subgoalfd2b) ) (Subgoalfd2a)) (Subgoalfd2c))

More Related