CS201 – Introduction to Computing – Sabancı University 1 First Midterm Exam l November 22, 2008, Saturday, 10:40 – 12:20, max 100 minutes l One A4 size.

Slides:



Advertisements
Similar presentations
Computer Programming w/ Eng. Applications
Advertisements

Parameter Passing Mechanisms Reference Parameters.
1 CS 105 Lecture 11 Arrays Version of Wed, Apr 6, 2011, 6:20 pm.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 5. Functions.
Wednesday, 10/23/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 10/23/02  QUESTIONS??  Today:  Discussion of HW #3  The const modifier for functions.
What Data Do We Have? Sections 2.2, 2.5 August 29, 2008.
1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
1 11/05/07CS150 Introduction to Computer Science 1 Functions Chapter 6, page 303.
CS Feb 2007 Chap 6. Functions General form; type Name ( parameters ) { … return value ; }
 Monday, 9/30/02, Slide #1 CS106 Introduction to CS1 Monday, 9/30/02  QUESTIONS (on HW02, etc.)??  Today: Libraries, program design  More on Functions!
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
CS 1400 Chap 6 Functions. Library routines are functions! root = sqrt (a); power = pow (b, c); function name argument arguments.
Chapter 7 Functions.
Classes: From Use to Implementation
Functions Parameters & Variable Scope Chapter 6. 2 Overview  Using Function Arguments and Parameters  Differences between Value Parameters and Reference.
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
Functions Pass by Reference Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005.
Announcements HW3 grades will be announced this week HW4 is due this week Final exam on August 25, 2010, Wednesday at 09:00 Duration is about 140 minutes.
Parameter Passing Mechanisms Reference Parameters Read § §
1 Advanced Issues on Classes Part 3 Reference variables (Tapestry pp.581, Horton 176 – 178) Const-reference variables (Horton 176 – 178) object sharing:
CPS120: Introduction to Computer Science Functions.
Software Design 8.1 A Rose by any other name…C or Java? l Why do we use Java in our courses (royal we?)  Object oriented  Large collection of libraries.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Parameter Passing Mechanisms Reference Parameters § §
CPS120: Introduction to Computer Science Lecture 14 Functions.
CSIS 113A Lecture 8 Parameters.  Two methods of passing arguments as parameters  Call-by-value  ‘copy’ of value is passed  Call-by-reference  ‘address.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
Reference parameters (6.2.3) We might need a function to return more than one value Find roots of a quadratic 2 return values. What are they? Get first.
Functions Overview Functions are sequence of statements with its own local variables supports modularity, reduces code duplication Data transfer between.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 Introduction An array is a collection of identical boxes.
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);
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
CS201 – Introduction to Computing – Sabancı University 1 Built-in Arrays l C++ native array type (not the class version) l Two versions ä fixed size arrays.
A Computer Science Tapestry 6.1 Classes: From Use to Implementation l We’ve used several classes, a class is a collection of objects sharing similar characteristics.
1 09/03/04CS150 Introduction to Computer Science 1 What Data Do We Have.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
Reference Parameters There are two ways to pass arguments to functions: pass- by-value and pass-by-reference. pass-by-value –A copy of the arguments’svalue.
Struct s (7.4) Used as data aggregates for an entity can be different types of data e.g. for student id, name, GPA, address,... Similar to classes, but.
Extra Recitations Wednesday 19:40-22:30 FENS L055 (tomorrow!) Friday 13:40-16:30 FENS L063 Friday 17: :30 FENS L045 Friday 19:40-22:30 FENS G032.
1 Parameter passing Call by value The caller evaluates the actual parameters and passes copies of their values to the called function. Changes to the copies.
C++ Programming Lecture 12 Functions – Part IV
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 7 Pointers and C-Strings.
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 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
Pointers and Pointer-Based Strings
New Structure Recall “average.cpp” program
Arrays in C.
Reference parameters (6.2.3)
Multiple Files Revisited
Pass by Reference, const, readonly, struct
Arrays Kingdom of Saudi Arabia
6 Chapter Functions.
CS150 Introduction to Computer Science 1
Functions Pass By Value Pass by Reference
CS150 Introduction to Computer Science 1
Let’s all Repeat Together
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Pointers and Pointer-Based Strings
Arithmetic Operations
CS150 Introduction to Computer Science 1
Based on slides created by Bjarne Stroustrup & Tony Gaddis
CS150 Introduction to Computer Science 1
CS250 Introduction to Computer Science II
CS150 Introduction to Computer Science 1
Presentation transcript:

CS201 – Introduction to Computing – Sabancı University 1 First Midterm Exam l November 22, 2008, Saturday, 10:40 – 12:20, max 100 minutes l One A4 size cheat-note allowed (both sides could be used) l Closed book, closed notes, no calculators and no laptops l Until the end of loops ä up to 5.4 from book (excluding 4.6.3) ä but you are responsible everything covered in class even if not covered in book e.g. robot class (all member functions; not only the ones you used in hw) several examples l Problem set and solutions are up on the web

CS201 – Introduction to Computing – Sabancı University 2 First Midterm Exam – Exam Places if (lastname >= "Acar" && lastname = "Başaran" && lastname = "Cev" && lastname = "Çokuysal" && lastname = "Konukoğlu" && lastname = "Onur" && lastname = "Özüpekoğlu" && lastname = "Su" && lastname = "Tomaç" && lastname <= "Zilan") cout << " FASS G062 amfi" << endl;

CS201 – Introduction to Computing – Sabancı University 3 Reference parameters (6.2.3) l It’s useful for a function to return more than one value ä Find roots of a quadratic two return values. What are they? ä Get first and last name of a user and return them two return values Functions are limited to one return value using return l If you need to return several values back from a function ä Use reference parameters ä The idea is that when the value of the parameter is modified function modifies the value of the corresponding argument as well

CS201 – Introduction to Computing – Sabancı University 4 Value Parameters - Pass by value l The parameters we have seen so far are value parameters ä their arguments are passed by value ä if you change the value of a parameter in function, corresponding argument does NOT change in the caller function Example: see passbyvalue.cpp (not in book) parameter a changes in function average, but corresponding argument, num1, does not change in main Enter two integers: in main before calling average: num1 = 10, num2 = 15 beginning of function average: a = 10, b = 15 end of function average: a = 25, b = 15 average is 12.5 in main after calling average: num1 = 10, num2 = 15

CS201 – Introduction to Computing – Sabancı University 5 Reference Parameters – Pass by Reference l Reference parameters are used to change the value of the argument in the caller function as well ä ampersand between type and name void myfunction (int & num) ä if you change the value of a reference parameter in function, corresponding argument changes in the caller function ä Argument of a reference parameter must be a variable This is a reasonable rule, because otherwise its value wouldn’t change ä See passbyreference.cpp (not in book) parameter a changes in function average; corresponding argument, num1, changes in main as well Enter two integers: in main before calling average: num1 = 10, num2 = 15 beginning of function average: a = 10, b = 15 end of function average: a = 25, b = 15 average is 12.5 in main after calling average: num1 = 25, num2 = 15

CS201 – Introduction to Computing – Sabancı University 6 Underlying Mechanisms l For value parameters, the arguments’ values are copied into parameters ä arguments and parameters have different memory locations double average (int a, int b) average(num1, num2) copy value main function

CS201 – Introduction to Computing – Sabancı University 7 Underlying Mechanisms l For reference parameters, the parameter and the argument share the same memory location ä parameter is an alias of argument double average (int & a, int b) average(num1, num2) refers to the same memory location copy value main function

CS201 – Introduction to Computing – Sabancı University 8 Example: Swap l Write a function that swaps the values of its two integer parameters void swap (int & a, int & b) { int temp; temp = a; a = b; b = temp; } l How do we use this in main? int a=5, b=8; swap (a,b); // a becomes 8, b becomes 5 swap(a,5); // syntax error, arguments must be variables

CS201 – Introduction to Computing – Sabancı University 9 Examples l What’s prototype for a void function that takes a string as parameter and returns the number of vowels and consonants in it. void letters (string s, int & vowels, int & consonants); l What’s prototype for a void function that returns the number of hours, minutes in N seconds. Where N is a parameter? void TimeConversion (int N, int & hours, int & minutes);

CS201 – Introduction to Computing – Sabancı University 10 Reference Parameters are not only to return multiple values Even if you have a single value to return, you may prefer to return it as a reference parameter, not as the return value of the function.  ToLower function, defined in strutils, changes its argument to all lowercase. It is actually a void function, i.e. does not return anything as the function’s return value ä Prototype is void ToLower(string & s);  Example use string s = "HeLLo"; ToLower(s); // s becomes “hello”

CS201 – Introduction to Computing – Sabancı University 11 Example (See roots.cpp) l Roots of quadratic equation ax 2 + bx + c = 0 l what could be a prototype? void roots (double a, double b, double c, double & r1, double & r2); l What happens if ä one root? ä no roots? l how will you inform the caller function about the number of roots? ä necessary in order to let the caller function to interpret arguments for r1 and r2 l Solution: let the function return (as the return value) an integer value for the number of roots ä So, the prototype becomes int roots (double a, double b, double c, double & r1, double & r2);

CS201 – Introduction to Computing – Sabancı University 12 Parameter Passing: const-reference l Pass by value (value parameters) has overheads ä copying the value ä memory allocation for both parameter and argument l Sometimes we want to avoid the overhead of making the copy, but we don’t want to allow the argument to be changed. ä const-reference parameters avoid copies, but cannot be changed in the function trying to change a const-reference parameter is a syntax error  defined with const before a reference parameter void demo (const int & num, const string & s);

CS201 – Introduction to Computing – Sabancı University 13 Example l Count # occurrences of a letter in a string ä write a function for it ä Look at every character in the string int LetterCount(const string& s, const string& letter) // post: return number of occurrences of letter in s { int k, count = 0, len = s.length(); for(k=0; k < len; k++) { if (s.substr(k,1) == letter) { count++; } return count; } l Calls below are legal int ec = LetterCount( " elephant ", " e " ); string s = " hello " ; cout << LetterCount(s, " a " );

CS201 – Introduction to Computing – Sabancı University 14 General rules for Parameters l Const-reference parameters allow constants and literals to be passed ä For example, “elephant” cannot be passed as an argument for a reference parameter, but it is ok with const-reference l Some good-programming tips ä Built-in types (int, double, bool, char) - pass by value unless you change it and return from the function ä All other types - pass by const-reference unless you change it and return from the function ä When you change and eant to return the changed value, use reference parameters l What about using classes as the parameter type? ä use reference parameters if changing the state of the parameter object that is why we use reference parameters for Robot objects ä use const reference if you are not changing the state of the parameter object