




















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 28
This page cannot be seen from the preview
Don't miss anything!





















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.
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
Write a function called swap() that interchanges two int values passed to it by the calling program.
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.
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.
Two kinds of strings are commonly used in C++ C-strings, strings that are objects of the string class.
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.
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.
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.
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.
#include
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).
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