DOC PREVIEW
Yale CPSC 427 - Functions and Parameter Passing
School name Yale University
Pages 8

This preview shows page 1-2-3 out of 8 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

5 Functions and Parameter Passing5.1 Function Calls5.2 Parameter Passing5.2.1 Three Odd Functions5.2.2 Calling The Odd Functions5.2.3 Parameter Passing Mechanisms5.3 Function Returns5.3.1 L-values and r-values.5.3.2 Using L-values.5.3.3 Using Return Values in Assignment StatementsChapter 5: Functions and Parameter PassingIn this chapter, we examine the difference between function calls in C and C++ and the resulting difference inthe way functions are defined in the two languages. Finally, we illustrate the semantics of the three methods ofpassing a parameter in C++ and the three kinds of function return values. You should use the demo programand its output as reference material when you are uncertain about how to use the various parameter and returntypes.Knowledge is of two kinds. We know a subject ourselves, or we know where we canfind information on it.— Samuel Johnson, English, author of the Dictionary of the English Language.5.1 Function CallsThe implied argument. In C++, every call to a class function has an “implied argument”, which is theobject whose name is written before the function name. The rest of the arguments are written inside theparentheses that follow the function name. Additional explicit arguments are written inside parentheses, as inC. Compare this C++ code fragment to the C code fragment below:if ( in.eof() )...stk->push(curtok);The implied argument is called this. In contrast, in C, all arguments are written inside the parentheses. In thestack.c example, the end-of-file test and the call on push() are written like this:if (feof( in ))...push(&stk, curtok);Number of parameters. Function calls in C++ tend to be shorter and easier to write than in C. In C,functions often have several parameters, and functions with no parameters are rare. In the stack.c example,note that the call on push() has two arguments in C and one in C++. The difference is the stack object itself,which must be passed by address in C and is the implied argument in C++.Functions with empty or very short parameter lists are common in C++. First, the implied argument itselfis not writtten as part of the argument list. Also, classes are used to group together related data, and thatdata is available for use by class functions. We often eliminate parameters by using class data members to storeinformation that is created by one class function and used by others.Code organization. When writing in a procedural language, many programmers think sequentially: do thisfirst, this second, and so on. Function definitions often reflect this pattern of thought Thus, in C, functions tendto be organized by the sequence in which things should be done.In an object-oriented language, the code is not arranged sequentially. Thus, in C++, functions are part ofthe class of the objects they work on. Control passes constantly into and out of classes; consecutive actionsare likely to be in separate functions, in separate classes with a part-whole relationship. One object passescontrol to the class of one of its components, which does part of the required action and passes on the rest ofthe responsibillity to one of its own components.The number and length of functions. There are many more functions in a C++ program than in aC program and C++ functions tend to be very short. Each C++ function operates on a class object. Theactions it performs directly should all be related to that object and to the purpose of that class within theapplication. Actions related to class components are delegated (passed on) to the components to execute.Thus, long functions with nested control structures are unusual, while one-line functions and one-line loops arecommon. Inline expansion is used to improve the efficiency of this scheme.5354 CHAPTER 5. FUNCTIONS AND PARAMETER PASSINGNon-class functions. Functions can be defined inside or outside classes in C++ . For those defined outside aclass, the syntax for definitions and calls is the same as in C. Non-class functions in C++ are used primarily tomodularize the main program, to extend the input and output operators, and to permit use of certain C libraryfunctions.5.2 Parameter PassingC supports two ways to pass a parameter to a function: call-by-value and call-by-pointer. C++ supports a thirdparameter-passing mechanism: call-by-reference. The purpose of this section is to demonstrate how the threeparameter-passing mechanisms work and to help you understand which to use, when, and why.5.2.1 Three Odd FunctionsThese three functions do not compute anything sensible or useful; their purpose is to illustrate how the threeparameter-passing mechanisms are similar and/or different. Each function computes the average of its twoparameters, then prints the parameter values and the average. Finally, both parameters are incremented. Thecomputation works the same way in all versions. However, the increment operator does not work uniformly:sometimes a pointer is incremented, sometimes an integer; sometimes the change is local, sometimes not.Call by value. The argument values are copied into the parameter storage locations in the function’s stackframe. There is no way for the function to change values in main’s stack frame.1 #include "odds.hpp" // Includes necessary libraries.23 void odd1( int aa, int bb ){ // Make copies of the argument values.4 int ansr = (aa + bb) / 2; // See diagram 1.5 cout <<"\nIn odd1, average of " <<aa <<", " <<bb <<" = " <<ansr <<endl;6 ++aa; ++bb; // Increment the two local integers.7 }Call by pointer. The arguments must be addresses of integer variables in the caller’s stack frame. Theseaddresses are stored in the parameter locations in the function’s stack frame. They permit the function tomodify its caller’s memory locations.8 #include "odds.hpp" // Includes necessary libraries.910 void odd2( int* aa, int* bb ){ // Call by address or pointer.11 int ansr = (*aa + *bb) / 2; // See diagram 2.12 cout << "\nIn odd2, average of " <<*aa <<", " <<*bb <<" = " <<ansr <<endl;13 ++aa; // increment the local pointer14 ++*bb; // increment main’s integer indirectly15 }Call by reference. The arguments must be integer variables in the caller’s stack frame. The addresses ofthese variables are stored in the parameter locations in the function’s stack frame, permitting the function tomodify its caller’s memory locations.16 #include "odds.hpp" // Includes necessary libraries.1718 void odd4( int& aa, int& bb ){ // Call by reference19 int ansr = (aa + bb) / 2; // See


View Full Document

Yale CPSC 427 - Functions and Parameter Passing

Download Functions and Parameter Passing
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Functions and Parameter Passing and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Functions and Parameter Passing 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?