1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,

Slides:



Advertisements
Similar presentations
1 CSC 221: Computer Programming I Fall 2006 interacting objects modular design: dot races constants, static fields cascading if-else, logical operators.
Advertisements

Modular Programming With Functions
CS 100: Roadmap to Computing Fall 2014 Lecture 0.
1 CSC 551: Web Programming Spring 2004 client-side programming with JavaScript  scripts vs. programs  JavaScript vs. JScript vs. VBScript  common tasks.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
CSC 221: Computer Programming I Fall 2011
1 A Balanced Introduction to Computer Science, 2/E David Reed, Creighton University ©2008 Pearson Prentice Hall ISBN Chapter 17 JavaScript.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
Introduction to Python
Computers and Scientific Thinking David Reed, Creighton University Functions and Randomness 1.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
Python Programming Fundamentals
CS190/295 Programming in Python for Life Sciences: Lecture 2 Instructor: Xiaohui Xie University of California, Irvine.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Introduction to Python
Munster Programming Training
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
FUNCTIONS. Function call: >>> type(32) The name of the function is type. The expression in parentheses is called the argument of the function. Built-in.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Introduction to Computational Linguistics Programming I.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Input, Output, and Processing
Linux Operations and Administration
1 Functions Lecfture Abstraction abstraction is the process of ignoring minutiae and focusing on the big picture in modern life, we are constantly.
Computers and Scientific Thinking David Reed, Creighton University Functions and Libraries 1.
Functions, Procedures, and Abstraction Dr. José M. Reyes Álamo.
COMP 116: Introduction to Scientific Programming Lecture 11: Functions.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Python Functions.
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.
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.
FUNCTIONS. Topics Introduction to Functions Defining and Calling a Void Function Designing a Program to Use Functions Local Variables Passing Arguments.
Loops and Simple Functions CS303E: Elements of Computers and Programming.
INLS 560 – F UNCTIONS Instructor: Jason Carter.
1 CSC 221: Computer Programming I Fall 2011 Python control statements  operator precedence  importing modules  random, math  conditional execution:
2. WRITING SIMPLE PROGRAMS Rocky K. C. Chang September 10, 2015 (Adapted from John Zelle’s slides)
1. COMPUTERS AND PROGRAMS Rocky K. C. Chang September 6, 2015 (Adapted from John Zelle’s slides)
12. MODULES Rocky K. C. Chang November 6, 2015 (Based on from Charles Dierbach. Introduction to Computer Science Using Python and William F. Punch and.
Today… Style, Cont. – Naming Things! Methods and Functions Aside - Python Help System Punctuation Winter 2016CISC101 - Prof. McLeod1.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
Review: A Structural View program modules -> main program -> functions -> libraries statements -> simple statements -> compound statements expressions.
Lesson 06: Functions Class Participation: Class Chat:
Exam #1 You will have exactly 30 Mins to complete the exam.
Topics Introduction to Functions Defining and Calling a Void Function
Introduction to Programming
Python: Experiencing IDLE, writing simple programs
Introduction to Python
Variables, Expressions, and IO
Functions CIS 40 – Introduction to Programming in Python
CSC113: Computer Programming (Theory = 03, Lab = 01)
Chapter 5 - Functions Outline 5.1 Introduction
Functions, Procedures, and Abstraction
CS 100: Roadmap to Computing
Variables and Expressions
Rocky K. C. Chang September 18, 2018 (Based on Zelle and Dierbach)
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
CISC101 Reminders All assignments are now posted.
Functions continued.
COMPUTER PROGRAMMING SKILLS
CSC 221: Introduction to Programming Fall 2018
CS 100: Roadmap to Computing
Functions, Procedures, and Abstraction
 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.
Using Modules.
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions, computational abstraction  parameters, local variables  return statements

Built-in functions Python provides many built-in functions  mathematically, a function is a mapping from some number of inputs to an output  e.g., the abs function maps a single number to its absolute value  e.g., the max / min functions map two or more numbers to the maximum/minimum number 2 note that these functions do not just print the output, they return it  that is, a function call can appear in an expression, its value is the output value

Modules & functions in addition to these basic, built-in functions, the math module contains other useful functions 3 as with turtle, you load a module using import can then call functions from the math module as math.FUNC(…) also have constants for π and e

Why the prefix? the round function does not have a prefix: round(3.2) but the ceiling function does: math.ceil(3.2) 4  when a module is loaded using import, the module prefix must be used to clearly identify functions (since different modules might have functions with the same name)  no ambiguity for built-in functions or modules run directly in IDLE, so no prefix required

random module the random module contains functions for generating random values 5 random() returns a float from range [0, 1) randint(X, Y) returns an int from range [X, Y] choice() returns a random character from a str

Python documentation you can get the latest Python details online  Language Reference gives an overview of the language  Library Reference documents the common library modules (e.g., math, random, turtle)  Global Module Index allows you to search for modules by name 6

Python help modules are also self- documenting call the help function to see info on a function can also call help on the entire module 7

User-defined functions in addition to built-in and standard module functions, you can organize your code into functions  recall the population growth code 8 as is, you have to rerun the module each time you want to calculate a new growth estimate better solution – package the code into a function, then can call that function repeatedly

Simple functions in its simplest form, a Python function is a sequence of statements grouped together into a block  general form: def FUNC_NAME(): SEQUENCE_OF_STATEMENTS 9 the statements that make up the function block must be indented (consistently) you can have blank lines inside the function for readability consecutive blank lines will end the function

Advantages of functions functions encapsulate a series of statements under a name  once defined, a function can be called over and over without rerunning the module a python function is analogous to a Scratch script, which combines a sequence of blocks that can be executed as a unit 10

Exercise: FORCAST function convert your FORCAST reading level code into a function  encapsulate the code under a function name, e.g., readingLevel  test your function by running the module and then doing multiple reading level calculations 11 simple functions like these are units of computational abstraction  the function encapsulates a sequence of computations into a single unit  once you know the name you no longer need to remember how it works (i.e., you abstract away the details and focus on the overall effect) but they don't resemble math functions very closely  no inputs & no explicit output

Functions with inputs to generalize a function so that it can take inputs, must add parameters  a parameter is a variable that appears in the heading of a function definition (inside the parentheses)  when the function is called, you must provide an input value for each parameter  these input values are automatically assigned to the parameter variables 12

Exercise: parameterizing FORCAST modify your readingLevel function to have two parameters def readingLevel(singleWords, totalWords)  note that the input values match up with parameters in the order provided >>> readingLevel(104, 380) 13 prompts vs. inputs?  if your code requires many different input values, then having a function that prompts for each value is often simpler (prompts remind the user of what to enter)  if there are few inputs, then often simpler & quicker to provide them as inputs avoids the prompts and allows the user to enter the values directly allows you to determine the inputs some other way (e.g., another function to calculate the input values)

Functions that return values consider functions that convert distances in Metric & Imperial  a return statement specifies the output value for a function 14 the reverse conversion could be similarly defined using the factor  or, could make use of the existing feetToMeters function

Print vs. return when the results are complex, printing is more readable  the population results involve two numbers and description text  much easier to understand than 15 however, a function that prints its results is limited  the user can view the displayed results, but it cannot be used by other code  if returned, the result can be used in other computations if the result of a computation is potentially useful for some other computation, then return that result

Exercise: years vs. seconds define a function that converts a number of years into the corresponding number of seconds similarly, define a function that does the opposite conversion 16

Function doc strings it is considered good programming practice to put a multiline comment at the beginning of every function definition  it should briefly document the purpose of the function  in addition to making it easier to read the code in the editor, the doc string can be viewed in the interpreter shell using the built-in help function 17

Simulating dice rolls Consider the following function for simulating the roll of a die  what is good about this code? what is bad? 18  suppose we wanted to generalize the function so that it works for an N-sided die  suppose we wanted to get the sum of two die rolls