Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Third Edition by Tony Gaddis.

Slides:



Advertisements
Similar presentations
1 Intro to Programming & Algorithm Design Functions Copyright 2003 by Janson Industries This presentation can be viewed on line in a file named: ch06.IntrotoProg.Functions.ppt.
Advertisements

Copyright © 2012 Pearson Education, Inc. Chapter 3: Expressions and Interactivity.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
Chapter 2: Input, Processing, and Output
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 3: Expressions and Interactivity.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 3 Variables, Calculations, and Colors Starting Out with Games.
Expressions and Interactivity Chapter 3. 2 The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Often.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 More About Strings.
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 © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 3 Simple.
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 © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6: Functions Starting Out with C++ Early Objects Seventh Edition.
Functions, Procedures, and Abstraction Dr. José M. Reyes Álamo.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 6 Using Methods.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Alice: A Visual Introduction to Programming Third Edition.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 5.3Math Library Functions Math library functions –perform.
© 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.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Repetition Structures.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6: Functions Starting Out with C++ Early Objects Eighth Edition.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
Chapter 3: User-Defined Functions I
A First Book of ANSI C Fourth Edition Chapter 6 Modularity Using Functions: Part I.
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.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Alice: A Visual Introduction to Programming Third Edition.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
Programming Logic and Design Fifth Edition, Comprehensive Chapter 7 Using Methods.
Lecture 3 Expressions, Type Conversion, Math and String
Chapter 9: Value-Returning Functions
Topics Introduction to Functions Defining and Calling a Void Function
Chapter 6: User-Defined Functions I
Chapter 2: Input, Processing, and Output
Chapter 3: Variables, Functions, Math, and Strings
Topics Introduction to Repetition Structures
Functions, Part 2 of 2 Topics Functions That Return a Value
The Selection Structure
Chapter 3: Expressions and Interactivity.
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 Programming
Chapter 5 - Functions Outline 5.1 Introduction
Starting Out with Programming Logic & Design
Expressions and Interactivity
Topics Introduction to File Input and Output
Functions, Procedures, and Abstraction
Unit 1: Introduction Lesson 1: PArts of a java program
Wednesday 09/23/13.
A First Book of ANSI C Fourth Edition
Topics Introduction to Functions Defining and Calling a Void Function
Chapter 6: User-Defined Functions I
Chapter 9: Value-Returning Functions
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
Introduction to Value-Returning Functions: Generating Random Numbers
Topics Introduction to Functions Defining and Calling a Function
Starting Out with Programming Logic & Design
Topics Introduction to Repetition Structures
Chapter 2: Input, Processing, and Output
Topics Introduction to File Input and Output
Presentation transcript:

Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Third Edition by Tony Gaddis Chapter 6: Functions

Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-2 Chapter Topics 6.1 Introduction to Functions: Generating Random Numbers 6.2 Writing Your Own Functions 6.3 More Library Functions

Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Introduction to Functions A function is a module that returns a value back to the part of the program that called it –Many languages provide libraries of functions that you can use, such as Random Number Generator –A function is like a module, but it returns a value that can be used in your program Library functions –Written functions that come with most languages –Usually common tasks and save time for the programmer because it allows for code reuse

Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Introduction to Functions The Random Number Generator function is useful in: –Game programs –Simulation programs –Statistical programs –Computer security such as encryption How random function works –Set number = random(1, 100) –1 and 100 define the range of the number that can be returned, and are called arguments –The function is called and a random number is returned and assigned to the variable number

Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Writing Your Own Functions Most languages allow coders to write functions The function header specifies the data type of the value that is returned, the name of the function, and any parameter variables The function body are the statements that execute when the function calls The return statement specifies the value that is returned when the function ends

Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Writing Your Own Functions

Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Writing Your Own Functions Additional concerns –While you can pass as many arguments into a function, you can only return one value –Functions simplify code, increase the speed of development, and ease the facilitation of teamwork –Each function should be flowcharted separately –IPO (input, processing, and output), can be used to show what a function does IPO Chart for the getRegularPrice Function InputProcessingOutput NonePrompts the user to enter an item’s regular price The item’s regular price, as a Real

Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley More Library Functions Mathematical Functions –Functions typically accept one or more values as arguments, perform a mathematical operation using the arguments, and return the results Set result = sqrt(16) –Returns the square root of 16 Set area = power(4, 2) –Raises the value of 4 to the power of 2

Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley More Library Functions Other Common Mathematical Functions –abs calculates the absolute value of a number –cos returns the cosign of an argument –round rounds to the nearest integer –sin returns the sine of an argument –tan returns the tangent of an argument

Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley More Library Functions Data Type Conversion Functions –Library functions that convert values from one data type to another toInteger converts a real to an integer toReal converts an integer to a real –Real numbers can store integers –Integers cannot store real numbers –Type mismatch errors will occur without converting values

Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley More Library Functions Formatting Functions –Allow to format a number in a certain way –currencyFormat will be used to format a number to a currency Declare Real amount = Display currencyFormat(amount) –Display would be $6,450.88

Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley More Library Functions String Functions –Allow for working with strings –length function returns the length of a function –append function joins multiple strings together –toUpper and toLower converts a string to upper or lower case –substring can extract a character or a portion of a string out of a string

Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley More Library Functions String Functions –contains identifies similar strings within two strings –stringToInteger and stringToReal converts string that stores a number, to a number data type –isInteger and isReal test numbers to see if it can be converted to a string