CMPT 120 Topic: Functions – Part 4

Slides:



Advertisements
Similar presentations
CS101: Introduction to Computer programming
Advertisements

Chapter 3 Planning Your Solution
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
Functions, Procedures, and Abstraction Dr. José M. Reyes Álamo.
CPS120: Introduction to Computer Science Functions.
CPS120: Introduction to Computer Science Lecture 14 Functions.
1 Programming for Engineers in Python Autumn Lecture 6: More Object Oriented Programming.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
CMPT 120 Topic: Searching – Part 2 and Intro to Time Complexity (Algorithm Analysis)
Program design Program Design Process has 2 phases:
Learning outcomes 5 Developing Code – Using Flowcharts
Introduction to Computing Science and Programming I
CMPT 120 Topic: Python’s building blocks -> More Statements
Topic: Introduction to Computing Science and Programming + Algorithm
Topic: Python’s building blocks -> Variables, Values, and Types
Topic: Recursion – Part 2
Topic: Functions – Part 1
Topic: Programming Languages and their Evolution + Intro to Scratch
Topic: Iterative Statements – Part 1 -> for loop
CMPT 120 Topic: Python Modules.
Chapter One Problem Solving
Topic: Introduction to Computing Science and Programming + Algorithm
Introduction to Algorithms
Topic: Python’s building blocks -> Statements
Topic: Conditional Statements – Part 1
ALGORITHMS part-1.
2008/09/22: Lecture 6 CMSC 104, Section 0101 John Y. Park
Algorithm & Programming
Topic: Python’s building blocks -> Variables, Values, and Types
Topic: Conditional Statements – Part 2
CMPT 120 Topic: Searching – Part 1
CMPT 120 Topic:  Case Study.
Topic: Recursion – Part 1
CMPT 120 Topic: Python strings.
CS1371 Introduction to Computing for Engineers
Topic: Functions – Part 2
Lecture 2 Introduction to Programming
Introduction To Flowcharting
Programming Fundamentals Lecture #7 Functions
CS 5010 Program Design Paradigms "Bootcamp" Lesson 9.3
Computer Programming.
Functions Inputs Output
Unit# 9: Computer Program Development
Learning to Program in Python
Functions, Procedures, and Abstraction
Algorithm An algorithm is a finite set of steps required to solve a problem. An algorithm must have following properties: Input: An algorithm must have.
Design and Implementation
CSC128 FUNDAMENTALS OF COMPUTER PROBLEM SOLVING
Faculty of Computer Science & Information System
ME 142 Engineering Computation I
Understanding Problems and how to Solve them by using Computers
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
COMPUTATIONAL THINKING COMPUTATIONAL THINKING IN PROGRAMMING
Basic Concepts of Algorithm
The structure of programming
Thinking procedurally
CMPT 120 Lecture 2 - Introduction to Computing Science – Problem Solving, Algorithm and Programming.
CMPT 120 Lecture 16 – Unit 3 – Graphics and Animation
Topic: Iterative Statements – Part 2 -> for loop
Functions, Procedures, and Abstraction
CMPT 120 Lecture 15 – Unit 3 – Graphics and Animation
CMPT 120 Lecture 4 – Unit 1 – Chatbots
CMPT 120 Lecture 13 – Unit 2 – Cryptography and Encryption –
Lecture 17 – Practice Exercises 3
CMPT 120 Lecture 18 – Unit 3 – Graphics and Animation
Lecture 20 – Practice Exercises 4
Lecture 20 – Practice Exercises 4
Lecture 17 – Practice Exercise 3 SOLUTIONS
CMPT 225 Lecture 7 – Stack.
Introduction to Methods and Interfaces
Presentation transcript:

CMPT 120 Topic: Functions – Part 4 Developing Software that incorporates Functions

Last Lecture Execution flow – so far, i.e., program with no user-defined functions Stack frame Execution flow through main part of program and functions When functions are called: Arguments and parameters matching Value returned from functions to “caller” Scope of local variable and parameters Immutable versus mutable

Learning outcomes At the end of this course, a student is expected to: Create (design) small to medium size programs using Python: Decompose a Python program into functions Use the core features of Python to design programs to solve problems: variables, expressions, terminal input and output, type conversion, conditionals, iteration, functions, standard library modules Design programs requiring approximately 100 lines and 6 functions (of well-designed code) Describe the benefits of using functions Construct functions such that: Functions have a single purpose (decomposition) Functions are reusable (generalisation) Functions include parameters and local variables Functions return values etc…

Today’s Menu Case study: developing software that incorporates functions In the process, we shall point out a few guidelines: Decomposition Incremental Development Function Interface Design Generalization Composition Encapsulation

Creating functions in our software Two ways of going about this! Way 1 If the software does not already exist, we can design and implement our solution incorporating functions Way 2 If the software already exist, we can encapsulate some of its code fragments (the ones with one specific purpose/repeated code fragments) into functions

Way 1 : Developing software incorporating functions Incorporating functions into our software as we are developing it! Let’s illustrate the development of software (a Python program) incorporating functions with a case study called Area Calculator

Step 1 – Problem Statement Problem statement: Develop a Python program to compute the area of various shapes: triangle, circle, rectangle, square, ellipse

Step 2 – Applying Decomposition As we design a solution, we decompose it into actions --> functions Compute the area of various shapes: triangle, circle, rectangle, square, ellipse Get option from user + validate Get input from user + validate Compute desired area Display result Print menu

Decomposition in the real world Getting myself to SFU in the morning Get dressed Eat breakfast Take the bus Wake up Wash up

Step 2 – Algorithm Each action -> a step of the algorithm So, each of these steps has a purpose Note that this algorithm is not very detailed -> High-level algorithm # Print menu # Get option from user (+ validate input) # Based on option selected by user, # get appropriate input from user # ( + validate input) # Compute desired area # Display result So, each step could potentially be implemented as a function

For example … # Print menu -> became the function printMenu() #Get option from user (+ validate input) -> became the function getSelection()

Step 2 – Low-level Algorithm # Print menu # Print description of program # Print menu displaying selection of shapes # Get options from user (+ validate input) # Print input instruction to user and read user input # Validate input # Based on option selected by user, get appropriate input from user (+ validate input) # If "triangle" is selected, then ask for the base and height # If "circle" is selected, then ask for the radius # if "rectangle" is selected, then ask for the width and height # if "square" is selected, then ask for one side # if "ellipse" is selected, then ask for both radii # Compute desired area # If "triangle" is selected, then compute area = 0.5 ( base * height ) # If "circle" is selected, then compute area = pi * radius squared # if "rectangle" is selected, then compute area = width * height # if "square" is selected, then compute area = side squared # if "ellipse" is selected, then compute area = pi * radius1 * radius2 # Display result # Print the shape, the input data and the area

Step 4 - Implementation See Area Calculator program posted on our course web site

Versions to our Case Study - 1 AreaCalculator - version 1 : Demonstrating incremental development guideline by implementing and testing the first two steps of our algorithm AreaCalculator - version 2 : Demonstrating incremental development guideline by implementing the sections of our algorithm dealing with the rectangle AreaCalculator - version 3 : Demonstrating incremental development guideline by implementing the sections of our algorithm dealing with the square

Versions to our Case Study - 2 AreaCalculator - version 4 : Demonstrating refactoring repeated code from the functions square( ) and rectangle( ) and encapsulating this repeated code into their own function: getUserInput( whichData, shape ) -> called from square( ) and rectangle( ) to get and validate side, width or height from user areaOfParallelogram( base, height ) -> called from square( ) and rectangle( ) to compute their area since square and rectangle are both parallelograms and therefore use the same area equation displayResult( theShape, area ) -> called from the main part of the program to display the result since all shapes will have a resulting area to display

Versions to our Case Study - 3 Note: Throughout the 4 versions of our AreaCalculator, we demonstrate how to design the interface of a function Function’s purpose and name Function’s parameter(s) Function’s returned value

AreaCalculator – Main Loop notASelection = 'X' ... # Main part of the program - top level (of execution) # As long as the user enters a valid selection ... while selectedShape != notASelection : area = 0 # If "triangle" is selected? if selectedShape == "T": # deal with triangle # If "circle" is selected? elif selectedShape == "C": # deal with circle # If "rectangle" is selected? elif selectedShape == "R": theShape = "rectangle" area = rectangle() # If "square" is selected? elif selectedShape == "S": theShape = "square" area = square() # If "ellipse" is selected? elif selectedShape == "E" : # deal with ellipse print("---") printMenu() called getSelection(…) called Event loop displayResult(…) called

Way 2 : Enhancing software by incorporating functions If the software already exist, we can encapsulate (i.e., refactor) some of its code fragments into functions using the following guidelines: If a code fragment is made of logically related statements, i.e., the code fragment has one well defined purpose, put the code into a function and replace the code fragment in the main part of the program by a call to this function If a code fragment is repeated in several places in the program, put the repeated code into a function and replace each instance of the repeated code in the main part of the program by a call to this function

Summary Developing Software that incorporates Functions Way 1 – the program does not exist yet Way 2 – the program has already been written

Next Lecture Recursion