30
Fruitful functions

Fruitful functions

  • Upload
    andra

  • View
    37

  • Download
    0

Embed Size (px)

DESCRIPTION

Fruitful functions. Return values. The built-in functions we have used, such as abs, pow , int , max, and range, have produced results. Calling each of these function generates a value, which usually assign to a variable or use as part of an expression. Return values. - PowerPoint PPT Presentation

Citation preview

Page 1: Fruitful functions

Fruitful functions

Page 2: Fruitful functions

Return values

• The built-in functions we have used, such as abs, pow, int, max, and range, have produced results.

• Calling each of these function generates a value, which usually assign to a variable or use as part of an expression.

Page 3: Fruitful functions

Return values

• We also wrote our own function to return the final amount for a compound interest calculation.

Page 4: Fruitful functions

Returns value

In a fruitful function the return statement includes a return value, it means : evaluate the return expression.

Page 5: Fruitful functions

Multiple return statements

• Sometimes it is useful to have multiple return statements.

Page 6: Fruitful functions

Multiple return statements

• Another way to write the previous function

Page 7: Fruitful functions

Multiple return statements• It’s good idea to ensure that every possible

path through the program hits a return statement. The following function fails to do this

Page 8: Fruitful functions

A return statements in the middle of a for loop

Page 9: Fruitful functions

Program development

• Suppose we want to find the distance between two points, given by the coordinates(x1,y1) and (x2,y2)

• By the pythagorean theorem ,the distance is :

distance = √(x2-x1)2 +(y2-y1)2

Page 10: Fruitful functions

The distance between two points

• The first step is to consider what a distance function should look like. In other words, what are the inputs (parameters) and what is the output (return value)?

• In this case, the two points are inputs, which can represent using four parameters. The return value is the distance, which is the floating-point value.

Page 11: Fruitful functions

An outline of the function

At this point we have confirmed that the function is syntactically correct.

Page 12: Fruitful functions

Adding lines of codeA logical first step in the computation is to find the difference x2 - x1 and y2 - y1

Page 13: Fruitful functions

Compute the sum of squares of dx and dy

Page 14: Fruitful functions

Compute and return the result

Page 15: Fruitful functions

Another version of the function

Page 16: Fruitful functions

The key aspects of the process are:

• Start with a working skeleton program and make small incremental changes. At any point, if there is an error, we will know exactly where it is.

• Use temporary variables to refer to intermediate values so that we can easily inspect and check them.

• One the program is working, play around with options

Page 17: Fruitful functions

Debugging with print

• Another powerful technique for debugging is to insert extra print functions in carefully selected places in code. By inspecting the output of the program, we can check whether the algorithm is doing what we expect it to.

Page 18: Fruitful functions

Composition

• Composition is the ability to call one function from within another.

Page 19: Fruitful functions

Composition

Page 20: Fruitful functions

Composition

• The temporary variables radius and result are useful for development, debugging, and single-stepping through the code to inspect what is happening, but once the program is working, we can make it more concise by composing the function calls:

Page 21: Fruitful functions
Page 22: Fruitful functions

Boolean functions• Function can return Boolean values, which is

convenient for hiding complicated tests inside function

Page 23: Fruitful functions

Boolean Function

Page 24: Fruitful functions

Program with style• Use 4 spaces for indentation• Limit line length to 78 characters• Use CamelCase for classes• Use lowercase_with_underscores for functions

and variables• Place imports at the top of the file• keep function definitions together• Use two blank lines to separate function

definitions from each other

Page 25: Fruitful functions

Unit testing

• It is best practice in software development to include automatic unit testing of source code.

Page 26: Fruitful functions

Unit testing

• Unit testing provide a way to automatically verify that individual pieces of code, such as functions, are working properly.

• This makes it possible to change the implementation of a function at a later time and quickly tests.

Page 27: Fruitful functions

Unit testing

• Unit testing also forces the programmer to think about the different cases that the function need to handle.

• You also only have to type the tests once into the script, rather than having to keep entering the same test data over and over as you develop your code.

Page 28: Fruitful functions

Test suite

• A collection of tests for some code is called its test suite.

Page 29: Fruitful functions
Page 30: Fruitful functions