Brent M. Dingle Texas A&M University Chapter 5 – Section 2-3

Slides:



Advertisements
Similar presentations
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Advertisements

Chapter 7: User-Defined Functions II
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Loops – While, Do, For Repetition Statements Introduction to Arrays
1 Introduction to Computers and Programming Quick Review What is a Function? A module of code that performs a specific job.
 Wednesday, 9/25/02, Slide #1 CS106 Introduction to CS1 Wednesday, 9/25/02  QUESTIONS??  Today: More on functions  Reading: Chapter 3 through 3.7 
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
CS241 PASCAL I - Control Structures1 PASCAL I - Control Structures Philip Fees CS241.
Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding.
Chapter 6: Functions.
Fortran- Subprograms Chapters 6, 7 in your Fortran book.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
CSCI 130 Chapter 5 Functions. Functions are named uniquely Performs a specific task Is independent –should not interfere with other parts of program May.
David Stotts Computer Science Department UNC Chapel Hill.
Pascal Programming Local Identifiers, Functions, Modularity and Data Flow.
LOOP & Type Conversion. do – while Loop In the while loop, the test expression is evaluated at the beginning of the loop. If the test condition is false.
Procedural Programming Criteria: P2 Task: 1.2 Thomas Jazwinski.
JavaScript, Fourth Edition
CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees.
CSCE 102 – Chapter 11 (Performing Calculations) CSCE General Applications Programming Benito Mendoza Benito Mendoza 1 By Benito Mendoza.
FUNCTIONS IN FORTRAN For a complex program difficulties like writing it and debugging are encountered. These can be minimized by breaking main program.
Arithmetic Expressions Addition (+) Subtraction (-) Multiplication (*) Division (/) –Integer –Real Number Mod Operator (%) Same as regular Depends on the.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
Functions Chapter 6. Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules Function: a collection.
Modularity using Functions Chapter 4. Modularity In programming blocks of code often can be "called up" and reused whenever necessary, for example code.
Fall 2001(c)opyright Brent M. Dingle 2001 Simple Sorting Brent M. Dingle Texas A&M University Chapter 10 – Section 1 (and some from Mastering Turbo Pascal.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
Chapter 02 (Part II) Introduction to C++ Programming.
Programming Logic and Design Fifth Edition, Comprehensive Chapter 7 Using Methods.
APS105 Functions (and Pointers) 1. Modularity –Break a program into manageable parts (modules) –Modules interoperate with each other Benefits of modularity:
Fall 2001(c)opyright Brent M. Dingle 2001 Multidimensional Arrays Brent M. Dingle Texas A&M University Chapter 10 – Section 2, part B (and some from Mastering.
Fall 2001(c)opyright Brent M. Dingle 2001 Abstract Data Types (ADTs) Brent M. Dingle Texas A&M University Chapter 8 – Sections 2 and 3 (and some from Mastering.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Constructors and Destructors
Functions + Overloading + Scope
Chapter 9: Value-Returning Functions
Chapter 7: User-Defined Functions II
CSE 220 – C Programming C Fundamentals.
Introduction to Computer Science / Procedural – 67130
Programming the Web using XHTML and JavaScript
CMPT 201 Functions.
Function There are two types of Function User Defined Function
Topics Introduction to Repetition Structures
Chapter 6: Functions Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Arithmetic Operator Operation Example + addition x + y
(c)opyright Brent M. Dingle 2001
Chapter 2 - Introduction to C Programming
Chapter 4 void Functions
CS1100 Computational Engineering
6 Chapter Functions.
Chapter 2 - Introduction to C Programming
Constructors and destructors
Constructors and Destructors
Pascal Subprogram Procedure Function Build in Function (e.g. Sin(x))
Chapter 2 - Introduction to C Programming
Turbo Pascal Units (TPU)
Procedures Brent M. Dingle Texas A&M University
Topics Introduction to Functions Defining and Calling a Void Function
Chapter 6 – Methods Topics are:
Topics Introduction to Functions Defining and Calling a Function
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Data Structures and Algorithms Introduction to Pointers
Topics Introduction to Repetition Structures
Review of Previous Lesson
Chapter (3) - Procedures
Functions, Part 2 of 3 Topics Functions That Return a Value
Brent M. Dingle Texas A&M University Chapter 5 – Section 1
Presentation transcript:

Brent M. Dingle Texas A&M University Chapter 5 – Section 2-3 Functions Brent M. Dingle Texas A&M University Chapter 5 – Section 2-3

What is a function? Simplistically a function is a procedure which returns a value. You have been using functions for some time now, for example: trunc round sqrt etc.

Function Declaration Form Functions take the form: FUNCTION [name] (param list) : [type returned by the function]; CONST {declarations} VAR BEGIN { body } [name] := [ some value ]; END;

Function Declaration Example FUNCTION CubeIt( x: integer) : integer; Begin CubeIt := x * x * x; End;

Differences between procedures and functions A function is given a type in the function declaration heading. Somewhere within the body of the function declaration, the function name must appear on the left-hand side of an assignment statement.

Arguments versus Parameters Arguments and parameters are more or less the same thing. The only distinction is that arguments are commonly associated with functions and parameters are associated with procedures.

Challenge (not graded) Write a program that breaks a number into the sum of powers of 2 + (some small extra) if needed. e.g. 800 = 512 + 256 + 32 e.g. 803 = 512 + 256 + 32 + 2 + 1 Write a program that has a function to calculate x^n. =) Can you use the first program to “speed up” things in the second program? Consider: x^4 = (x^2)^2 x^16 = (x^4)^2 x^32 = (x^16)^2 … x^512 = (x^256)^2

Function Rule ! A function should NEVER change the value of anything outside the function! A well written function will never change the value of anything outside the function.

Function guidelines Functions should only return ONE value. Don’t use variable parameters to return values when you are creating functions. Functions should not have any variable parameters. i.e. Whenever possible send value parameters to functions – this decreases the chances of accidentally changing something outside the function. Functions should not input or output anything.

Side effect If a function changes the value of a global variable (or a variable outside its own scope) sets the value of a variable parameter or causes a read or write statement to be executed. that extra feature is referred to as a side effect. Avoid side effects !

Locals Functions may have local constants, local variables, local procedures, local functions. For the most part I would discourage using local procedures and local functions, but know that they can exist and there may be scenarios where they are useful.

Common Error Within a given function called [name], the function name cannot appear on the right hand side of an assignment operator. For example: Function Graham( x: real) : real; VAR y : real; BEGIN … y := Graham * 4.5; { This line is an ERROR } END; [name] however can appear multiple times on the left hand side of an assignment operator. In fact it MUST so appear at least once.

READ SECTION 5.3 !!! You should read section 5.3

Suggested Exercises (not graded) page 184: 13, 14, 15, 16, 17 page 193: 22 programs: page 195: 23, 24 page 196: 27, 28, 30 page 197: 35, 37

End Functions Thus ends chapter 5.