Function “Inputs and Outputs”

Slides:



Advertisements
Similar presentations
Chapter 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
Advertisements

CSE202: Lecture 12The Ohio State University1 Function Calling.
Functions ROBERT REAVES. Functions  Interface – the formal description of what a subprogram does and how we communicate with it  Encapsulation – Hiding.
AU/MITM/1.6 By Mohammed A. Saleh 1. Arguments passed by reference  Until now, in all the functions we have seen, the arguments passed to the functions.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 6 Functions.
1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
Macro & Function. Function consumes more time When a function is called, the copy of the arguments are passed to the parameters in the function. After.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 6 Functions.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Functions Pass by Reference Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005.
CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
1 CS161 Introduction to Computer Science Topic #10.
Built-In and user-Defined functions Software Design Concepts Lecture IV Dr. Sothy Vignarajah.
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 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
Starting Out with C++ Early Objects ~~ 7 th Edition by Tony Gaddis, Judy Walters, Godfrey Muganda Modified for CMPS 1044 Midwestern State University 6-1.
1 Brief Version of Starting Out with C++, 4th Brief Edition Chapter 6 Functions.
#include using namespace std; // Declare a function. void check(int, double, double); int main() { check(1, 2.3, 4.56); check(7, 8.9, 10.11); } void check(int.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Brief Edition Chapter 6 Functions.
Functions Chapter 6. Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules Function: a collection.
Function prototype A function must be declared before it can be referenced. One way to declare a function is to insert a function prototype before the.
Function 2. User-Defined Functions C++ programs usually have the following form: // include statements // function prototypes // main() function // function.
Lecture 5 functions 1 © by Pearson Education, Inc. All Rights Reserved.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
Variables  A piece of memory set aside to store data  When declared, the memory is given a name  by using the name, we can access the data that sits.
1 CSC103: Introduction to Computer and Programming Lecture No 16.
User-Defined Functions (cont’d) - Reference Parameters.
Chapter 6 Functions. 6-2 Topics 6.1 Modular Programming 6.2 Defining and Calling Functions 6.3 Function Prototypes 6.4 Sending Data into a Function 6.5.
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Pointers Call-by-Reference.
Constructors and Destructors
User-Written Functions
Chapter 10: Void Functions
A Lecture for the c++ Course
Variables A piece of memory set aside to store data
Pointers and Pointer-Based Strings
Chapter 6: Functions Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Pointers and References
Object Oriented Programming COP3330 / CGS5409
CMSC202 Computer Science II for Majors Lecture 04 – Pointers
Functions A function is a “pre-packaged” block of code written to perform a well-defined task Why? Code sharing and reusability Reduces errors Write and.
6 Chapter Functions.
Constructors and Destructors
Functions Pass By Value Pass by Reference
5.1 Introduction Pointers Powerful, but difficult to master
CS150 Introduction to Computer Science 1
The Function Prototype
CS150 Introduction to Computer Science 1
Pointers and Pointer-Based Strings
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Instructor: Hidayah Elias Course: COMPUTER PROGRAMMING (BFC 2042)
Pointers and References
Class Circle { Float xc, yc, radius;
C Parameter Passing.
Presentation transcript:

Function “Inputs and Outputs” Variables used as arguments to a function are, effectively, “inputs” only Why? - because arguments to functions are “passed by value” Variable changes inside the function don’t affect the calling arguments But can have an unlimited number of “inputs” Return value of a function serves as its “output” Only one return value - so, function is limited to one output!

“Pass by reference” A mechanism for passing unlimited amounts of information to and from a function Arguments can be made to be inputs or outputs (actually both) Makes use of reference variables Return value is retained

Reference Variable A reference variable is a variable that “refers” to another, already existing, variable It is a separate variable with its own name, but… It serves as an “alias“ to the existing variable So, changing the value of either the original or the reference variables will change the other Reference shares the same storage (memory) location as the original variable

Reference Declaration To declare a reference variable: place “&” between the variable type and name int &iref = i; // iref is a reference to i float& piref= pi; // piref is a reference to pi Important notes: Reference variable must be “initialized” upon creation References cannot be changed after creation “&” can be placed anywhere between the type and name Spaces don’t matter – next to either type or variable name

Variable storage For each variable, compiler needs to keep track of 4 pieces of information: Variable name Location of data Memory address of stored data Variable type How to interpret data How many bytes? Value Stored at the memory address Memory locations … ? 0x1000 0x1001 3.14159 0x1002 0x1003 0x1004 0x1005 float pi= 3.14159; Variable Name pi Location 0x1001 Type float

Reference storage float pi= 3.14159; float &piref= pi; Memory locations … ? 0x1000 0x1001 3.14159 0x1002 0x1003 0x1004 0x1005 float pi= 3.14159; float &piref= pi; Variable Name pi Location 0x1001 Type float Variable Name piref Location 0x1001 Type float & Different variables with different names, but… Reference uses the same storage location as the original variable

Functions, local variables and “scope” All variables in a function are “local” to the function Including both parameters (in the “input” list) And variables declared in internal function statements They are created “on-the-fly” when the function is invoked They are destroyed when the function returns (finishes) They have local scope Any modifications to a local variable do not affect variables in the calling routine even if they have the same name!

“Normal” Function usage // prototype float circle(float); main() { float area; // call the function area= circle(3.0); cout << area << endl; } // function definition / compute area of a circle float circle(float r) { float pi= 3.14159; return pi*r*r; } r and pi are local to circle() i.e. have local scope

Example 1: “pass by reference” This will compute and output both the area and the perimeter of the circle! // prototype float circle(float, float&); main() { float area, perim; // call the function area= circle(3.0, perim); cout << area << endl; cout << perim << endl; } // function definition // compute area and perimeter float circle(float r, float &p) { float pi= 3.14159; p= 2.0*pi*r; return pi*r*r; }

Example 2: “pass by reference” // prototype void circle(float, float&, float&); main() { float area, perim; // call the function circle(3.0, area, perim); cout << area << endl; cout << perim << endl; } Don’t need to use a return value! // function definition / compute area and perimeter void circle(float r, float &a, float &p) { float pi= 3.14159; p= 2.0*pi*r; a= pi*r*r; }

“pass by reference” usage Both the function prototype and definition must declare parameters as reference variables Prototype need not have variable names Arguments (when the function is “called”) do NOT use an “&” So, can’t tell from a function call whether the parameters or references or not! Cannot use literals as arguments with a reference parameter Must have a variable to “refer to”!!!