Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 3 Simple.

Slides:



Advertisements
Similar presentations
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 9 Classes.
Advertisements

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 1- 1.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 2- 1.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 11 Classes and Object- Oriented Programming.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 9- 1.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18 Indexing Structures for Files.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 11 Object, Object- Relational, and XML: Concepts, Models, Languages,
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Chapter 2: Input, Processing, and Output
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 6: Functions by.
Copyright © 2012 Pearson Education, Inc. Chapter 6 Modularizing Your Code with Methods.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 Part 1 Conditionals and Loops.
Topic 4 – Programmer- Defined Functions. CISC 105 – Topic 4 Functions So far, we have only seen programs with one function, main. These programs begin.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Using Data Active Server Pages Objectives In this chapter, you will: Learn about variables and constants Explore application and session variables Learn.
CHAPTER SIX Reducing Program Complexity General Sub Procedures and Developer-defined Functions.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 6 Value-Returning.
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.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design First Edition by Tony Gaddis.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design First Edition by Tony Gaddis.
General Computer Science for Engineers CISC 106 Lecture 04 Dr. John Cavazos Computer and Information Sciences 09/10/2010.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 2 Graphics Programming with C++ and the Dark GDK Library Starting.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6: Functions Starting Out with C++ Early Objects Seventh Edition.
Programming in C++ Language ( ) Lecture 5: Functions-Part1 Dr. Lubna Badri.
Overview Intro to functions What are functions? Why use functions? Defining functions Calling functions Documenting functions Top-down design Variable.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Liang, Introduction to C++ Programming, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Advanced Function Features.
Starting Out with Java: From Control Structures through Objects Fifth Edition by Tony Gaddis Chapter 5: Methods.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions Outline 5.1Introduction 5.2Program Modules.
FUNCTIONS. Topics Introduction to Functions Defining and Calling a Void Function Designing a Program to Use Functions Local Variables Passing Arguments.
CHAPTER SIX Reducing Program Complexity General Sub Procedures and Developer-defined Functions.
INLS 560 – F UNCTIONS Instructor: Jason Carter.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 5 Repetition.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6: Functions Starting Out with C++ Early Objects Eighth Edition.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-1 Why Write Methods? Methods are commonly used to break a problem down.
Chapter Topics 2.1 Designing a Program 2.2 Output, Input, and Variables 2.3 Variable Assignment and Calculations 2.4 Variable Declarations and Data Types.
Introduction to Functions CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
Guide to Programming with Python Chapter Six Functions: The Tic-Tac-Toe Game.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Third Edition by Tony Gaddis.
Topics Introduction to Functions Defining and Calling a Void Function
The Selection Structure
User-Defined Functions
Starting Out with Programming Logic & Design
© 2016 Pearson Education, Inc.,Hoboken, NJ. All rights reserved.
Chapter 3 Simple Functions
Chapter 4 void Functions
Topics Introduction to Functions Defining and Calling a Void Function
Topics Introduction to Functions Defining and Calling a Function
A function is a group of statements that exist within a program for the purpose of performing a specific task. We can use functions to divide and conquer.
Starting Out with Programming Logic & Design
Chapter 2: Input, Processing, and Output
Presentation transcript:

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 3 Simple Functions

1-2 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Introduction to Functions Concept: A function is a group of statements that exists within a program for the purpose of performing a specific task.

1-3 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Introduction to Functions Divide and Conquer Figure 3-1 Using functions to divide and conquer a large task

1-4 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Introduction to Functions Benefits of Using Functions Simple Code Code Reuse Better Testing Faster Development Easier Facilitation of Teamwork

1-5 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Defining and Calling a Function Concept: The code for a function is known as a function definition. To execute the function, you write a statement that calls it.

1-6 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Defining and Calling a Function Function Names Cannot use Python key words Cannot contain spaces First character must be one of the letters a through z, A through Z, or an underscore character(_) After the first character use letters a through z, A through Z, digits 0 through 9, or underscore Uppercase and lowercase characters are distinct

1-7 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Defining and Calling a Function Defining and Calling a Function def function_name(): statement etc. Function header Block

1-8 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Defining and Calling a Function Defining and Calling a Function Figure 3-2 The function definition and the function call

1-9 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Defining and Calling a Function Indentation in Python Figure 3-7 All of the statements in a block are indented

1-10 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Designing a Program to Use Functions Concept: Programmers commonly use a technique known as top-down design to break down an algorithm into functions.

1-11 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Designing a Program to Use Functions Flowcharting a Program with Functions Function Call Figure 3-8 Function call symbol

1-12 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Designing a Program to Use Functions Flowcharting a Program with Functions Figure 3-9 Flowchart for Program 3-2

1-13 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Designing a Program to Use Functions Top-Down Design The overall task of the program is broken down into a series of subtasks Each of the subtasks is examined to determine whether it can be further broken down into more subtasks. Once the subtasks are identified, they are written in code.

1-14 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Designing a Program to Use Functions Pausing Execution Until the User Presses Enter raw_input(‘Press Enter to see Step 1.’)

1-15 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Local Variables Concept: A local variable is created inside a function and cannot be accessed by statements that are outside the function. Different functions can have local variables with the same names because the functions cannot see each others’ local variables.

1-16 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Local Variables Scope and Local Variables A local variable’s scope is the function in which the variable is created

1-17 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Local Variables Program 3-5 (birds.py)

1-18 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Passing Arguments to Functions Concept: An argument is any piece of data that is passed into a function when the function is called. A parameter is a variable that receives an argument that is passed into a function.

1-19 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Passing Arguments to Functions Program 3-6 (pass_arg.py)

1-20 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Passing Arguments to Functions Figure 3-13 The value variable is passed as an argument Figure 3-14 The value variable and the number parameter reference the same value

1-21 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Passing Arguments to Functions Parameter Variable Scope A parameter variable’s scope is the function in which the parameter is used. Inside the function, the parameter variable can be accessed, but no statement outside the function can access it.

1-22 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Passing Arguments to Functions Passing Multiple Arguments Program 3-8 (multiple_args.py)

1-23 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Passing Arguments to Functions Making Changes to Parameters Program 3-10 (change_me.py)

1-24 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Passing Arguments to Functions Keyword Arguments Specifies which parameter the argument should be passed into. Its position in the function call does not matter. Program 3-11 (keyword_args.py)

1-25 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Global Variables and Global Constants Concept: A global variable is accessible to all the functions in a program file.

1-26 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Global Variables and Global Constants Program 3-13 (global1.py)

1-27 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Global Variables and Global Constants Restrict the use of global variables for the following reasons: Global variables make debugging difficult Functions that use global variables are usually dependent on those variables. Global variables make a program hard to understand.

1-28 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Global Variables and Global Constants Global Constants It is a global name that references a value that cannot be changed during the program’s execution.

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley QUESTIONS ? Chapter 3 Simple Functions