Passing Arguments by Reference in C++, Exercises of Computer Programming

The concept of passing arguments by reference in c++. It covers the differences between passing arguments by value and by reference, the mechanism of passing arguments by reference, and examples of functions that use this mechanism. It also includes lab tasks for practicing the concept.

Typology: Exercises

2011/2012

Uploaded on 07/31/2012

dhairya
dhairya 🇮🇳

5

(4)

32 documents

1 / 28

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Reference Arguments
A reference provides an alias—a different name
—for a variable
We’ve seen examples of function arguments
passed by value. When arguments are passed
by value, the called function creates a new
variable of the same type as the argument and
copies the argument’s value into it. As we
noted, the function cannot access the original
variable in the calling program, only the copy it
created.
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c

Partial preview of the text

Download Passing Arguments by Reference in C++ and more Exercises Computer Programming in PDF only on Docsity!

Reference Arguments

 A reference provides an alias—a different name —for a variable  We’ve seen examples of function arguments passed by value. When arguments are passed by value, the called function creates a new variable of the same type as the argument and copies the argument’s value into it. As we noted, the function cannot access the original variable in the calling program, only the copy it created.

Reference Arguments

 Passing arguments by value is useful when the function does not need to modify the original variable in the calling program. In fact, it offers insurance that the function cannot harm the original variable.  Passing arguments by reference uses a different mechanism. Instead of a value being passed to the function, a reference to the original variable, in the calling program, is passed.

#include using namespce std; int sq_by_value(int); void sq_by_ref(int&); int main() { int x = 2; int z = 4; cout<<”before sq by value x = “<<x<<endl; cout<<sq_by_value(x)<<endl; cout<<”After sq by value x = “<<x<<endl; cout<<”before sq by ref z = “<<z<<endl; sq_by_ref(z); cout<<”after sq by ref z = “<<z<<endl; } int sq_by_value(int a) { return a = a * a; } void sq_by_ref(int& a) { a = a * a; }

Lab Task

 Write a function called swap() that interchanges two int values passed to it by the calling program.

Passing Arrays to Functions

 Arrays can be used as arguments to functions.  When the function is called, only the name of the array(WITHOUT SIZE) is used as an argument.  my_func(ar);  This name (ar in this case) actually represents the memory address of the array.  The values of the array elements are not duplicated (copied) into the function.

Passing Arrays to Functions

 The function works with the original array, although it refers to it by a different name. This system is used for arrays because they can be very large; duplicating an entire array in every function that called it would be both time- consuming and wasteful of memory.  No ampersand (& ) is used with the array name in the function declaration.

Strings

 Two kinds of strings are commonly used in C++  C-strings,  strings that are objects of the string class.

C - Strings

 C-strings are arrays of type char.  strings created with the string class, which we’ll examine in the next section, have superceded C-strings in many situations, C-strings are still important, for a variety of reasons.  First, they are used in many C library functions.  Second, they will continue to appear in legacy code for years to come.  Third, for students of C++, C-strings are more primitive and therefore easier to understand on a fundamental level.

C Strings

 Each character occupies 1 byte of memory. An important aspect of C-strings is that they must terminate with a byte containing 0.  This is often represented by the character constant ‘\0’, which is a character with an ASCII value of 0. This terminating zero is called the null character.  When the << operator displays the string, it displays characters until it encounters the null character.

Avoiding Buffer Overflow

 What happens if the user enters a string that is longer than the array used to hold it?  As we mentioned earlier, there is no built-in mechanism in C++ to keep a program from inserting array elements outside an array. So an overly enthusiastic typist could end up crashing the system.  However, it is possible to tell the >> operator to limit the number of characters it places in an array.

Avoiding Buffer Overflow

 This program uses the setw manipulator to specify the maximum number of characters the input buffer can accept. The user may type more characters, but the >> operator won’t insert them into the array.  Actually, one character fewer than the number specified is inserted, so there is room in the buffer for the terminating null character.

String Constants

#include using namespace std; int main() { char str[] = “Hello how are you.”; cout << str << endl; return 0; }

 Fortunately, the designers of C++ (and C) took pity on us and provided this shortcut approach.  The effect is the same: The characters are placed one after the other in the array. As with all C-strings, the last character is a null (zero).

Reading Embedded Blanks

 If you tried the above program with strings that contained more than one word, you may have had an unpleasant surprise. Here’s an example: Enter a string: Hello how are you. You entered: Hello