Topic: Functions – Part 1

Slides:



Advertisements
Similar presentations
Chapter 6: User-Defined Functions I
Advertisements

C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
CS 201 Functions Debzani Deb.
Chapter 6: User-Defined Functions I
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Introduction to Computing Using Python for loop / def- ining a new function  Execution control structures ( if, for, function call)  def -ining a new.
1 Functions, Part 1 of 2 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function Header Comments.
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 
PYTHON FUNCTIONS. What you should already know Example: f(x) = 2 * x f(3) = 6 f(3)  using f() 3 is the x input parameter 6 is the output of f(3)
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Topic: Python Lists – Part 1
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
Chapter 6: User-Defined Functions I
Topic: Programming Languages and their Evolution + Intro to Scratch
Topic: Iterative Statements – Part 1 -> for loop
Introduction to Programming
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
CMPT 120 Topic:  Case Study.
Topic: Recursion – Part 1
CMPT 120 Topic: Functions – Part 4
CMPT 120 Topic: Python strings.
Topic: Functions – Part 2
Functions CIS 40 – Introduction to Programming in Python
Functions.
2011/11/20: Lecture 15 CMSC 104, Section 4 Richard Chang
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Functions, Procedures, and Abstraction
Functions, Part 1 of 3 Topics Using Predefined Functions
Elements of a Python Program
Chapter 20: Programming Functions
Functions, Part 1 of 3 Topics Using Predefined Functions
COMPUTER PROGRAMMING SKILLS
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
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
def-ining a function A function as an execution control structure
CPS125.
Functions, Procedures, and Abstraction
Lecture 7 – Unit 1 – Chatbots Python – For loops + Robustness
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 –
 A function is a named sequence of statement(s) that performs a computation. It contains  line of code(s) that are executed sequentially from top.
CMPT 120 Lecture 19 – Unit 3 – Graphics and Animation
Lecture 17 – Practice Exercises 3
CMPT 120 Lecture 18 – Unit 3 – Graphics and Animation
Topic: Where to put functions in a program
Lecture 20 – Practice Exercises 4
Functions John R. Woodward.
Lecture 20 – Practice Exercises 4
Lecture 17 – Practice Exercise 3 SOLUTIONS
Lecture 3 – Data collection List ADT
Review and Instructions
Functions, Part 1 of 2 Topics Using Predefined Functions
Presentation transcript:

Topic: Functions – Part 1 CMPT 120 Topic: Functions – Part 1

Last Lecture We had a look at some algorithms commonly used in CS

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 Functions Create our own functions Built-in User-defined Design function interface Syntax

So far … … we have used functions that were already built into Python by calling them Built-in functions (some came from modules) For example: print(…), input(…), type(…), random.randint(1, 10) Built-in methods For example: <string>.upper( ), <string>.isalpha( )

Remember (from Lecture 8): Terminology related to functions We call a function by name Example of a function call: userName = input("Please, enter your name: ") The name of the function is input The expression(s) in parentheses is (are) called the argument(s) of the function We say that a function takes an argument(s) The result of the function is what the function produces In the above example, the result of the function is what the user has typed, i.e., her/his name, which is assigned to userName ‘Anne’ We say that a function returns a result and the result is called the returned value

Different kinds of functions Activity: Fill this table with examples of built-in functions! Function takes arguments Function does not take arguments Function returns a result (i.e., a returned value) Function does not return a result (i.e., a returned value)

So, what is a function? Description: (adapted from our textbook) A named sequence of Python statements that has one purpose

Are there “functions” in the real world? Mom says to Louise “Go make your bed!“ Purpose: Make bed Parameter(s): None required Returned value: None returned Mom says to Louise “Here’s $5, go buy a bag of apples!“ Purpose: Parameter(s): Returned value:

Are there “functions” in the real world? Mom says to Louise “Go find my cell phone!“ Purpose: Parameter(s): Returned value: Mom says to Louise “Go put these bags on the kitchen counter!“ Purpose: Put bags on kitchen counter

Back to the computer world, let’s create our own functions User-defined functions (as opposed to built-in functions)

Step 1 - 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

Step 2 – Solution design (algorithm and data) Purpose of function Algorithm –> how the function achieve its purpose Data -> Data needed by the function to achieve its purpose: Input Does the function need data from the main program? -> arguments/parameters Local variables Does the function need to create variables? Output Does the function need to return its result to the main program? -> returned value

Function Interface Function name (purpose) Parameters Returned value

Step 4 – Implementation Syntax of function definition def <functionName>( [parameter(s)] ) : < 1 or more statements > return [expression] def -> means “here is the definition of a function” <functionName> (Good Programming Style - GPS) Function name is descriptive -> it describes its purpose Function name syntax: same as for variable name syntax Function definition -> Header of a function -> Body of a function -> One return statement

Step 4 – Implementation Solution See LargestOf3.py on our course web site

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

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