1 / 60

CSE 115

CSE 115. Introduction to Computer Science I. Announcements. Must have 3 stars on both Module 1's Calling Functions & Defining Functions to take THIS week's lab Must have 3 stars on Module 1 PreLab to take NEXT week's LAB EXAM. Announcements.

edgell
Télécharger la présentation

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 • Must have 3 stars on bothModule 1's Calling Functions&Defining Functionsto take THIS week's lab • Must have 3 stars onModule 1 PreLabto take NEXTweek's LAB EXAM

  3. Announcements • Next week's lab exam during 1st half of labNothing happening afterward; lab dark for 2nd half • Schedule differs for future lab exams1st half for that module's lab exam • Make-up of previous module exam in 2nd half of lab • Still need to earn stars to take make-up exam • Will use higher of exam & make-up score

  4. Today's Plan Python review JavaScript Expressions, variables, assignments Functions JavaScript on repl.it

  5. REVIEW REVIEW REVIEW REVIEW REVIEW REVIEW REVIEW

  6. REVIEW REVIEW REVIEW REVIEW REVIEW REVIEW REVIEW Writing a Compound Expression Subgoals • exp1) Determine the left-hand side of the expression • a) Write the left-hand subexpressionb) Determine the type for the value this subexpression evaluates to • exp2) Write the operator • exp3) Determine the right-hand side of the expression • a) Write the right-hand subexpressionb) Determine the type for the value this subexpression evaluates to • exp4) Verify operator valid for subexpressions' types

  7. REVIEW REVIEW REVIEW REVIEW REVIEW REVIEW REVIEW Expressions Simple • Atomic (cannot decompose)(cannot have subexpressions) • Often literal valueint (e.g., 4037, 4037) • float (e.g. 3.1415, 4.0) • boolean (True, False) • str (e.g. 'Y U',"Y not") • Can be a variableprice_per_gallon Compound Must be able to decompose(always has subexpressions) Often contains 2 subexpressions & 1 operator12 * 1'Hi '+"Mom" 56 // 12 4.5 ** 2 Can be a function callarea(w,h)

  8. REVIEW REVIEW REVIEW REVIEW REVIEW REVIEW REVIEW Writing an Assignment Subgoals • assign1) Write the name of the variable • assign2) Write the assignment operator • assign3) Write the expression whose value will be assigned to the variable

  9. REVIEW REVIEW REVIEW REVIEW REVIEW REVIEW REVIEW Variables Assignment name=expression assign1) namewritten on left-handsideof assignment operator assign3) expressionwritten onright-handsideof assignment operator assign2) write the assignment operator

  10. 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

  11. 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

  12. 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

  13. 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

  14. REVIEW REVIEW REVIEW REVIEW REVIEW REVIEW REVIEW Writing a Function Definition Subgoals • fd1) Write comment describing result of evaluating call to this 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 for 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) assigned values BEFORE function begins; you should not reassign these valuesb) If call to this function evaluates to a value, make certain that each path through function body ends at a return whose expression evaluates to that value

  15. REVIEW REVIEW REVIEW REVIEW REVIEW REVIEW REVIEW Defining functions Parts of Function Definition def keyword parameter list function name def averageOfThree( x, y, z ): average = (x + y + z) / 3 return average indentation code suite

  16. Today's Plan Python review JavaScript Expressions, variables, assignments Functions JavaScript on repl.it

  17. Expressions • Simple expressions in JSsimilar to Python • number literals examples: 99 , 3.1415, 6.02214e23, -35 • boolean literalsfalsetrue • string literalsexamples: "This is text", 'Also text', 'A', "A"

  18. Boolean literals • When written, must use: • true • false

  19. Expressions • JS compound expressions also familiar • Compound expression containssubexpressions and an operator • Already familiar applying binaryoperator(has 2 subexpressions) e.g., Binary subtraction operator: 43 – 5 e.g., Binary addition operator: 12 + 6

  20. Operators • Operatorssimilar to those in Python

  21. Important to Remember • ==comparesvalues • but • =assigns variable

  22. Operators • Most operatorssimilar to those in Python

  23. Operators • Most operatorssimilar to those in Python

  24. Writing a Compound Expression Subgoals • exp1) Determine the left-hand side of the expression • a) Write the left-hand subexpressionb) Determine the type for the value this subexpression evaluates to • exp2) Write the operator • exp3) Determine the right-hand side of the expression • a) Write the right-hand subexpressionb) Determine the type for the value this subexpression evaluates to • exp4) Verify operator valid for subexpressions' types

  25. Writing a Compound Expression Subgoals • exp1) Determine the left-hand side of the expression • a) Write the left-hand subexpressionb) Determine the type for the value this subexpression evaluates to • exp2) Write the operator • exp3) Determine the right-hand side of the expression • a) Write the right-hand subexpressionb) Determine the type for the value this subexpression evaluates to • exp4) Verify operator valid for subexpressions' types

  26. Which Expression Illegal? Choose 1 expression which is NOT legal JavaScript • false • 89 (34 + 34) • 'I am taking CSE115' • "Prof looks " + (2 ** 6) • 4 * (45 - 2) + 334 – 4359

  27. Which Expression Illegal? Convince Your Neighbor Your Answer Is Correct Choose 1 expression which is NOT legal JavaScript • false • 89 (34 + 34) • 'I am taking CSE115' • "Prof looks " + (2 ** 6) • 4 * (45 - 2) + 334 – 4359

  28. Which Expression Illegal? Choose 1 expression which is NOT legal JavaScript • false • 89 (34 + 34) • 'I am taking CSE115' • "Prof looks " + (2 ** 6) • 4 * (45 - 2) + 334 – 4359

  29. Which Expression Illegal? Choose 1 expression which is NOT legal JavaScript • false • 89 (34 + 34) • "I am taking CSE115' • "Prof looks " + (2 ** 6) • 4 * (45 - 2) + 334 – 4359 Still must be explicit:JavaScript cannotguess your intention

  30. Which Expression Illegal? Choose 1 expression which is NOT legal JavaScript • false • 89 (34 + 34)89 * (34 + 34) • 'I am taking CSE115' • "Prof looks " + (2 ** 6) • 4 * (45 - 2) + 334 – 4359

  31. Variables & Statements JavaScript has some larger syntax changes "Requires" most statements to end with ; letdeclaration "required" before variable can be used • let x;x = 13;let oneLine = 'Hi, Mom';let y, x = 13, z;y = 'Hi mom';z = x;x = 45;

  32. Writing an Assignment Subgoals • assign1) Write the name of the variable • assign2) Write the assignment operator • assign3) Write the expression whose value will be assigned to the variable

  33. Writing an Assignment Subgoals • assign1) If first use of variable in function, write let keyword • assign2) Write the name of the variable • assign3) Write the assignment operator • assign4) Write the expression whose value will be assigned to the variable

  34. Comments • JavaScript has 2 different formats • # This is a Python single-line comment • // This is a JavaScript single-line comment • /* This is a JavaScript comment that spans many lines. */ • /* Another JavaScript comment on 1 line */

  35. Today's Plan Python review JavaScript Expressions, variables, assignments Functions JavaScript on repl.it

  36. Defining functions Functions contain same parts as before header functionaverageOfThree( x, y, z ) {let average = (x + y + z) / 3; return average; } body delimiters

  37. Defining functions Only keyword & delimiters change… function keyword parameter list function name functionaverageOfThree( x, y, z ){ let average = (x + y + z) / 3; return average; } code block delimiters

  38. Writing a Function Definition Subgoals • fd1) Write comment describing result of evaluating call to this 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 for 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) assigned values BEFORE function begins; you should not reassign these valuesb) If call to this function evaluates to a value, make certain that each path through function body ends at a return whose expression evaluates to that value

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

  40. Defining functions … and indentation "optional" function keyword parameter list function name functionaverageOfThree( x, y, z ){ let average = (x + y + z) / 3; return average; } code block delimiters

  41. Defining functions … and indentation "optional" function keyword parameter list function name functionaverageOfThree( x, y, z ){ let average = (x + y + z) / 3; return average; } code block delimiters

  42. Select Correct Answer(s) • Select the legal JavaScript function headers(including opening delimiter) • a. function 12Times(x) { • b. Function doWork(x,y) { • c. function createFile(){ • d. function foo(av,d,c) { • e. function noParams {

  43. Select Correct Answer(s) • Select the legal JavaScript function headers(including opening delimiter) • a. function 12Times(x) { • b. Function doWork(x,y) { • c. function createFile(){ • d. function foo(av,d,c) { • e. function noParams { Convince Your Neighbor Your Answer Is Correct

  44. Select Correct Answer(s) • Select the legal JavaScript function headers(including opening delimiter) • a. function 12Times(x) { • b. Function doWork(x,y) { • c. function createFile(){ • d. function foo(av,d,c) { • e. function noParams {

  45. Select Correct Answer(s) • Select the legal JavaScript function headers(including opening delimiter) • a. function 12Times(x) { • b. Function doWork(x,y) { • c. function createFile(){ • d. function foo(av,d,c) { • e. function noParams { Just like with variables, function names must begin with letteror _

  46. Select Correct Answer(s) • Select the legal JavaScript function headers(including opening delimiter) • a.function 12Times(x) { • b. Function doWork(x,y) { • c. function createFile(){ • d. function foo(av,d,c) { • e. function noParams { • Capitals matter Function headermust start with function

  47. Select Correct Answer(s) • Select the legal JavaScript function headers(including opening delimiter) • a.function 12Times(x) { • b.FunctiondoWork(x,y) { • c. function createFile(){ • d. function foo(av,d,c) { • e. function noParams { • ParametersNOTrequired but parentheses are!

  48. Select Correct Answer(s) • Select the legal JavaScript function headers(including opening delimiter) • a.function 12Times(x) { • b.FunctiondoWork(x,y) { • c. function createFile(){ • d. function foo(av,d,c) { • e. function noParams {

  49. Select Correct Answer(s) • Select the legal JavaScript function headers(including opening delimiter) • a.function 12Times(x) { • b.FunctiondoWork(x,y) { • c. function createFile(){ • d. function foo(av,d,c) { • e. function noParams{ • Parameters NOTrequired, but parenthesesrequired

  50. Select Correct Answer(s) • Select the legal JavaScript function headers(including opening delimiter) • a.function12Times(x) { • b.FunctiondoWork(x,y) { • c. function createFile(){ • d. function foo(av,d,c) { • e. function noParams{

More Related