Introduction to Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.

Slides:



Advertisements
Similar presentations
Python Basics: Statements Expressions Loops Strings Functions.
Advertisements

CSE 251 Dr. Charles B. Owen Programming in C1 Functions.
Adapted from John Zelle’s Book Slides
Python Programming: An Introduction to Computer Science
Vahé Karamian Python Programming CS-110 CHAPTER 2 Writing Simple Programs.
Lecture 2 Introduction to C Programming
Introduction to C Programming
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
CS 201 Functions Debzani Deb.
Introduction to Python
Chapter 2 Writing Simple Programs
Introduction to C Programming
Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.
Functions.
Introduction to C++ - How C++ Evolved Most popular languages currently: COBOL, Fortran, C, C++, Java (script) C was developed in 1970s at AT&T (Richie)
An Introduction to Textual Programming
Introduction to Python
COMP 171: Functions, for loops and range John Barr Section 03 Slides by Toby Dragon.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
Chapter 6 Functions -- QuickStart. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. What is a function?
Chapter 2 Overview of C Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
More on Functions (Part 2) Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
CSE 131 Computer Science 1 Module 1: (basics of Java)
Functions, Procedures, and Abstraction Dr. José M. Reyes Álamo.
Built-in Data Structures in Python An Introduction.
Introduction to Computing Using Python for loop / def- ining a new function  Execution control structures ( if, for, function call)  def -ining a new.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Chapter 3 Top-Down Design with Functions Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
Introduction to Strings Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg 1.
Guide to Programming with Python Chapter Seven Files and Exceptions: The Trivia Challenge Game.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Last Week Modules Save functions to a file, e.g., filename.py The file filename.py is a module We can use the functions in filename.py by importing it.
2. WRITING SIMPLE PROGRAMS Rocky K. C. Chang September 10, 2015 (Adapted from John Zelle’s slides)
More on Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
CSE 251 Dr. Charles B. Owen Programming in C1 Functions.
Introduction to Functions CSIS 1595: Fundamentals of Programming and Problem Solving 1.
1. COMPUTERS AND PROGRAMS Rocky K. C. Chang September 6, 2015 (Adapted from John Zelle’s slides)
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Introduction to Files Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg 1.
CS 115 Lecture 5 Math library; building a project Taken from notes by Dr. Neil Moore.
More on Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
More on Functions (Part 2) Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Python Programming Module 3 Functions Python Programming, 2/e1.
Programming what is C++
Fundamentals of Python: First Programs
More on Functions (Part 2)
Python: Experiencing IDLE, writing simple programs
Chapter 7 Text Input/Output Objectives
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Variables, Expressions, and IO
Topics Introduction to File Input and Output
Functions, Procedures, and Abstraction
More on Functions (Part 2)
Elements of a Python Program
Fundamentals of Python: First Programs
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
Introduction to Computer Science
Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg
Introduction to Computer Science
Introduction to Strings
Chopin’s Nocturne.
Topics Introduction to File Input and Output
Introduction to Strings
Functions, Procedures, and Abstraction
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

Introduction to Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg

Assorted Topics Issues about reading files? Questions about the HW assignment due Monday

Review So far, most of our programs have retrieved data from the keyboard and written data to the screen  Data must be entered on every program run  Programs have no way to write permanent output Text files provide convenient input/output storage  e.g. programs can read configuration data or input files to process, and can write output to files

Question #1 A program is designed to retrieve some data from a file, process it, and output the revised data to another file. Which of the following functions/methods will not be called in the program?  A. open  B. A loop or method for reading (e.g. read)  C. write  D. close  E. All should be called

Review Several methods for reading text from files:  readline: reads and returns next line; returns empty string at end-of-file  read: reads the entire file into one string  readlines: reads the entire file into a list of strings All of these leave a trailing '\n' character at the end of each line.

Review A file is a sequence of lines. Can be read with a for-loop f = open(‘data.txt‘,”r”) for line in f: print(line.strip()) …or using a while-loop: f = open(‘data.txt‘,”r”) line = f.readline() while line: print(line.strip()) line = f.readline()

Question #2 – What is the last thing printed? data.txt Reading Assignments #Each line lists the reading #assignment for that date Sep, 17, Section Sep, 19, Section Sep, 21, Section program line = f.readline() while line.startswith('#'): line = f.readline() print(f.readline())

Question #3 – What is the last thing printed? data.txt Reading Assignments #Each line lists the reading #assignment for that date Sep, 17, Section Sep, 19, Section Sep, 21, Section program line = f.readline() while line.startswith('#'): line = f.readline() print( line )

What is a Function?

Functions From mathematics we know that functions perform some operation and return one value. They “encapsulate” the performance of some particular operation, so it can be used by others (for example, the len() function).

Why Have Them? Abstraction of an operation Reuse: once written, use again Sharing: if tested, others can use Security: if well tested, then secure for reuse Simplify code: more readable Support divide-and-conquer strategy

Mathematical Notation Consider a function which converts temperatures in Celsius to temperatures in Fahrenheit:  Formula: F = C *  Functional notation: F = celsisus2Fahrenheit(C) where celsius2Fahrenheit(C) = C*

Python Invocation Math: F= celsius2Fahrenheit(C) Python, the invocation is much the same F = celsius2Fahrenheit(C) Terminology: argument “C”

Function Definition Math: g(C) = C* Python def celsius2Fahrenheit (C): return C* Terminology: parameter “C”

Return Statement The return statement indicates the value that is returned by the function. The statement is optional (the function can return nothing). If no return, the function is often called a procedure.

Code Listing 6.1 Temp Convert

Code Listing 6.1 # Temperature conversion def celsius2fahrenheit(celsius): """ Convert Celsius to Fahrenheit.""" return celsius*

Triple Quoted String in Function A triple quoted string just after the def is called a docstring docstring is documentation of the function’s purpose, to be used by other tools to tell the user what the function is used for.

Operation def celsius2Fahrenheit (celsius): return celsius* F = celsius2Fahrenheit(C) 1. Call copies argument C to parameter celsius 2. Control transfers to function “celsius2Farenheit”

Operation (con’t) 3. Expression in celsius2Farenheit is evaluated 4. Value of expression is returned to the invoker F = celsius2Fahrenheit(C) def celsius2Fahrenheit (celsius): return celsius*

Code Listing 6.3 Implement len

Code Listing 6.3 def length(S): """Return the length of S.""" count = 0 for s in S: count += 1 return count

Code Listing 6.4 Count Letters in String

Check Membership in lowercase import string use string.lowercase, string of lowercase  ‘abcdefghijklmnopqrstuvwxyz’ check if each letter is a member (using the in operator) of string.lowercase

Code Listing 6.4 import string def letterCount(S): """Return the count of letters in S.""" count = 0 for s in S: if s.lower() in string.ascii_lowercase: count += 1 return count

How to Write a Function Does one thing. If it does too many things, it should be broken down into multiple functions (refactored). Readable. How often should we say this? If you write it, it should be readable. Reusable. If it does one thing well, then when a similar situation (in another program) occurs, use it there as well.

More on Functions Complete. A function should check for all the cases where it might be invoked. Check for potential errors. Not too long. Kind of synonymous with “does one thing”. Use it as a measure of doing too much.

Procedures Functions that have no return statements are often called procedures. Procedures are used to perform some duty (print output, store a file, etc.) Remember, return is not required.

Multiple Returns in a Function A function can have multiple return statements. Remember, the first return statement executed ends the function. Multiple returns can be confusing to the reader and should be used judiciously.