File Review Declare the File Stream Object Name –ofstream for output to file –ifstream for input from file Associate a File Name with the File Stream Object.

Slides:



Advertisements
Similar presentations
Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
Advertisements

1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
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.
PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
More on Functions Programming. COMP104 Lecture 19 / Slide 2 Passing Parameters by Reference l To have a function with multiple outputs, we have to use.
1 CS 105 Lecture 9 Files Version of Mon, Mar 28, 2011, 3:13 pm.
Functions:Passing Parameters by Value Programming.
If You Missed Last Week Go to Click on Syllabus, review lecture 01 notes, course schedule Contact your TA ( on website) Schedule.
1 CS 105 Lecture 10 Functions Version of Mon, Mar 28, 2011, 3:13 pm.
1 CS 105 Lecture 3 Constants & Expressions Wed, Jan 26, 2011, 4:15 pm.
1 Chapter 8 Scope, Lifetime, and More on Functions Dale/Weems/Headington.
Functions Modules in C++ are called functions and classes
Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding.
Computer Science 1620 Lifetime & Scope. Variable Lifetime a variable's lifetime is finite Variable creation: memory is allocated to the variable occurs.
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.
Functions Parameters & Variable Scope Chapter 6. 2 Overview  Using Function Arguments and Parameters  Differences between Value Parameters and Reference.
Modular Programming Chapter Value and Reference Parameters t Function declaration: void computesumave(float num1, float num2, float& sum, float&
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
CSC 107 – Programming For Science. Today’s Goal  Discuss writing & using functions  How to declare them, use them, & trace them  Could write programs.
C++ function call by value The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter.
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.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Chapter 8 Scope of variables Name reuse. Scope The region of program code where it is legal to reference (use) a variable The scope of a variable depends.
Chapter 7 Functions CS185/09 - Introduction to Programming Caldwell College.
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.
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Quiz 2 Results. What Is Wrong? #include using namespace std int Main() { // Say Hello 4 times for(i == 0; i < 3; i++) { cout >> "Hello World!"
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
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.
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.
Function 2. User-Defined Functions C++ programs usually have the following form: // include statements // function prototypes // main() function // function.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
FUNCTIONS - What Is A Function? - Advantages Function Declaration
CSE 1341 Honors Note Set 2 1. Overview  Java vs. C++  Functions in C++  First Programming Packet  Development Environment 2.
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
1 Manipulators manipulators are used only in input and output statements endl, fixed, showpoint, setw, and setprecision are manipulators that can be used.
ECE 103 Engineering Programming Chapter 31 C Scopes Herbert G. Mayer, PSU CS Status 8/1/2015 Initial content copied verbatim from ECE 103 material developed.
Files Review For output to a file: –FileOutputStream variable initialized to filename (String) and append/not append (boolean) –PrintWriter variable initialized.
CMSC 202 Lesson 6 Functions II. Warmup Correctly implement a swap function such that the following code will work: int a = 7; int b = 8; Swap(a, b); cout.
CSC1201: Programming Language 2 1 Functions. 2 Function declaration: return_type FuncName( Type arg1, Type arg2,….. Type argN) { function body } A program.
CPS120 Introduction to Computer Science Exam Review Lecture 18.
Exam 2 EXAM 2 Thursday!!! 25% of Final Grade Know: loops, switch/case Files Input failure (e.g. scan.hasNextInt())
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
1 Structure of Simple C++ Program Chapter 1 09/09/13.
Lecture 01d: C++ review Topics: functions scope / lifetime preprocessor directives header files C structures ("simple classes")
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
Global & Local Identifiers
Quiz Next Monday.
Extra.
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.
Exam 2 EXAM 2 Thursday!!! 25% of Final Grade
File Review Declare the File Stream Object Name
Functions.
Fundamental Programming
Functions Imran Rashid CTO at ManiWeber Technologies.
CMSC 202 Lesson 6 Functions II.
CS1201: Programming Language 2
TOPIC: FUNCTION OVERLOADING
Presentation transcript:

File Review Declare the File Stream Object Name –ofstream for output to file –ifstream for input from file Associate a File Name with the File Stream Object by Opening the File Read or Write to the File using > Operators Close the File

#include using namespace std; int main() { ifstream input; int loops, integer, i; float decimal; string name; input.open("mydata.txt"); input >> loops; for(i= 0 ; i < loops; i++) { input >> integer; input >> decimal; input >> name; cout << integer << " "; cout << decimal << " "; cout << name << endl; } return(0); } mydata.txt file Jon Bill e9 Mary Smith -3 -4e3 xyz Jon Bill e+010 Mary Smith xyz Output:

Functions Lines of code with a Name() Name() can be executed over and over again in the same program.

Functions int main() { … displayVals(); … displayVals(); … displayVals(); return(0); } displayVals() { cout << …; return; /*back to where we left off */ }

Function Attributes Function Name: Identifier Used to Call Function Function Parameter(s) or Argument(s): Value(s) Passed into Function for Use by Function Code Function Return Value: Value Returned by Function Back to Calling Function

Function Parameters (Arguments) May Pass as Many Parameters as Necessary to Function A Copy of the Value of the Parameter Is Passed to the Function Changing the Value of the Parameter in the Function Does Not Affect the Value of the Original Variable This Is Called Pass-by-Value

Declaring a Function Function Prototype: Declaring a Function and How It Is Called Syntactically Used by the Compiler to Signal Syntax Errors Pseudocode Example: return_type function_name(parameter_type parameter_name); Example: double calculate_avg(int totOfItems, int numItems);

Functions – Return Values Functions Are Typed According to Their Return Values: void, int, double, etc. Functions Are Declared by Function Prototypes (Declarations) Found Above main() Function Returns a Value to Calling Function via return Statement Standard Functions (e.g., Those in STL) Have Function Prototypes in Header Files

Scope: Area of a Program within which a Variable Can Be Referenced Variable Definitions Are Recognized in the Curly Braces in which They Were Defined Variables Declared Outside of Functions Are Recognized from the Point Declaration through the Rest of the Program Variable Scope

Function Attributes #include using namespace std; void printNum(int); // function prototype int main() { int myNumber = 7; printNum(myNumber); // function call printNum(9); return 0; } // begin function definition void printNum(int numToPrint) { cout << numToPrint; }

Another Function Example #include using namespace std; double calcAverage(int total, int numItems); int main() { int allGrades = 974, numStudents = 10; double avgGrade = 0.0; avgGrade = calcAverage(allGrades,numStudents); cout << avgGrade << endl; return 0; } double calcAverage(int total, int numItems) { return (double) total / numItems; }