Multiple Files Revisited

Slides:



Advertisements
Similar presentations
Computer Science 1620 Loops.
Advertisements

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.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 6: Functions by.
1 Midterm Review COMP 102. Tips l Eat a light meal before the exam l NO electronic devices (including calculators, dictionaries, phones, pagers, etc.)
Basic Elements of C++ Chapter 2.
Chapter 6: Functions.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
Functions Parameters & Variable Scope Chapter 6. 2 Overview  Using Function Arguments and Parameters  Differences between Value Parameters and Reference.
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is a legal identifier? what.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 6 Functions.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
CPS120: Introduction to Computer Science Functions.
CSE 332: C++ execution control statements Overview of C++ Execution Control Expressions vs. statements Arithmetic operators and expressions * / % + - Relational.
CPS120: Introduction to Computer Science Lecture 14 Functions.
L function n predefined, programmer-defined l arguments, (formal) parameters l return value l function call, function invocation l function definition.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
CSC 107 – Programming For Science. Today’s Goal  Discuss writing functions that return values  return statement’s meaning and how it works  When and.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
CSCI 171 Presentation 6 Functions and Variable Scope.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
1 Brief Version of Starting Out with C++, 4th Brief Edition Chapter 6 Functions.
Review 1 List Data Structure List operations List Implementation Array Linked List.
Instructor - C. BoyleFall Semester
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.
L what are predefined functions? l what is? n function name n argument(s) n return value n function call n function invocation n nested function call l.
L what are executable/non-executable statements l out of the ones below which constructs are executable #include p=3.14; const double PI=3.14; int myfunc(int);
Fundamental Programming Fundamental Programming More Expressions and Data Types.
Lecture 2 Functions. Functions in C++ long factorial(int n) The return type is long. That means the function will return a long integer to the calling.
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.
Functions. Predefined Functions C++ comes with libraries of code that can be reused in your programs. The code comes in the form of predefined functions.
Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal
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.
L what is a void-function? l what is a predicate? how can a predicate be used? l what is program stack? function frame? l what’s call-by-value? l what’s.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
L what is a void-function? l what is a boolean function? l is it possible for a function to have no parameters? l what is program stack? function frame?
Chapter 1.2 Introduction to C++ Programming
Test 2 Review Outline.
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
What Is? function predefined, programmer-defined
Dr. Shady Yehia Elmashad
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Predefined Functions Revisited
Basic Elements of C++.
CSE 143 Introduction to C++ [Appendix B] 4/11/98.
Variables A piece of memory set aside to store data
Programmer-Defined Functions, Call-by-Value, Multiple Files Lab 5
Functions, variables, operators, loops Modularity
Dr. Shady Yehia Elmashad
Chapter 6: Functions Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Basic Elements of C++ Chapter 2.
Dr. Shady Yehia Elmashad
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is legal identifier? what.
Previous Lecture Review
Chapter 6: Functions Starting Out with C++ Early Objects Ninth Edition
6 Chapter Functions.
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Engineering Problem Solving with C++ An Object Based Approach
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Fundamental Programming
Multiple Files Revisited
CS150 Introduction to Computer Science 1
Predefined Functions Revisited
What Is? function predefined, programmer-defined
Standard Version of Starting Out with C++, 4th Edition
Presentation transcript:

Multiple Files Revisited what are executable/non-executable statements? out of the ones below, which constructs are executable? #include <iostream> p=3.14; const double pi=3.14; int myfunc(int); what is a header file? How is it different from include file? what is the difference between these two statements? #include <filename> and #include ”filename.h” why are programs included in multiple files what are object files and how are they related to multiple file-program what is linking? how are milti-file programs linked? what is multiple inclusion protection and why is it needed?

Programmer-Defined Functions II Void Functions, Predicates Program Stack, Call-by-Reference

Void Functions void function – does not return a value, can only be used as a standalone statement void is specified as return type return-statement is not necessary; if used, cannot contain expression output functions are often void-functions: void showResults(double fard, double celd){ cout << fard << ” degrees Fahrenheit is equivalent to ” << celd << ” degrees Celsius.\n”; return; // not necessary }

Predicates predicate – function whose return value is boolean used to compute a binary decision idiom: use a predicate as an expression in a looping or branching construct bool again(){ cout << "Again? [y/n] "; char answer; cin >> answer; if (answer == 'y') return true; else return false; } int main(){ do cout << "Hello, World!\n"; while(again()); } how do you code looping construct so that it continues if boolean function returns false rather than true? how do you shorten code for again()?

Program Stack void a(); void b(); void c(); int main(){ a(); } program (call) stack – means of RAM allocation for local function variables last-in/first-out (LIFO) data structure function frame – unit of allocation contains local variables, parameters, return value void a(); void b(); void c(); int main(){ a(); }   void a(){ b(); c(); void b(){ ; // does not do anything }   void c(){ }

Call-by-Reference what is call-by-value again? call-by-reference – parameter passing discipline that allows the function to modify the arguments it assigns parameter the same memory location as argument modification of parameter affects the argument to distinguish call-by-reference ampersand (&) precedes parameter declaration both in function head and in function prototype function call is (almost) indistinguishable ampersand may be next to type int& num1 or next to variable name int &num1 where to put: stylistic issue prototype void getNumbers(int &num1, int &num2); // extended form void getNumbers(int&, int&); // abbreviated form definition void getNumbers(int &num1, int &num2){ cout << ”Input two numbers”; cin >> num1 >> num2; }

Call-by-Reference Example this function swaps the values of arguments void swap (int& left, int& right){ const int temp = left; left = right; right = temp; } what is output when this code is executed? int i=1, j=2; swap(i,j); cout << i << ’ ’ << j;

More on Call-by-Reference function invocations for call-by-reference and call-by-value are similar but not the same: double temp; getInput(temp); only variables may be passed by reference in call-by-reference, the function operates on the memory location of the argument: argument needs to be a variable passing constants or expressions by reference is not allowed getInput(23.0); getInput(temp + 5); // WRONG! mixing call be value and reference is allowed: myfunc(int&, double, int&); functions that need to return more than one value usually use call-by-reference: prototype: void getNumbers(int& input1, int& input2); call: getNumbers(one, two); passing one similar value as return and the other as parameter is bad style: prototype: int getNumbers(int& input2); // BAD call: one=getNumbers(two); // STYLE