Presentation is loading. Please wait.

Presentation is loading. Please wait.

Functions Part I (Syntax). What is a function? A function is a set of statements which is split off into a separate entity that can be used like a “new.

Similar presentations


Presentation on theme: "Functions Part I (Syntax). What is a function? A function is a set of statements which is split off into a separate entity that can be used like a “new."— Presentation transcript:

1 Functions Part I (Syntax)

2 What is a function? A function is a set of statements which is split off into a separate entity that can be used like a “new keyword” It has a name It should have a specific task to do (functional cohesion) It has a parameter list (data that it needs to work on for the task) It is also called a subroutine, subprogram, procedure or module in different languages

3 Why write code in functions? You can write the code once, use it many times It’s easier to divide work among the members of a team – each one is responsible for a certain function You can put off the details when you are writing the top-level code of the program “abstraction or generalization” and then later, focus on just one function at a time (top down design) You can write the program incrementally, one function at a time (bottom up design) Testing is easier (unit testing)

4 Why write code in functions? You can think of a function as a ‘black box’ – you only know what goes into it and what should come out of it, not what is going on inside This organization allows for easier changes of a function’s implementation – the function code is all that has to change Software companies often sell libraries of functions – they sell the executable code and documentation of the headers of the functions, that is, the parameter lists and the value that is returned by the function – you don’t get to see the implementation, just the interface. Later the company may implement the function library in a more efficient way – they can send you the update without your having to change your code at all!

5 A Black Box You can think of a function as a black box. Put certain things IN and certain things (or actions) come OUT. The details of what’s in the box can be postponed for a while. Black Box InputOutput

6 Syntax of functions – two parts A function has to have a definition And a function needs to be called (otherwise it is not executed)

7 Syntax of a definition of a function Starts with def identifier (parameters): at left edge of screen Statements follow the def line, indented one tab stop (the “body”) When the lines stopped being indented, the function definition is finished It may or may not end with a return statement Parameter list can be empty or as many identifiers as desired with commas between, parameters are not expressions or constants Where the function definition is placed in the source file is up to you – the main rule is that it must be defined before the first call to the function appears in the file Functions can be defined inside other function definitions. This is generally not necessary – please do not do this in this class

8 Syntax of function call (invocation) There are two kinds of calls to functions If the function returns a value, the function call must be part of a statement – it cannot stand alone Example: y = sqrt(x) Example: print(sqrt(x)) NOT correct: sqrt(x) # Python allows this to be written but it does # nothing with the result of the function, so it’s a waste of time If the function does not return a value, the function call is a stand-alone statement Example: win.close() Example: circ1.setFill(“red”) # changes the object, does not return a value Not correct: y = win.close() # y will get the value None


Download ppt "Functions Part I (Syntax). What is a function? A function is a set of statements which is split off into a separate entity that can be used like a “new."

Similar presentations


Ads by Google