Topic: Functions – Part 2

Slides:



Advertisements
Similar presentations
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
Advertisements

Chapter 6: User-Defined Functions I
An Introduction to Python – Part IV Dr. Nancy Warter-Perez.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
Chapter 6: User-Defined Functions I
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
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.
Oct 15, 2007Sprenkle - CS1111 Objectives Creating your own functions.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
CPS120: Introduction to Computer Science Functions.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Python Functions.
1 CS161 Introduction to Computer Science Topic #9.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Debugging and Printing George Mason University. Today’s topics Review of Chapter 3: Printing and Debugging Go over examples and questions debugging in.
1 UMBC CMSC 104, Section Fall 2002 Functions, Part 1 of 3 Topics Top-down Design The Function Concept Using Predefined Functions Programmer-Defined.
Functions, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
Programming Fundamentals Enumerations and Functions.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Today… Modularity, or Writing Functions. Winter 2016CISC101 - Prof. McLeod1.
Lesson 06: Functions Class Participation: Class Chat:
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
Chapter 6: User-Defined Functions I
Topic: Programming Languages and their Evolution + Intro to Scratch
Topic: Iterative Statements – Part 1 -> for loop
CMPT 120 Topic: Python Modules.
Topic: Introduction to Computing Science and Programming + Algorithm
Topic: Python’s building blocks -> Statements
Topic: Conditional Statements – Part 1
Topic: Python’s building blocks -> Variables, Values, and Types
Topic: Conditional Statements – Part 2
Functions Review.
Topic: Recursion – Part 1
CMPT 120 Topic: Functions – Part 4
CMPT 120 Topic: Python strings.
Functions CIS 40 – Introduction to Programming in Python
Functions.
User-Defined Functions
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Functions I Creating a programming with small logical units of code.
Functions, Procedures, and Abstraction
Functions, Part 1 of 3 Topics Using Predefined Functions
Lesson 06: Functions Class Chat: Attendance: Participation
Chapter 6: User-Defined Functions I
Topics Introduction to Functions Defining and Calling a Function
Functions, Part 1 of 3 Topics Using Predefined Functions
Introduction to Computer Science
Functions, Part 1 of 3 Topics Using Predefined Functions
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Functions I Creating a programming with small logical units of code.
CMPT 120 Lecture 16 – Unit 3 – Graphics and Animation
Functions Taken from notes by Dr. Neil Moore & Dr. Debby Keen
CPS125.
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
Topic: Where to put functions in a program
Lecture 20 – Practice Exercises 4
Lecture 20 – Practice Exercises 4
Lecture 2 – Abstract Data Type (ADT)
Presentation transcript:

Topic: Functions – Part 2 CMPT 120 Topic: Functions – Part 2

Last Lecture Functions Create our own functions Built-in User-defined Design function interface Syntax

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 Create our own functions Guideline Why creating functions Docstring Location of functions in program Guideline Generalization Why creating functions

Review: Different kinds of functions + syntax Function takes arguments (i.e., function has parameters) Function does not take arguments (i.e., function does not have parameters) Function returns a result (i.e., a returned value) Function does not return a result (i.e., a returned value) def functionName( <parameter(s)> ) : < 1 or more statements > return <expression> def functionName( ) : < 1 or more statements > return <expression> def functionName( <parameter(s)> ) : < 1 or more statements > return def functionName( ) : < 1 or more statements > return

Creating our own functions Problem Statement Let us create a function that returns the largest number out of 3 numbers Requirements: cannot use the max( ) built-in function cannot use the word max to name your function Solution See LargestOf3.py on our course web site

Docstring - Demo What else does it do? -> Demo!!! Docstring is a comment Can be used to describe the purpose of a function (what it does and returns) and its parameters Example: """This function returns the largest number out of 3 numbers passed as parameters. Parameters: number1, number2, number3 -> contain the 3 numbers """ What else does it do? -> Demo!!!

Where to put functions in our Python program Header comment block + import statement(s) # Header Comment Block # Function 1 … Function definitions # Function n # Main part of my program The main part of my program

Function call –> situation 1 Python program Header comment block + import statement(s) # Header Comment Block # Function Function definition # Main part of my program The main part of my program

Function call –> situation 2 Python program Header comment block + import statement(s) # Header Comment Block # Function 1 Function definitions # Function 2 # Main part of my program The main part of my program

Function call –> situation 3 Python program Header comment block + import statement(s) # Header Comment Block # Function 1 Function definitions # Function 2 # Main part of my program The main part of my program

Function call -> Would this situation work? Python program # Header Comment Block Header comment block + import statement(s) # Function 1 Function definition # Main part of my program The main part of my program # Function 2 Function definition

Function call –> Would this situation work? Python program Header comment block + import statement(s) # Header Comment Block # Function 1 Function definitions # Function 2 # Main part of my program The main part of my program

Where to put functions in our Python program – Bad! Header comment block + import statement(s) # Header Comment Block # Main part of my program The main part of my program # Function 1 Function definitions # Function 2

Generalization guideline We always strive to create functions (and programs) that solve as many similar problems as possible Characteristic of good programming style (GPS)

Revisit Problem Statement - 1 Let us create a function that returns the largest number out of 3 numbers Requirements: cannot use the max( ) built-in function cannot use the word max to name your function A more generalised version of the above function, would be:

Solution to Revisited Problem Statement - 1 See LargestOfX.py on our course web site

Problem Statement - 2 Let us create a function that prints x number of a symbol in a row Sample Runs: >>> !!!!! >>> ***

Solution to Problem Statement - 2 See PrintRow.py on our course web site

Why creating functions? Incremental development: Dividing a long program into functions allows us to implement, test and debug the parts one at a time and then assemble them into a working whole “Logical” decomposition: Creating a new function gives us an opportunity to name a code fragment, which makes our program easier to read and debug No more repeated code: Functions can make a program smaller by eliminating repeated code Repeated code is also very error-prone Easy modification: Later on, if we make a change to our program, we know “where to go” to find the code fragment we need to change Code reuse: Well-designed functions are often useful in many programs. Once we write, test and debug a function, we can reuse it

Summary Create our own functions Guideline Why creating functions Docstring Location of functions in program Guideline Generalization Why creating functions

Next Lecture Hand tracing functions – Things to consider: Stack frame Execution flow Immutable versus mutable Scope of local variable and parameters