800 likes | 817 Views
Learn about defining functions, calling functions, and function call expressions in this introductory computer science course. Get hands-on practice in writing and executing function calls.
E N D
CSE 115 Introduction to Computer Science I
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
Today's Plan Review Defining functions Calling functions Function calls as expressions
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
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))
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))
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))
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))
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))
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))
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))
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))
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))
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))
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)) π
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
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
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))
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))
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))
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
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
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))
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))
Today's Plan Review Defining functions Calling functions Function calls as expressions
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
Naming Key Concept • Starts with letteror _ • Then any combination of letters,numbers, or _ • Always make it meaningful
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
Defining functions Example # Calculates mean of the inputs def averageOfThree( x, y, z ) : average = (x + y + z) / 3return average
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
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
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
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
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
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
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
Defining functions Parameter listing ` Commas neededbetween parameters # Calculates mean of the inputs ( x, y, z ) averageOfThree def : average = (x + y + z) / 3return average
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
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 :
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
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 :
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 _
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
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!
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 :
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
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))