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);

Slides:



Advertisements
Similar presentations
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
Advertisements

Functions  Programmer-Defined Functions  Local Variables in Functions  Overloading Function Names  void Functions,  Call-By-Reference Parameters in.
1 ACS 168 Structured Programming Using the Computer Chapter 4 by Joaquin Vila Prepared by Shirley White.
Computer Science 1620 Loops.
Computer Programming 1 Functions. Computer Programming 2 Objectives Take a first look at building functions Study how a function is called Investigate.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 5 Functions for All Subtasks.
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.
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
Functions Parameters & Variable Scope Chapter 6. 2 Overview  Using Function Arguments and Parameters  Differences between Value Parameters and Reference.
Classes Representing Non-Trivial Objects. Problem Write a program that reads a temperature (either Fahrenheit or Celsius), and displays that same temperature.
Chapter 5 Functions for All Subtasks. Slide 5- 2 Overview 5.1 void Functions 5.2 Call-By-Reference Parameters 5.3 Using Procedural Abstraction 5.4 Testing.
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is a legal identifier? what.
Chapter 5 Functions For All Subtasks. Void functions Do not return a value. Keyword void is used as the return type in the function prototype to show.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
CONTROLLING PROGRAM FLOW
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.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
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.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
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.
Function ( I ) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
CSCI 171 Presentation 6 Functions and Variable Scope.
Review 1 List Data Structure List operations List Implementation Array Linked List.
CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees.
By Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 13 More on Classes Chapter 8 Week 13 More on Classes Chapter 8.
Instructor - C. BoyleFall Semester
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
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.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Functions (2)
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 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
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.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
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?
Function Parameters and Overloading Version 1.0. Topics Call-by-value Call-by-reference Call-by-address Constant parameters Function overloading Default.
Chapter Topics The Basics of a C++ Program Data Types
What Is? function predefined, programmer-defined
Functions for All Subtasks
Dr. Shady Yehia Elmashad
Chapter 5 Functions for All Subtasks 1
Chapter 5 Functions for All Subtasks 1
Predefined Functions Revisited
Basic Elements of C++.
Variables A piece of memory set aside to store data
Programmer-Defined Functions, Call-by-Value, Multiple Files Lab 5
Dr. Shady Yehia Elmashad
Basic Elements of C++ Chapter 2.
Dr. Shady Yehia Elmashad
Multiple Files Revisited
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
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
Presentation transcript:

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); l what is a header file? l what is the difference between the two statements? #include and #include ”filename.h” l why are programs included in multiple files what are object files and how are they related to multiple file-program l what is linking? l what is multiple inclusion protection and why is it needed? Multiple Files Revisited 1

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

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

l predicate – function whose return value is boolean l used to compute a binary decision l idiom – use a boolean function 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; } Predicates 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 ? 4

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

l what was call-by-value? l call-by-reference is a parameter passing discipline that allows the function to modify the arguments to distinguish call-by-reference ampersand ( & ) precedes parameter declaration both in function head and in function prototype n function call is not distinguishable l prototype void getIntput(double &fard); // extended form void getIntput(double &); // abbreviated form definition void getIntput(double &fard){ cout << ”I will convert Fahrenheit Temperature ” << ”to Celsius.\n” << ”Enter temperature in Fahrenheit: ”; << ” degrees Celsius.\n”; cin >> fard; } mixing calls of parameters is allowed: myfunc(int&, double, int&); Call-by-Reference 6

l function invocations for call-by-reference and call-by-value are similar but not the same: double temp; getInput(temp); l passing expressions by reference is not allowed! getInput(23.0); // WRONG! l in call-by-reference the function operates on the memory location of the argument l 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); l 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 Call-by-Reference (Cont.) 7

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