Functions ROBERT REAVES. Functions  Interface – the formal description of what a subprogram does and how we communicate with it  Encapsulation – Hiding.

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

1 Lab 4 Westfield High School APCS LAB 4 Parameters, apvectors, structs.
1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
Subprogram Control - Data sharing Mechanisms to exchange data Arguments - data objects sent to a subprogram to be processed. Obtained through  parameters.
Functions, Interfaces, Parameters 8.1 – 8.2. include using namespace std; int main ( ) { int cal, fatGrams, fatCal; float fatPercent; cout
1 Lecture 10 Chapter 7 Functions Dale/Weems/Headington.
Chapter 6: User-Defined Functions I
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
1 Chapter 7 Functions Dale/Weems/Headington. 2 Functions l Control structures l every C++ program must have a function called main l program execution.
CS 201 Functions Debzani Deb.
Chapter 6: User-Defined Functions I
1 Chapter 8 Scope, Lifetime, and More on Functions Dale/Weems/Headington.
Functions Modules in C++ are called functions and classes
Chapter 7 Functions.
1 Chapter 7 Functions. 2 Chapter 7 Topics l Writing a Program Using Functional Decomposition l Writing a Void Function for a Task l Using Function Arguments.
Modular Programming Chapter Value and Reference Parameters t Function declaration: void computesumave(float num1, float num2, float& sum, float&
1 Chapter 9 Scope, Lifetime, and More on Functions.
1 Programming in C++ Dale/Weems/Headington Chapter 7 Functions.
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
1 Chapter 8 Scope, Lifetime, and More on Functions Dale/Weems/Headington.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Chapter 6: User-Defined Functions
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
Functions Why we use functions C library functions Creating our own functions.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
1 Functions Chapter 7 2 Hope you can function! What is R2D2 doing here? What is his function? Who is Nibble? Can he function? IS he a function? Who is.
Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence.
1 Functions every C++ program must have a function called main program execution always begins with function main any other functions are subprograms and.
Chapter 8 Functions. Chapter 8 Topics l Writing a Program Using Functional Decomposition l Writing a Void Function for a Task l Using Function Arguments.
Functions Modules in C++ are called functions and classes Functions are block of code separated from main() which do a certain task every C++ program must.
1 Functions. 2 Chapter 7 Topics  Writing a Program Using Functional Decomposition  Writing a Void Function for a Task  Using Function Arguments and.
CPS120: Introduction to Computer Science Functions.
Parameter Passing Mechanisms Reference Parameters § §
CPS120: Introduction to Computer Science Lecture 14 Functions.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 6 User-Defined Functions I. Objectives Standard (predefined) functions What are they, and How to use them User-Defined Functions Value returning.
Day Class Definitions and Methods Local & global variables, parameters & arguments,
Chapter 7 Functions. Types of Functions Value returning Functions that return a value through the use of a return statement They allow statements such.
User Defined Functions Chapter 7 2 Chapter Topics Void Functions Without Parameters Void Functions With Parameters Reference Parameters Value and Reference.
COMPUTER PROGRAMMING. Functions’ review What is a function? A function is a group of statements that is executed when it is called from some point of.
1 Chapter 7 Functions Dale/Weems/Headington. 2 Chapter 7 Topics l Writing a Program Using Functional Decomposition l Writing a Void Function for a Task.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
Chapter 3 Top-Down Design with Functions Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Lecture 5 functions 1 © by Pearson Education, Inc. All Rights Reserved.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
1 Chapter 9 Scope, Lifetime, and More on Functions.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
Lecture 4 – Function (Part 1) FTMK, UTeM – Sem /2014.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
1 Chapter 8 Scope, Lifetime, and More on Functions CS185/09 - Introduction to Programming Caldwell College.
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
Defining Classes and Methods
Chapter 8 Functions.
-Neelima Singh PGT(CS) KV Sec-3 Rohini
Bill Tucker Austin Community College COSC 1315
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
Chapter 9 Scope, Lifetime, and More on Functions
Defining Classes and Methods
Defining Classes and Methods
Defining Classes and Methods
Chapter 8 Functions.
Presentation transcript:

Functions ROBERT REAVES

Functions  Interface – the formal description of what a subprogram does and how we communicate with it  Encapsulation – Hiding a module implementation in a separate block with a formally specified interface.  Module parameter list – a set of variables within a module that hold values that are either incoming to the module or outgoing to the caller, or both.  Data flow – the direction of flow of information between the caller and a module through each parameter.

Communicating with a module  Incoming values are values received from the caller.  Outgoing values are values produced and returned to the caller.  Incoming/outgoing values are values that the caller has that the module changes. (receives and returns.)

Writing functions  void x();  This would come before main and is called the function prototype.  Must include the data types of the parameters for this function.  x();  This would be invoked where ever you needed to call your function, called function call.  void x() {  // some code.  }  This would come after main and the “some code” would be what your function did. Called the function definition.

Void Functions  Do NOT return a value to caller.  void x() { // after the name is the parameter list, this one is empty.  // Some code here…  }  “void” signals the compiler that this is not a value-returning function.  Can use inside function:  return; // returns to the caller of the function immediately.

Function Parameters  The code between parentheses that looks like a variable declaration, called parameter declaration.  void x(int y, int t) { // int y and int t would be the parameter declaration.  // some code.  }  Argument is a variable or expression listed in a call to a function.  x(height, width); // height and width would be arguments.  Parameter is a variable declared in a function definition.

Local Variables  Local variable is a variable declared within a block and not accessible outside of that block.  void x();  int main() {  int x = 5; // local variable to main.  x();  return 0;  }  void x() {  cout << x << endl; // will say x is undefined, x is local to main.  }

Parameters  Value parameter – a parameter that receives a copy of the value of the corresponding argument.  void x(int y); // y is a value parameter.  Reference parameter – a parameter that receives the location (memory address) of the caller’s argument.  void x(int& y); // y is a reference parameter.

Documentation  Use preconditions and post-conditions as comments to document function interfaces.  Pre is an assertion describing everything the function requires to be true at the moment the caller invokes the function.  Post describes the state of the program the moment the function finishes executing.  // Pre: sum has been assigned and count is greater than 0.  // Post: The average has been output on one line.  void PrintAverage(float sum, int count) {  cout << “Average is “ << sum / float(count) << endl;  }