Chapter 7 Functions. Types of Functions Value returning Functions that return a value through the use of a return statement They allow statements such.

Slides:



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

Spring Semester 2013 Lecture 5
Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Functions ROBERT REAVES. Functions  Interface – the formal description of what a subprogram does and how we communicate with it  Encapsulation – Hiding.
1 Lecture 10 Chapter 7 Functions Dale/Weems/Headington.
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.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 7: User-Defined Functions II.
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.
C++ Functions CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
1 Chapter 8 Scope, Lifetime, and More on Functions Dale/Weems/Headington.
Functions Modules in C++ are called functions and classes
Chapter 7 Functions.
Chapter 4 Procedural Abstraction and Functions That Return a Value.
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.
Functions Parameters & Variable Scope Chapter 6. 2 Overview  Using Function Arguments and Parameters  Differences between Value Parameters and Reference.
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.
Chapter 6: User-Defined 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.
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.
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.
Programming in C++ Language ( ) Lecture 5: Functions-Part1 Dr. Lubna Badri.
CPS120: Introduction to Computer Science Functions.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Functions Top-down design Breaking a complex problem into smaller parts that we can understand is a common practice. The process of subdividing a problem.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
CPS120: Introduction to Computer Science Lecture 14 Functions.
FUNCTIONS (a) Value returning e.g. int main() ….…. return 0; (b) Void (returning) no return statements example To print this message **** ** Welcome.
User Defined Functions Chapter 7 2 Chapter Topics Void Functions Without Parameters Void Functions With Parameters Reference Parameters Value and Reference.
Chapter 7 Functions CS185/09 - Introduction to Programming Caldwell College.
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.
Cosc175/module.ppt1 Introduction to Modularity Function/procedures void/value-returning Arguments/parameters Formal arguments/actual arguments Pass by.
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
Chapter 3 Top-Down Design with Functions Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
1 MODULAR DESIGN AND ABSTRACTION. 2 SPECIFYING THE DETAILS OF A PROBLEM INTO A RELATED SET OF SMALLER PROBLEMS.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Functions Chapter 6. Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules Function: a collection.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Functions Functions, locals, parameters, and separate compilation.
1 Chapter 9 Scope, Lifetime, and More on Functions.
1 Scope Lifetime Functions (the Sequel) Chapter 8.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
Programming Fundamentals Enumerations and Functions.
CHAPTER 8 Scope, Lifetime, and More on Functions.
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.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
Intro Programming in C++ Computer Science Dept Va Tech August, 2001 © Barnette ND & McQuain WD 1 Pass-by-Value - default passing mechanism except.
Chapter 8 Functions.
Chapter 9: Value-Returning Functions
Chapter 7: User-Defined Functions II
User-Defined Functions
Chapter 9 Scope, Lifetime, and More on Functions
User Defined Functions
User Defined Functions
Chapter 7: User-Defined Functions II
Chapter 9: Value-Returning Functions
In C Programming Language
Predefined Functions Revisited
Chapter 8 Functions.
Presentation transcript:

Chapter 7 Functions

Types of Functions Value returning Functions that return a value through the use of a return statement They allow statements such as this: Y = 3.8 * sqrt( x ); Void Functions They do not return anything through their return statement cin.get( someChar );

When do you create Functions? One simple heuristic to use If the overall program is easier to understand as a result of creating a function, then the function should be created.

Void Functions #include void Print2Lines(); void Print4Lines(); int main (){ Print2Lines(); std::cout << “ Welcome Home!\n”; Print4Lines(); return 0; }

More Void void Print2Lines() { std::cout << “********************\n”; }

One More void Print4Lines() { std::cout << “********************\n”; }

Flow of Control Functions definitions can appear in any order in the source file main usually comes first Flow of control begins with the first line in main and continues until a function invocation Flow is passed to the function When the function is done, flow returns to the first statement after the function call

Parameters Can the first program be written better? How? Use a parameter to indicate how many lines to print

Types of Parameters Argument A variable or expression listed in a call to a function. Also called actual argument or actual parameter Parameter A variable declared in a function heading. Also called formal argument or formal parameter

New Welcome program void PrintLines( int ); int main() { PrintLines( 2 ); std::cout << “Welcome Home!\n”; PrintLines( 4 ); return 0; }

New PrintLines void PrintLines( int numLines ) { int count = 0; while ( count < numLines ) { std::cout << “********************\n”; } }

Syntax and Semantics Function Call: a statement that transfers control to a void function. FunctionName ( ArgumentList ); ArgumentList is a comma separated list of values and variables

Declarations and Definitions Function prototype: A function declaration without the body of the function Does not allocate memory Function definition: A function declaration that includes the body of the function Allocates memory

Local Variables Any function you write can also have variables Those variables that are inside of a function are called local variables They go away when the function ends

Global Variables Variables that are declared outside of any functions are called Global variables Any function can use global variables Global variables should be avoided

Return Statement void SomeFunc( int n ) { if ( n > 50 ) { cout << “The value is out of range.”; return; } n = 412 * n; cout << n; }

Return Again void SomeFunc( int n ) { if ( n > 50 ) cout << “The value is out of range.”; else { n = 412 * n; cout << n; } }

Single-Entry / Single-Exit What’s the difference in the preceding two examples? The return You can argue the each example is a better approach I prefer the second; It uses Single- entry/single-exit

Parameters Two types Value Parameter Simply pass information in to a function Reference Parameter Can pass information in and receive information from a function

Value Parameters void PrintLines ( int numLines ) When invoke like this: PrintLines ( LineCount ) The function receives a copy of the variable LineCount You can put anything in the function call that produces a value consts, expressions, variables

Reference Parameters void CountandSum( int List, int &Sum) When invoked like: CountandSum(MyList, sum); The function receives the MyList by value and sum by reference. The function is now allowed to inspect and change sum and the calling function will be aware of the changes The & actually gives the function the address of the variable

Designing Functions Two Key Ideas in Programming Interface A connecting link at a shared boundary that permits independent systems to meet and act on or communicate with each other. Encapsulation Hiding a module’s implementation in a separate block with a formally specified interface

More on Designing Need a list of incoming values, outgoing values, and incoming/outgoing values Decide which of the values from the list need to be given to the function These values are declared in the function heading with the appropriate passing mechanism All others are local variables

Writing Assertions as Comments void PrintAverage ( float sum, int count ) //Precondition: //sum is assigned && count > 0 //Postcondition: //The average sum/count has been // outputted on one line { cout << “Average is “ << sum/float (count); }

Flow of Data Data Flow The flow of information from the calling code to a function and from the function back to the calling code. Two Directions/Three Data Flows In Out In/Out

Data Flow and Passing Mechanism Data Flow for a Parameter Incoming Outgoing Incoming/Outgoing Argument-Passing Mechanism Pass-by-value Pass-by-reference

Quiz What are the two passing mechanisms for passing a variable to a function? What does the & return for a variable? Why might you want to use pass-by- reference?