Chapter 13. Functions and Parameter Passing (Part 2)

Similar documents
Chapter 11. Using Functions for Procedural Abstraction (Part 1)

Chapter 10. Arrays and Strings

To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows

Chapter-11 POINTERS. Important 3 Marks. Introduction: Memory Utilization of Pointer: Pointer:

Pointers. Pointer References

Pointers. Pointers. Pointers (cont) CS 217

Structured Programming. Functions and Structured Programming. Functions. Variables

Dynamic arrays / C Strings

CS 0449 Sample Midterm

Chapter 11: Pointers

EL2310 Scientific Programming

Lab Instructor : Jean Lai

#include <iostream> #include <algorithm> #include <cmath> using namespace std; int f1(int x, int y) { return (double)(x/y); }

Memory Allocation in C

Chapter 2. Procedural Programming

Chapter 14. Structures

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

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

ECE 2035 Programming HW/SW Systems Spring problems, 7 pages Exam One Solutions 4 February 2013

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

CS 220: Introduction to Parallel Computing. Input/Output. Lecture 7

High-performance computing and programming Intro to C on Unix/Linux. Uppsala universitet

Lecture 16. Daily Puzzle. Functions II they re back and they re not happy. If it is raining at midnight - will we have sunny weather in 72 hours?

EL2310 Scientific Programming LAB2: C lab session. Patric Jensfelt, Andrzej Pronobis

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Chapter 3 Function Overloading

High Performance Programming Programming in C part 1

EL2310 Scientific Programming

Procedures, Parameters, Values and Variables. Steven R. Bagley

STRUCTURED DATA TYPE ARRAYS IN C++ ONE-DIMENSIONAL ARRAY TWO-DIMENSIONAL ARRAY

Object oriented programming C++

C-String Library Functions

More on Func*ons Command Line Arguments CS 16: Solving Problems with Computers I Lecture #8

Programming in C. Pointers and Arrays

CS240: Programming in C

FORM 1 (Please put your name and form # on the scantron!!!!) CS 161 Exam I: True (A)/False(B) (2 pts each):

5. Assuming gooddata is a Boolean variable, the following two tests are logically equivalent. if (gooddata == false) if (!

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

UNIT V Sub u P b ro r g o r g a r m a s

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

C++ Lab 02 - Command Line Arguments and Strings

ARRAYS(II Unit Part II)

Parameter passing. Programming in C. Important. Parameter passing... C implements call-by-value parameter passing. UVic SEng 265

Outline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C

CHAPTER 4 FUNCTIONS. 4.1 Introduction

Bitwise Operator and Typecasting

Announcements. Strings and Pointers. Strings. Initializing Strings. Character I/O. Lab 4. Quiz. July 18, Special character arrays

Overloading Functions & Command Line Use in C++ CS 16: Solving Problems with Computers I Lecture #6

Array Initialization

Functions. Using Bloodshed Dev-C++ Heejin Park. Hanyang University

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

THE NAME OF THE CONSTRUCTOR AND DESTRUCTOR(HAVING (~) BEFORE ITS NAME) FUNCTION MUST BE SAME AS THE NAME OF THE CLASS IN WHICH THEY ARE DECLARED.

COP4020 Programming Languages. Subroutines and Parameter Passing Prof. Robert van Engelen

Homework #3 CS2255 Fall 2012

From Java to C. Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 8: Dynamic Memory Allocation

CS103 Unit 6 - Pointers. Mark Redekopp

Exercise: Inventing Language

UNIT III (PART-II) & UNIT IV(PART-I)

ENERGY 211 / CME 211. Functions

Communication With the Outside World

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

Final CSE 131B Winter 2003

ADVENTURE_IO Input/Output format and libraries for ADVENTURE modules List of Input/Output Functions February 17, 2006

Pointers, Dynamic Data, and Reference Types

CSCI 104 Memory Allocation. Mark Redekopp David Kempe

CSI33 Data Structures

Unit 7. Functions. Need of User Defined Functions

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017

CS2141 Software Development using C/C++ C++ Basics

Software Development With Emacs: The Edit-Compile-Debug Cycle

AMCAT Automata Coding Sample Questions And Answers

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

Solution: A pointer is a variable that holds the address of another object (data item) rather than a value.

Unit 14. Passing Arrays & C++ Strings

C Language: Review. INFO High Performance Scientific Computing. 26 septembre 2017

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

Arguments and Return Values. EE 109 Unit 16 Stack Frames. Assembly & HLL s. Arguments and Return Values

LSN 3 C Concepts for OS Programming

Output of sample program: Size of a short is 2 Size of a int is 4 Size of a double is 8

CSE 374 Programming Concepts & Tools

Outline. Computer Memory Structure Addressing Concept Introduction to Pointer Pointer Manipulation Summary

C:\Temp\Templates. Download This PDF From The Web Site

Part II Answer to all the questions (2 Marks):

CS103 Unit 6 - Pointers. Mark Redekopp

UNIT- 3 Introduction to C++

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Linked List Practice Questions

Exercise 1.1 Hello world

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

Pointers. Part VI. 1) Introduction. 2) Declaring Pointer Variables. 3) Using Pointers. 4) Pointer Arithmetic. 5) Pointers and Arrays

CS 345. Functions. Vitaly Shmatikov. slide 1

The University of Nottingham

Chapter 7 C Pointers

Fundamental of Programming (C)

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

Direct Memory Access. Lecture 2 Pointer Revision Command Line Arguments. What happens when we use pointers. Same again with pictures

TEMPLATE IN C++ Function Templates

11. Arrays. For example, an array containing 5 integer values of type int called foo could be represented as:

Strings and Stream I/O

Transcription:

Christian Jacob Chapter 13 Functions and Parameter Passing (Part 2) 13.1 Passing Arguments to Functions 13.1.1 Passing Pointers 13.1.2 Passing Arrays 13.1.3 Passing Strings 13.2 Parameter Passing Mechanisms 13.2.1 General Parameter Passing Mechanisms 13.2.2 C++ Approaches to Parameter Passing 13.2.3 Call-by-Reference Using Pointers 13.2.4 Reference Parameters Chapter Overview

Christian Jacob 13.2.5 Returning References 13.2.6 Restrictions When Using References 13.3 Command Line Arguments for main() 13.3.1 argc and argv 13.3.2 Passing Numeric Command Line Arguments 13.3.3 Converting Numeric Strings to Numbers 13.4 References Chapter Overview

Page 3 Chapter 13: Functions (Part 2) Christian Jacob 13.1 Passing Arguments to Functions 13.1.1 Passing Pointers C++ allows to pass a pointer to a function. Then the parameter has to be declared as a pointer type. void f(int *j) *j = 100; int main() int i; int *p; p = &i; // p now points to i f(p); cout << i; // i is now 100 return 0; First Back TOC Prev Next Last

Page 4 Chapter 13: Functions (Part 2) Christian Jacob The pointer variable p is actually not necessary: void f(int *j); int main() int i; f(&i); cout << i; return 0; void f(int *j) *j = 100; First Back TOC Prev Next Last

Page 5 Chapter 13: Functions (Part 2) Christian Jacob 13.1.2 Passing Arrays When an array is an argument to a function, only the address of the first element of the array is passed, not a copy of the entire array. Note: In C++, an array name without any index is a pointer to the first array element. This means that the formal parameter declaration has to be of a compatible type. There are three ways to declare a parameter that is to receive an array pointer: as an array of the same type and size as that used to call the function as an unsized array as a pointer The following three example programs illustrate these three possibilities. First Back TOC Prev Next Last

Page 6 Chapter 13: Functions (Part 2) Christian Jacob Passing Arrays (1): with size specification void display(int n[10]); int main() int t[10], i; for(i=0; i<10; ++i) t[i] = i; display(t); // pass array t to function return 0; void display(int n[10]) int i; for(i=0; i<10; i++) cout << n[i] << ; First Back TOC Prev Next Last

Page 7 Chapter 13: Functions (Part 2) Christian Jacob Passing Arrays (2): as an unsized array void display(int n[]); int main() int t[10], i; for(i=0; i<10; ++i) t[i] = i; display(t); // pass array t to function return 0; void display(int n[]) int i; for(i=0; i<10; i++) cout << n[i] << ; First Back TOC Prev Next Last

Page 8 Chapter 13: Functions (Part 2) Christian Jacob Passing Arrays (3): using a pointer void display(int *n); int main() int t[10], i; for(i=0; i<10; ++i) t[i] = i; display(t); // pass array t to function return 0; void display(int *n) int i; for(i=0; i<10; i++) cout << n[i] << ; First Back TOC Prev Next Last

Page 9 Chapter 13: Functions (Part 2) Christian Jacob Passing Arrays (4): using a pointer and array size information void display(int *n, int s); int main() int t[10], i; for(i=0; i<10; ++i) t[i] = i; display(t, 10); return 0; void display(int *n, int s) int i; for(i=0; i<s; i++) cout << n[i] << ; First Back TOC Prev Next Last

Page 10 Chapter 13: Functions (Part 2) Christian Jacob Important to remember: As for arrays, only addresses are passed to a function, the function will potentially alter the contents of the array. void cube(int *n, int num) while(num) *n = *n * *n * *n; num--; n++; void main() int i, nbs[10]; for(i=0;i<10;i++) nbs[i] = i+1; cube(nbs,10); for(i=0;i<10;i++) cout << nbs[i] << ; First Back TOC Prev Next Last

Page 11 Chapter 13: Functions (Part 2) Christian Jacob 13.1.3 Passing Strings Strings are simply character arrays that are null-terminated. When a string is passed to a function, only a pointer to the beginning of the string (of type char *) is actually passed. First Back TOC Prev Next Last

Page 12 Chapter 13: Functions (Part 2) Christian Jacob The following code fragment defines a function that converts strings to uppercase: void stringtoupper(char *str); int main() char str[80]; strcpy(str, this is a test ); stringtoupper(str); cout << str; return 0; void stringtoupper(char *str) while(*str) *str = toupper(*str); // one character str++; /* move on to next char */ First Back TOC Prev Next Last

Page 13 Chapter 13: Functions (Part 2) Christian Jacob 13.2 Parameter Passing Mechanisms 13.2.1 General Parameter Passing Mechanisms Call by Value The value of an argument is copied into the formal parameter of the subroutine. --> Changes made to the parameters of the subroutine will not affect the arguments used to call it. Call by Name The formal parameter is textually replaced by the calling argument. Call by Reference The address of an argument is copied into the parameter. --> Changes made to the parameter will affect the argument used to call the subroutine. First Back TOC Parameter Passing Mechanisms Prev Next Last

Page 14 Chapter 13: Functions (Part 2) Christian Jacob Example: We use the following pseudo-code for defining a function, two array elements, and a variable: Now consider a call to f( a[i] ): f( p ) = i = 2; a[1] = 12; x = p a[1] = 10; a[2] = 11; i = 1; Call by Value: f( a[1] ) = f( 10 ) = i = 2; a[1] = 12; x = 10 x = 10 Call by Reference: f( &a[1] ) = i = 2; a[1] = 12; x = a[1] x = 12 First Back TOC Parameter Passing Mechanisms Prev Next Last

Page 15 Chapter 13: Functions (Part 2) Christian Jacob Example (con t): We use the following pseudo-code for defining a function, two array elements, and a variable: f( p ) = i = 2; a[1] = 12; x = p Now consider a call to f( a[i] ): a[1] = 10; a[2] = 11; i = 1; Call by Name: f( a[i] ) = i = 2; a[1] = 12; x = a[i] x = a[2] x = 11 The call-by-name parameter passing is not provided in C++! First Back TOC Parameter Passing Mechanisms Prev Next Last

Page 16 Chapter 13: Functions (Part 2) Christian Jacob 13.2.2 C++ Approaches to Parameter Passing In general, there are several ways that a computer language can pass an argument to a subroutine. In C++, there are two methods of parameter passing: Call-by-Value The value of an argument is copied into the formal parameter of the subroutine. Changes made to the parameters of the subroutine will not affect the arguments used to call it. Call-by-Reference The address of an argument is copied into the parameter. Changes made to the parameter will affect the argument used to call the subroutine. First Back TOC Parameter Passing Mechanisms Prev Next Last

Page 17 Chapter 13: Functions (Part 2) Christian Jacob Call-by-Value: int sqr_it(int x); int main() int t = 10; cout << sqr_it(t) << << t; return 0; int sqr_it(int x) x = x * x; return x; First Back TOC Parameter Passing Mechanisms Prev Next Last

Page 18 Chapter 13: Functions (Part 2) Christian Jacob 13.2.3 Call-by-Reference Using Pointers One can manually create a call-by-reference by passing the address of an argument, i.e., a pointer, to a function. The following function, which exchanges the values of two variables, uses explicit pointers in its formal parameters. void swap(int *x, int *y) int temp; temp = *x; // save the value at address x *x = *y; // put y into x *y = temp; // put temp, i.e., x, into y First Back TOC Parameter Passing Mechanisms Prev Next Last

Page 19 Chapter 13: Functions (Part 2) Christian Jacob Illustration of the swap( ) function Step 0 temp: x: 12 y: 7 First Back TOC Parameter Passing Mechanisms Prev Next Last

Page 20 Chapter 13: Functions (Part 2) Christian Jacob Illustration of the swap( ) function Step 1 temp: 12 x: 12 y: 7 temp = *x; // save the value at address x *x = *y; // put y into x *y = temp; // put temp into y First Back TOC Parameter Passing Mechanisms Prev Next Last

Page 21 Chapter 13: Functions (Part 2) Christian Jacob Illustration of the swap( ) function Step 2 temp: 12 x: 7 y: 7 temp = *x; // save the value at address x *x = *y; // put y into x *y = temp; // put temp into y First Back TOC Parameter Passing Mechanisms Prev Next Last

Page 22 Chapter 13: Functions (Part 2) Christian Jacob Illustration of the swap( ) function Step 3 temp: 12 x: 7 y: 12 temp = *x; // save the value at address x *x = *y; // put y into x *y = temp; // put x into y First Back TOC Parameter Passing Mechanisms Prev Next Last

Page 23 Chapter 13: Functions (Part 2) Christian Jacob Here is an example code fragment how to use the swap function: int main() int i, j; i = 10; j = 20; cout << initial values of i and j: ; cout << i << << j << \n ; swap(&i, &j); cout << swapped values of i and j: ; cout << i << << j << \n ; return 0; First Back TOC Parameter Passing Mechanisms Prev Next Last

Page 24 Chapter 13: Functions (Part 2) Christian Jacob Note: A less desireable solution is using swap( ) without any references to variables, because it will work only on global variables, and only on these particular variables: a = 12; b = 7; void swap() int temp; temp = a; b = a; a = temp; First Back TOC Parameter Passing Mechanisms Prev Next Last

Page 25 Chapter 13: Functions (Part 2) Christian Jacob 13.2.4Reference Parameters Instead of using pointers to manually achieve a call-by-reference, it is possible to tell the C++ compiler to automatically use call-byreference for the formal parameters of a particular function. A reference parameter is declared by preceding the parameter name in the function s declaration with an &. When a reference parameter is created, that parameter automatically refers to the argument used to call the function. The reference parameter implicitly points to the argument. Operations performed on a reference parameter affect the argument used to call the function, not the reference parameter itself. There is no need to apply the & operator to an argument. Inside the function, the reference parameter is used directly; i.e., the * operator is not necessary. In fact, it is not correct to use the * operator for reference parameters. First Back TOC Parameter Passing Mechanisms Prev Next Last

Page 26 Chapter 13: Functions (Part 2) Christian Jacob Here is a simple example to understand the use of reference parameters: void f(int &i) i = 10; // modifies the calling argument int main() int val = 1; cout << Old val: << val << \n ; f(val); // pass address of val to f() cout << New val: << val << \n ; return 0; First Back TOC Parameter Passing Mechanisms Prev Next Last

Page 27 Chapter 13: Functions (Part 2) Christian Jacob Here is the version of the swap example, now using reference parameters: void swap (int &x, int &y) int temp; temp = x; // save the value at address x x = y; // put y into x y = temp; // put x into y First Back TOC Parameter Passing Mechanisms Prev Next Last

Page 28 Chapter 13: Functions (Part 2) Christian Jacob int main() int i, j; i = 10; j = 20; cout << initial values of i and j: ; cout << i << << j << \n ; swap(i, j); cout << swapped values of i and j: ; cout << i << << j << \n ; return 0; First Back TOC Parameter Passing Mechanisms Prev Next Last

Page 29 Chapter 13: Functions (Part 2) Christian Jacob 13.2.5 Returning References A function can return a reference, which means, it returns an implicit pointer to its return value. The function can be used on the left side of an assignment statement! double val = 100; double &f() return val; // return reference to val First Back TOC Parameter Passing Mechanisms Prev Next Last

Page 30 Chapter 13: Functions (Part 2) Christian Jacob double val = 100; double &f() return val; // return reference to val int main() double newval; newval = f(); // assign value of val f() = 99.1; // change val s value // reference to val becomes // target of assignment return 0; First Back TOC Parameter Passing Mechanisms Prev Next Last

Page 31 Chapter 13: Functions (Part 2) Christian Jacob Here is another example program using a reference return type: double v[] = 1.1, 2.2, 3.3, 4.4, 5.5; double &change_element(int i) return v[i]; // reference to i-th element void main() int i; for(i=0; i<5; i++) cout << v[i] << ; change_element(1) = 523.9; // second element change_element(3) = -98.7; // 4th element for(i=0; i<5; i++) cout v[i] << ; First Back TOC Parameter Passing Mechanisms Prev Next Last

Page 32 Chapter 13: Functions (Part 2) Christian Jacob Mind the Scope of References! When returning a reference, one has to be careful that the object being referred to does not go out of scope: // Error: cannot return reference to local var int &f() int i = 10; return i; First Back TOC Parameter Passing Mechanisms Prev Next Last

Page 33 Chapter 13: Functions (Part 2) Christian Jacob 13.2.6 Restrictions When Using References There are some restrictions that apply to reference variables: You cannot reference a reference variable. You cannot create arrays of references. You cannot create a pointer to a reference. You cannot apply the & operator to a reference. References are not allowed on bit-fields. First Back TOC Parameter Passing Mechanisms Prev Next Last

Page 34 Chapter 13: Functions (Part 2) Christian Jacob 13.3 Command Line Arguments for main() To pass information into a program when you call it, is accomplished by passing command line arguments to the program. A command line argument is the information that follows the program s name on the command line of the operating system. Examples: > ls -a > ls -la > diff file-1 file-2 > CC program-name.cc > CC -out prog-ex-name program-name.cc > add 123 745 First Back TOC Command Line Arguments for main() Prev Next Last

Page 35 Chapter 13: Functions (Part 2) Christian Jacob 13.3.1 argc and argv C++ defines two built-in, but optional, parameters to main( ), which receive the command line arguments: argc: an integer The argc parameter is an integer that holds the number of arguments on the command line. It will always be at least 1, because the name of the program also counts. argv: a pointer The argv parameter is a pointer to an array of character pointers. Each pointer in the argv array points to a string containing a command line argument: argv[0]: argv[1]: argv[2]: the program s name the first argument the second argument First Back TOC Command Line Arguments for main() Prev Next Last

Page 36 Chapter 13: Functions (Part 2) Christian Jacob The following program demonstrates how to access command line arguments. It prints hello, followed by the string entered as the first command line argument. int main(int argc, char *argv[]) if(argc!= 2) cout << You forgot to type your name.\n ; return 1; cout << Hello << argv[1] << \n ; return 0; First Back TOC Command Line Arguments for main() Prev Next Last

Page 37 Chapter 13: Functions (Part 2) Christian Jacob Output of this program: >name Jessica Hello Jessica > First Back TOC Command Line Arguments for main() Prev Next Last

Page 38 Chapter 13: Functions (Part 2) Christian Jacob Command line arguments should be separated by spaces or tabs. The following program prints all command line arguments it is called with, one character at a time. int main(int argc, char *argv[]) int t, i; for(t=0; t < argc; ++t) i = 0; while( argv[t][i] ) cout << argv[t][i]; ++i; cout << ; return 0; First Back TOC Command Line Arguments for main() Prev Next Last

Page 39 Chapter 13: Functions (Part 2) Christian Jacob 13.3.2 Passing Numeric Command Line Arguments All command line arguments are passed to the program as strings. Hence, numeric arguments have to be converted into their proper internal formats. The following program displays the sum of two numeric command line arguments. int main(int argc, char *argv[]) double a, b; if(argc!= 3) cout << Usage: add num num\n ; a = atof(argv[1]); b = atof(argv[2]); cout << a + b; return 0; First Back TOC Command Line Arguments for main() Prev Next Last

Page 40 Chapter 13: Functions (Part 2) Christian Jacob 13.3.3 Converting Numeric Strings to Numbers The C++ standard library, cstdlib, includes several functions that allow conversions from the string representation of a number into its internal numerical format: atoi( ): atol( ): atof( ): numeric string --> integer numeric string --> long integer numeric string --> double floating-point First Back TOC Command Line Arguments for main() Prev Next Last

Page 41 Chapter 13: Functions (Part 2) Christian Jacob int main() int i; long j; double k; i = atoi( 100 ); j = atol( 100000 ); k = atof( -0.123 ); cout << i << << j << << k << \n ; return 0; First Back TOC Command Line Arguments for main() Prev Next Last

Page 42 Chapter 13: Functions (Part 2) Christian Jacob 13.4References G. Blank and R. Barnes, The Universal Machine, Boston, MA: WCB/ McGraw-Hill, 1998. Chapter 9. H. Schildt, C++ from the Ground Up, McGraw-Hill, Berkeley, CA, 1998. Chapter 7. First Back TOC References Prev Next Last