Computer Programming

Similar documents
Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Chapter 6: User-Defined Functions. Objectives (cont d.) Objectives. Introduction. Predefined Functions 12/2/2016

Computer Programming

Lab Instructor : Jean Lai

Computer Programming

Computer Programming

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

CS 31 Discussion 1A, Week 4. Zengwen Yuan (zyuan [at] cs.ucla.edu) Humanities A65, Friday 10:00 11:50 a.m.

Module 07: Programming in C++

Computer Programming

Lab 2: Pointers. //declare a pointer variable ptr1 pointing to x. //change the value of x to 10 through ptr1

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Computer Programming

(3) Some memory that holds a value of a given type. (8) The basic unit of addressing in most computers.

Call-by-Type Functions in C++ Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture #5

CS 101 Computer Programming and utilization. Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal Rekhi Building IIT Bombay

Computer Programming

Lecture on pointers, references, and arrays and vectors

Chapter 9: Pointers Co C pyr py igh i t gh Pear ea so s n n E ducat ca io i n, n Inc. n c.


CS 101 Computer Programming and utilization. Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal Rekhi Building IIT Bombay

What we will learn about this week: Declaring and referencing arrays. arrays as function arguments. Arrays

BITG 1113: Function (Part 2) LECTURE 5

Week 3: Pointers (Part 2)

Lecture 23: Pointer Arithmetic

Note 12/1/ Review of Inheritance Practice: Please write down 10 most important facts you know about inheritance...

CS24 Week 3 Lecture 1

C++ basics Getting started with, and Data Types.

UEE1302(1066) F12: Introduction to Computers and Programming Function (II) - Parameter

CSCI 123 Introduction to Programming Concepts in C++

Programming in C++: Assignment Week 8

Function Call Example

Week 4 EECS 183 MAXIM ALEKSA. maximal.io

Review. Modules. CS 151 Review #6. Sample Program 6.1a:

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

For loops, nested loops and scopes. Jordi Cortadella Department of Computer Science

More Functions. Pass by Value. Example: Exchange two numbers. Storage Classes. Passing Parameters by Reference. Pass by value and by reference

Chapter Five: Functions. by Cay Horstmann Copyright 2018 by John Wiley & Sons. All rights reserved

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

CSE 333. Lecture 10 - references, const, classes. Hal Perkins Paul G. Allen School of Computer Science & Engineering University of Washington

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011

Pointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book.

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

Outline. For loops, nested loops and scopes. Calculate x y. For loops. Scopes. Nested loops. Algorithm: repeated multiplication x x x x

CSCI 2132 Software Development. Lecture 17: Functions and Recursion

EECS402 Lecture 02. Functions. Function Prototype

CS240: Programming in C

EECS 183, Week 5. General. Variables I/O. 0. At which location do you have to take the exam? 1. Source code vs. object code? 2. What s a library?

Lecture 3: C Programm

Topics. Functions. Functions

Void Functions Call-By-Reference Parameters Using Procedural Abstraction Testing and Debugging General Debugging Techniques

Programming Studio #9 ECE 190

Agenda / Learning Objectives: 1. Map out a plan to study for mid-term Review the C++ operators up to logical operators. 3. Read about the tips

Linked Lists CS 16: Solving Problems with Computers I Lecture #16

Reference Parameters A reference parameter is an alias for its corresponding argument in the function call. Use the ampersand (&) to indicate that

Lab 8 The Stack (LIFO) Structure

Computer Science II Lecture 1 Introduction and Background

Study Guide for Test 2

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Weeks 6&7: Procedures and Parameter Passing

Welcome to MCS 360. content expectations. using g++ input and output streams the namespace std. Euclid s algorithm the while and do-while statements

CS-201 Introduction to Programming with Java

CSE 303: Concepts and Tools for Software Development

Implementing an ADT with a Class

C++ For Science and Engineering Lecture 12

Name MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Computer Programming. Dr. Fahad Computer Science Deptt.

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

Intermediate Programming, Spring 2017*

[CSE10200] Programming Basis ( 프로그래밍기초 ) Chapter 9. Seungkyu Lee. Assistant Professor, Dept. of Computer Engineering Kyung Hee University

More Examples Using Functions and Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture #6

CS 101 Computer Programming and utilization. Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal Rekhi Building IIT Bombay

Module 27 Switch-case statements and Run-time storage management

The University Of Michigan. EECS402 Lecture 02. Andrew M. Morgan. Savitch Ch. 3-4 Functions Value and Reference Parameters.

Programmer-Defined Functions

Function Calls and Stack Allocation

Exceptions, Case Study-Exception handling in C++.

CS 314 Principles of Programming Languages. Lecture 13

Function Calls and Stack Allocation

Stack. Stack. Function Calls and Stack Allocation. Stack Popping Popping. Stack Pushing Pushing. Page 1

1) What of the following sets of values for A, B, C, and D would cause the string "one" to be printed?

Local and Global Variables

Transcription:

Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering Session: Parameter Passing in Function Calls Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 1

Quick Recap of Relevant Topics Use of simple functions in programs Contract-centric view of programming with functions Flow of control in function call and return Activation records and call stack 2 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

Overview of This Lecture Paradigms of parameter passing in function calls Call by value Call by reference Functions without return values 3 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

Recall: Encoding Example We want to store quiz 1 and quiz 2 marks of CS101 students in an encoded form Encoding strategy: The ordered pair of marks (m, n) is encoded as 2 m x 3 n Assume all marks are integers in {1, 2, 10 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 4

Recall: C++ Program Structure #include <iostream> using namespace std; int myencode(int q1marks,int q2marks); int power(int base, int exponent); int main() { for ( ) { cipher = myencode(q1marks, q2marks); // PRECONDITION: int myencode(int q1marks, int q2marks) { tworaisedq1 = power(2, q1marks); threeraisedq2 = power(3, q2marks); // POSTCONDITION: // PRECONDITION: int power(int base, int exponent) { // POSTCONDITION: Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 5

CALL STACK Recall: Activation Records in Call Stack When a function (caller) calls a function (callee) a fresh activation record for callee created Values of function parameters from caller copied to space allocated for formal parameters of callee PC of caller saved Other book-keeping information updated Activation record for callee pushed on call stack int myencode(int q1marks, int q2marks) {. tworaisedq1 = power(2, q1marks); Activation record: power Activation record: myencode Activation record: main Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 6

Call-by-Value Paradigm Values of function parameters copied from activation record of caller to activation record of callee Recall: Formal parameters of callee (power) are its local variables Not confused with parameters used in caller (myencode) when invoking callee (power) Only way in which callee (power) can let caller (myencode) see effects of its computation is through return value of callee Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 7

Caveat When Using Call-by-Value Any changes done on local variables of callee completely lost when callee returns to caller Recall: Space for local variables of callee allocated in activation record of callee Local variables of a function also called its stack variables When callee returns to caller, activation record of callee freed up (lost forever!) Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 8

Program For Swapping Numbers #include <iostream> using namespace std; Values swap(int of m m, and int n n); as local variables of swap { int(in main() swap s { activation record) are swapped int status; cout << main Give doesn t two integers: get to see ; cin this >> swap a >> b; status = swap(a, b); cout << a: << a << b: << b << endl; int swap(int m, int n) int temp; temp = m; m = n; n = temp; Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 9

How Could We Fix This? #include <iostream> using namespace std; int swap(int Can we &m, let int the int n); formal &n); parameters int main() refer to { the variables used in main int status; when calling swap? cout << Give two integers: ; cin >> a >> b; status Can caller = swap(a, and callee b); refer to the same Note change in declaration too! cout << a: << a variable? << b: << b << endl; int swap(int &mm, int &n n ) { int temp; temp = m; m = n; n = temp; Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 10

Program For Swapping Numbers #include <iostream> int swap(int &m, int &n) using namespace std; { int swap(int &m, int &n); int main() { int temp; m intand status; n are NOT local variables of swap, temp = m; cout but << references Give two integers: (or aliases) ; cin to >> caller a >> b; m = n; variables status = swap(a, and b) b); used to pass parameters n = temp; cout << a: << a << b: << b << endl; Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 11

Call-by-Reference Paradigm #include <iostream> using namespace std; int swap(int &m, int &n); int main() { int status; cout << Give two integers: ; cin >> a >> b; status = swap(a, b); cout << a: << a << b: << b << endl; call stack int swap(int &m, int &n) Since m and n are not local variables int of temp; swap, no space allocated for m and n in activation record of swap { temp = m; m = n; Can lead to significant savings in memory required for n = temp; Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 12

Caveat When Using Call-by-Reference Cannot pass a constant as parameter of a function called by reference (otherwise constant could be changed by callee) #include <iostream> using namespace std; int swap(int &m, int &n); int main() { int status; cout << Give an integer: ; cin >> a; status = swap(2, a); cout << a: << a << endl; int swap(int &m, int &n) { int temp; temp = m; m = n; n = temp; Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 13

Call-by-Value vs Call-by-Reference Call-by-reference allows functions to share variables Need to be careful Inadvertent updates: Formal parameters are not local variables, but shared with caller Variables declared in body of function are local variables Can save significant memory for activation records on call stack for deeply nested function calls Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 14

Call-by-Value vs Call-by-Reference Call-by-value by far the safest No change to caller except through returned value Clean separation of variables of caller and callee Can lead to significant memory usage in activation records for deeply nested function calls Specific choice is context-dependent Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 15

Functions Not Returning Values #include <iostream> using namespace std; int swap(int &m, int int n); &n); No scope for errors here! int main() { swap also not used to compute an int status; cout << Give two int integers: result. ; cin >> a >> b; status = swap(a, b); cout Can << a: we let << swap a << return b: << nothing b << endl; (no value)? int swap(int &m, int &n) { int temp; temp = m; m = n; n = temp; Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 16

Functions Not Returning Values #include <iostream> using namespace std; int void swap(int m, &m, int n); int &n); int main() { int status; cout << Give Return two integers: type void ; cin >> a >> b; status = swap(a, b); cout << a: << a << b: << b << endl; void swap(int &m, int &n) { int temp; temp = m; m = n; n = temp; return; Simply return without argument Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 17

Summary Parameter passing in function calls Call-by-value Call-by-reference Caveats and benefits of each paradigm Functions without return values Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 18