© 2016 Pearson Education, Inc.,Hoboken, NJ. All rights reserved.

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

Introduction to C Programming
Chapter 7: User-Defined Functions II
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
Fortran- Subprograms Chapters 6, 7 in your Fortran book.
Using Data Active Server Pages Objectives In this chapter, you will: Learn about variables and constants Explore application and session variables Learn.
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 © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 3 Simple.
Chapter 6: User-Defined Functions
Chapter 13 Programming in the Large Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. TA: 鄭筱親 陳昱豪.
Oct 15, 2007Sprenkle - CS1111 Objectives Creating your own functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 6 Using Methods.
CPS120: Introduction to Computer Science Functions.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Variables and control statements in PL\SQL Chapter 10.
FUNCTIONS. Topics Introduction to Functions Defining and Calling a Void Function Designing a Program to Use Functions Local Variables Passing Arguments.
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Python Let’s get started!.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
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.
ITM 3521 ITM 352 Functions. ITM 3522 Functions  A function is a named block of code (i.e. within {}'s) that performs a specific set of statements  It.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Chapter 9: Value-Returning Functions
Programming Logic and Design Seventh Edition
Topics Introduction to Functions Defining and Calling a Void Function
Chapter 6: User-Defined Functions I
Introduction to Programming
Python Let’s get started!.
Introduction to Python
COMP 170 – Introduction to Object Oriented Programming
Function There are two types of Function User Defined Function
Functions CIS 40 – Introduction to Programming in Python
User-Defined Functions
C++ for Engineers and Scientists Second Edition
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Chapter 3 Simple Functions
Chapter 3 Introduction to Classes, Objects Methods and Strings
And now for something completely different . . .
Chapter 4 void Functions
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
variables and control statements in PL\SQL
Introduction to Programming
Topics Introduction to Functions Defining and Calling a Void Function
Rocky K. C. Chang 15 November 2018 (Based on Dierbach)
Core Objects, Variables, Input, and Output
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
Topics Introduction to Functions Defining and Calling a Function
CIS16 Application Development and Programming using Visual Basic.net
COMPUTER PROGRAMMING SKILLS
Introduction to C++ Programming Language
12th Computer Science – Unit 5
Introduction to Programming
Methods/Functions.
ITM 352 Functions.
CPS125.
 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.
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

© 2016 Pearson Education, Inc.,Hoboken, NJ. All rights reserved. Functions Part 1 Section 1 Chapter 4 © 2016 Pearson Education, Inc.,Hoboken, NJ.  All rights reserved.

© 2016 Pearson Education, Inc.,Hoboken, NJ. All rights reserved. Built-in Functions Like miniature programs Receive input Process the input Have output Table 4.1 Some Python built-in functions. © 2016 Pearson Education, Inc.,Hoboken, NJ.  All rights reserved.

© 2016 Pearson Education, Inc.,Hoboken, NJ. All rights reserved. Built-in Functions Output of functions is a single value Function is said to return its output Items inside parentheses called arguments Examples: © 2016 Pearson Education, Inc.,Hoboken, NJ.  All rights reserved.

User-defined Functions Defined by statements of the form par1, par2 are variables (called parameters) Expression evaluates to a literal of any type Header must end with colon Each statement in block indented same © 2016 Pearson Education, Inc.,Hoboken, NJ.  All rights reserved.

User-defined Functions Passing parameters We consider here pass by position Arguments in calling statement matched to the parameters in function header based on order Parameters and return statements optional in function definitions Function names should describe the role performed © 2016 Pearson Education, Inc.,Hoboken, NJ.  All rights reserved.

Functions Having One Parameter FIGURE 4.1 Header of the fahrenheitToCelsius function © 2016 Pearson Education, Inc.,Hoboken, NJ.  All rights reserved.

Functions Having One Parameter Example 1: Program uses the function fahrenheitToCelsius © 2016 Pearson Education, Inc.,Hoboken, NJ.  All rights reserved.

Functions Having One Parameter Example 2: Program uses the function firstName © 2016 Pearson Education, Inc.,Hoboken, NJ.  All rights reserved.

Passing a Value to a Function If the argument in a function call is a variable Object pointed to by the argument variable (not the argument variable itself) passed to a parameter variable Object is immutable, there is no possibility that value of the argument variable will be changed by a function call © 2016 Pearson Education, Inc.,Hoboken, NJ.  All rights reserved.

Passing a Value to a Function Example 3: Program shows there is no change in the value of the argument © 2016 Pearson Education, Inc.,Hoboken, NJ.  All rights reserved.

Passing a Value to a Function FIGURE 4.2 Passing a value to a function. © 2016 Pearson Education, Inc.,Hoboken, NJ.  All rights reserved.

Functions Having Several Parameters Must be the same number of arguments as parameters in the function Data types of arguments’ values must be compatible with data types expected by the parameters Must also be in the same order © 2016 Pearson Education, Inc.,Hoboken, NJ.  All rights reserved.

Functions Having Several Parameters Example 4: Program uses the function pay. © 2016 Pearson Education, Inc.,Hoboken, NJ.  All rights reserved.

Functions Having Several Parameters FIGURE 4.3 Passing arguments to a function. © 2016 Pearson Education, Inc.,Hoboken, NJ.  All rights reserved.

Functions Having Several Parameters Example 5: Function computes the balance in a saving account © 2016 Pearson Education, Inc.,Hoboken, NJ.  All rights reserved.

Functions Having Several Parameters Example 5: Function computes the balance in a saving account © 2016 Pearson Education, Inc.,Hoboken, NJ.  All rights reserved.

Boolean- and List-valued Functions Example 6: Program uses a Boolean-valued function to determine whether input is a vowel word © 2016 Pearson Education, Inc.,Hoboken, NJ.  All rights reserved.

Boolean- and List-valued Functions Example 7: Program uses a list-valued function © 2016 Pearson Education, Inc.,Hoboken, NJ.  All rights reserved.

Functions that do not Return Values Example 8: Program displays three verses of children’s song. © 2016 Pearson Education, Inc.,Hoboken, NJ.  All rights reserved.

Functions without Parameters Example 9: Program calculates the population density of a state © 2016 Pearson Education, Inc.,Hoboken, NJ.  All rights reserved.

Functions without Parameters Example 9: Program calculates the population density of a state © 2016 Pearson Education, Inc.,Hoboken, NJ.  All rights reserved.

© 2016 Pearson Education, Inc.,Hoboken, NJ. All rights reserved. Scope of Variables Variable created inside a function can only be accessed by statements inside that function Ceases to exist when the function is exited Variable is said to be local to function or to have local scope If variables created in two different functions have the same name They have no relationship to each other © 2016 Pearson Education, Inc.,Hoboken, NJ.  All rights reserved.

© 2016 Pearson Education, Inc.,Hoboken, NJ. All rights reserved. Scope of Variables Example 10: Variable x in the function main, variable x in the function trivial are different variables © 2016 Pearson Education, Inc.,Hoboken, NJ.  All rights reserved.

© 2016 Pearson Education, Inc.,Hoboken, NJ. All rights reserved. Scope of Variables Example 11: Variable x created in function main not recognized by function trivial. © 2016 Pearson Education, Inc.,Hoboken, NJ.  All rights reserved.

© 2016 Pearson Education, Inc.,Hoboken, NJ. All rights reserved. Scope of Variables Scope of a variable is the portion of the program that can refer to it To make a variable global, place assignment statement that creates it at top of program. Any function can read the value of a global variable Value cannot be altered inside a function unless © 2016 Pearson Education, Inc.,Hoboken, NJ.  All rights reserved.

© 2016 Pearson Education, Inc.,Hoboken, NJ. All rights reserved. Scope of Variables Example 12: Program contains a global variable © 2016 Pearson Education, Inc.,Hoboken, NJ.  All rights reserved.

© 2016 Pearson Education, Inc.,Hoboken, NJ. All rights reserved. Named Constants Program sometimes employs a special constant used several times in program Convention programmers use Create a global variable Name written in uppercase letters with words separated by underscore In Python, programmer is responsible for not changing value of the variable © 2016 Pearson Education, Inc.,Hoboken, NJ.  All rights reserved.

© 2016 Pearson Education, Inc.,Hoboken, NJ. All rights reserved. Library Modules A library module is a file with extension .py Contains functions and variables Can be used (imported) by any program can be created in IDLE or any text editor Looks like an ordinary Python program To gain access to the functions and variables place a statement of the form import moduleName at the beginning of the program © 2016 Pearson Education, Inc.,Hoboken, NJ.  All rights reserved.

© 2016 Pearson Education, Inc.,Hoboken, NJ. All rights reserved. Library Modules Consider a file with pay and futureValue functions Save as finance.py in same folder as examples 4 and 6 Rewrite example 4 as © 2016 Pearson Education, Inc.,Hoboken, NJ.  All rights reserved.

Library Modules Table 4.2 Several modules from the standard library. © 2016 Pearson Education, Inc.,Hoboken, NJ.  All rights reserved.

© 2016 Pearson Education, Inc.,Hoboken, NJ. All rights reserved. End Section 1 Chapter 4 © 2016 Pearson Education, Inc.,Hoboken, NJ.  All rights reserved.