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.

Slides:



Advertisements
Similar presentations
Methods Java 5.1 A quick overview of methods
Advertisements

Python Programming Chapter 5: Fruitful Functions Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
FIT FIT1002 Computer Programming Unit 19 Testing and Debugging.
Introduction to C Programming
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
Chapter 2: Algorithm Discovery and Design
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
 2007 Pearson Education, Inc. All rights reserved C Program Control.
Introduction to C Programming
ECE122 L17: Method Development and Testing April 5, 2007 ECE 122 Engineering Problem Solving with Java Lecture 17 Method Development and Testing.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Chapter 2: Algorithm Discovery and Design
Intro to Robots Conditionals and Recursion. Intro to Robots Modulus Two integer division operators - / and %. When dividing an integer by an integer we.
Fundamentals of Python: From First Programs Through Data Structures
DCT 1123 PROBLEM SOLVING & ALGORITHMS INTRODUCTION TO PROGRAMMING.
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
Fundamentals of Python: First Programs
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Simple Program Design Third Edition A Step-by-Step Approach
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
ICAPRG301A Week 4Buggy Programming ICAPRG301A Apply introductory programming techniques Program Bugs US Navy Admiral Grace Hopper is often credited with.
General Programming Introduction to Computing Science and Programming I.
Testing Michael Ernst CSE 140 University of Washington.
An Introduction to Programming and Algorithms. Course Objectives A basic understanding of engineering problem solving process. A basic understanding of.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
 Expression Tree and Objects 1. Elements of Python  Literals, Strings, Tuples, Lists, …  The order of file reading  The order of execution 2.
Testing and Debugging Version 1.0. All kinds of things can go wrong when you are developing a program. The compiler discovers syntax errors in your code.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
Program Errors and Debugging Week 10, Thursday Lab.
6 Chapter 61 Looping Programming Logic and Design, Second Edition, Comprehensive 6.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
CS101 Computer Programming I Chapter 4 Extra Examples.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
1 Program Input Software Design Chapter 4. 2 You Will Want to Know... Prompting for and reading values into a program. Accessing data from a file. What.
ITI 1120 Lab #5 Contributors: S. Boyd, R. Plesa, A. Felty, D. Inkpen, A. Williams, D. Amyot.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
Operating System Using setw and setprecision functions Using setiosflags function Using cin function Programming 1 DCT
Introduction to Functions CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Flow of Control: Loops Module 4. Objectives Design a loop Use while, do, and for in a program Use the for-each with enumerations Use assertion checks.
1 Project 7: Looping. Project 7 For this project you will produce two Java programs. The requirements for each program will be described separately on.
Functions Chapter 4 Python for Informatics: Exploring Information Slightly modified by Recep Kaya Göktaş on March 2015.
Copyright © 2014 Curt Hill Algorithms From the Mathematical Perspective.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
1 ENGI 2420 Structured Programming (Lab Tutorial 2) Memorial University of Newfoundland.
Testing i. explain the importance of system testing and installation planning;
Functions. What is a Function?  We have already used a few functions. Can you give some examples?  Some functions take a comma-separated list of arguments.
L131 Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips rand( ) math library functions Reading Sections.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Chapter 4 – C Program Control
Loop Structures.
The Selection Structure
User-Defined Functions
Assignment Operators Topics Increment and Decrement Operators
Assignment Operators Topics Increment and Decrement Operators
CSE 1020:Software Development
Using C++ Arithmetic Operators and Control Structures
Assignment Operators Topics Increment and Decrement Operators
Presentation transcript:

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 We also wrote our own function to return the final amount for a compound interest calculation.

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

Multiple return statements Sometimes it is useful to have multiple return statements.

Multiple return statements Another way to write the previous function

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

A return statements in the middle of a for loop

Program development Suppose we want to find the distance between two points, given by the coordinates(x 1,y 1 ) and (x 2,y 2 ) By the pythagorean theorem,the distance is : distance = √(x 2 -x 1 ) 2 +(y 2 -y 1 ) 2

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.

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

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

Compute the sum of squares of dx and dy

Compute and return the result

Another version of the function

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

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.

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

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:

Boolean functions Function can return Boolean values, which is convenient for hiding complicated tests inside function

Boolean Function

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

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

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.

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.

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