610 likes | 641 Vues
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.
E N D
CSE 115 Introduction to Computer Science I
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
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
Today's Plan Python review JavaScript Expressions, variables, assignments Functions JavaScript on repl.it
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
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)
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
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
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
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
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
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
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
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
Today's Plan Python review JavaScript Expressions, variables, assignments Functions JavaScript on repl.it
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"
Boolean literals • When written, must use: • true • false
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
Operators • Operatorssimilar to those in Python
Important to Remember • ==comparesvalues • but • =assigns variable
Operators • Most operatorssimilar to those in Python
Operators • Most operatorssimilar to those in Python
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
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
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
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
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
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
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
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;
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
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
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 */
Today's Plan Python review JavaScript Expressions, variables, assignments Functions JavaScript on repl.it
Defining functions Functions contain same parts as before header functionaverageOfThree( x, y, z ) {let average = (x + y + z) / 3; return average; } body delimiters
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
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
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
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
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
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 {
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
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 {
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 _
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
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!
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 {
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
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{