Unformatted Document Excerpt
Coursehero >>
New Jersey >>
NJIT >>
CS, ECE 332
Course Hero has millions of student submitted documents similar to the one
below including study guides, practice problems, reference materials, practice exams, textbook help and tutor support.
Course Hero has millions of student submitted documents similar to the one
below including study guides, practice problems, reference materials, practice exams, textbook help and tutor support.
9
Functions
Itisbettertohave100functionsoperateononedatastructure
than10functionson10datastructures.
A.Perlis
Copyright ProgrammingLanguages
2ndedition
TuckerandNoonan
Chapter 2006 The McGraw-Hill Companies, Inc.
Contents
9.1BasicTerminology
9.2FunctionCallandReturn
9.3Parameters
9.4ParameterPassingMechanisms
9.5ActivationRecords
9.6RecursiveFunctions
9.7RunTimeStack
Copyright 2006 The McGraw-Hill Companies, Inc.
9.1BasicTerminology
Value-returning functions:
known as non-void functions/methods in C/C++/Java
called from within an expression.
e.g., x=(b*bsqrt(4*a*c))/2*a
Non-value-returning functions:
known as procedures in Ada,
subroutines in Fortran,
void functions/methods in C/C++/Java
called from a separate statement.
Copyright 2006 The McGraw-Hill Companies, Inc.
9.2FunctionCallandReturn
Example C/C++
Program
Fig 9.1
inth,i;
voidB(intw){
intj,k;
i=2*w;
w=w+1;
}
voidA(intx,inty){
booli,j;
B(h);
}
intmain(){
inta,b;
h=5;a=3;b=2;
A(a,b);
Copyright 2006 The McGraw-Hill Companies, Inc.
9.3Parameters
Definitions
An argument is an expression that appears in a function
call.
A parameter is an identifier that appears in a function
declaration.
E.g., in Figure 9.1
The call A(a,b) has arguments a and b.
The function declaration A has parameters x and y.
Copyright 2006 The McGraw-Hill Companies, Inc.
ParameterArgumentMatching
Usually by number and by position.
I.e., any call to A must have two arguments, and they must
match the corresponding parameters types.
Exceptions:
Perl - parameters arent declared in a function header.
Instead, parameters are available in an array @_, and are
accessed using a subscript on this array.
Ada - arguments and parameters can be linked by name. E.g.,
the call A(y=>b,x=>a) is the same as A(a,b)
Copyright 2006 The McGraw-Hill Companies, Inc.
9.4ParameterPassingMechanisms
By value
By reference
By value-result
By result
By name
Copyright 2006 The McGraw-Hill Companies, Inc.
PassbyValue
Compute the value of the argument at the time of the call
and assign that value to the parameter.
E.g., in the call A(a,b) in Fig. 9.1, a and b are passed by
value. So the values of parameters x and y become 3 and 2,
respectively when the call begins.
So passing by value doesnt normally allow the called
function to modify an arguments value.
All arguments in C and Java are passed by value.
But references can be passed to allow argument values to be
modified. E.g., voidswap(int*a,int*b){}
Copyright 2006 The McGraw-Hill Companies, Inc.
PassbyReference
inth,i;
Compute the address of the
argument at the time of the
call and assign it to the
parameter.
Example
voidB(int*w){
Fig 9.3
booli,j;
Since h is passed by
reference, its value changes
during the call to B.
intj,k;
i=2*(*w);
*w=*w+1;
}
voidA(int*x,int*y){
B(&h);
}
intmain(){
inta,b;
h=5;a=3;b=2;
Copyright The 2006 McGraw-Hill Companies, Inc.
PassbyValueResultandResult
Pass by value at the time of the call and/or copy the
result back to the argument at the end of the call.
E.g., Adas inout parameter can be implemented as valueresult.
Value-result is often called copy-in-copy-out.
Reference and value-result are the same, except when
aliasing occurs. That is, when:
the same variable is both passed and globally referenced
from the called function, or
the same variable is passed for two different parameters.
Copyright 2006 The McGraw-Hill Companies, Inc.
PassbyName
Textually substitute the argument for every instance of
its corresponding parameter in the function body.
Originated with Algol 60 (Jensens device), but was
dropped by Algols successors -- Pascal, Ada, Modula.
Exemplifies late binding, since evaluation of the argument
is delayed until its occurrence in the function body is
actually executed.
Associated with lazy evaluation in functional languages
(see, e.g., Haskell discussion in Chapter 14).
Copyright 2006 The McGraw-Hill Companies, Inc.
9.5ActivationRecords
A block of information associated with each function
call, which includes:
parameters and local variables
Return address
Saved registers
Temporary variables
Return value
Static link - to the functions static parent
Dynamic link - to the activation record of the caller
Copyright 2006 The McGraw-Hill Companies, Inc.
9.6RecursiveFunctions
A function that can call itself, either directly or
indirectly, is a recursive function. E.g.,
intfactorial(intn){
if(n<2)
return1;
elsereturnn*factorial(n1);
}
Copyright 2006 The McGraw-Hill Companies, Inc.
self-call
9.7RunTimeStack
A stack of activation records.
Each new call pushes an activation record, and
each completing call pops the topmost one.
So, the topmost record is the most recent call, and
the stack has all active calls at any run-time moment.
For example, consider the call factorial(3).
This
places one activation record onto the stack and generates a
second call factorial(2). This call generates the call
factorial(1), so that the stack gains three activation records.
Copyright 2006 The McGraw-Hill Companies, Inc.
StackActivityfortheCallfactorial(3)
Fig.9.7
n
3
3
n
3
n
3
n
2
n
2
n
2
n
First call
n
1
Second call
Third call
returns 1
n
3
Second call
First call
returns 2*1=2 returns 3*2=6
Copyright 2006 The McGraw-Hill Companies, Inc.
StackActivityforPrograminFig.9.1
Fig.9.8(linksnotshown)
h
i
a
b
undef
undef
3
2
Activation of main
h
i
a
b
x
y
i
j
5
undef
3
2
3
2
undef
undef
main calls A
Copyright 2006 The McGraw-Hill Companies, Inc.
h
i
a
b
x
y
i
j
w
j
k
5
10
3
2
3
2
undef
undef
5
undef
undef
A calls B
Find millions of documents on Course Hero - Study Guides, Lecture Notes, Reference Materials, Practice Exams and more.
Course Hero has millions of course specific materials providing students with the best way to expand
their education.
Below is a small sample set of documents:
NJIT - CS, ECE - 332
ProgrammingLanguages2ndeditionTuckerandNoonanChapter 7SemanticsSurely all this is not without meaning.Ishmael, Moby Dick by Herman MelvilleCopyright 2006 The McGraw-Hill Companies, Inc.Contents7.17.27.37.47.57.67.7MotivationExpression Sem
Abu Dhabi University - ACCOUNTING - 101
CHAPTER 4ACTIVITY-BASED PRODUCT COSTINGQUESTIONS FOR WRITING AND DISCUSSION1. Unit costs provide essential informationneeded for inventory valuation and preparation of income statements. Knowingunit costs is also critical for many decisionssuch as b
Drexel - ACCT - 321
ACCT 331COST ACCOUNTINGWINTER 2012Exam 2150 PointsStudent: Milo RujeviInstructionsThe following questions are based on a set of facts which were developed from actual recentbusiness cases and events. Provide a three to four paragraph response or t
Baylor - REL - 1310
Geometric Series: an = crnA sequence is monotonic if its always increasing or always decreasingEvery increasing sequence that is bounded above and every decreasing sequence that is boundedbelow converges..If and are both convergent, then is converge
Masters CA - CS - 101
Chapter 7ChapterSpace and TimeTradeoffsTradeoffsCopyright 2007 Pearson Addison-Wesley. All rights reserved.Space-for-time tradeoffsTwo varieties of space-for-time algorithms:Twoa input enhancement preprocess the input (or its part) tostore some
Keller Graduate School of Management - STATISTIC - GM533
Saturday, October 16, 2010 10:39 PMSaturday, October 16, 2010 10:41 PMSaturday, October 16, 2010 10:42 PMSaturday, October 16, 2010 10:44 PMSaturday, October 16, 2010 11:41 PMSunday, October 17, 2010 1:40 AM
University of Texas - CHEM 302 - 302
302 Exam 2 Review OutlineDefinitions:oSolutionoSolventoSoluteoMass %oMole fractionoMolalityoMolarityoSolvationoHydrationoPolaroNonpolaroHydrophobicoHydrophilicoHenrys lawoRaoults LawoColligative propertiesoOsmosisoOsmo
University of Texas - CHEM 302 - 302
Important Facts for CH 302 Exam 2A. Only pure water has a density of 1 g/ml (or 1 kg/L)B. Dissolving happens in 3 steps1. Solute breaks/expands into individual components2. Solvent breaks/expands to make room for solute3. Solute and solvent interact
City University of Hong Kong - MATHEMATIC - 4523
Society of Actuaries Course 5Application of Basic Actuarial Principles Morning Session SECTION A - WRITTEN ANSWERIllustrative Solutions from Fall 2002Course 5: November 2002Page 1 of 66Question No. 1(5 points) With respect to retirement income secur
City University of Hong Kong - MATHEMATIC - 4523
Exam MFE Spring 2007 FINAL ANSWER KEYQuestion # Answer1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19B A C E D C E C A B D A E E C D B A D*BEGINNING OF EXAMINATION* ACTUARIAL MODELS FINANCIAL ECONOMICS SEGMENT1.On April 30, 2007, a common stock is p
Dalhousie - ECON - 2280
Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s). Editorial review has deemed that
Berkeley - PHYSICS - 137a
Berkeley - PHYSICS - 137a
Berkeley - PHYSICS - 137a
Berkeley - PHYSICS - 137a
Berkeley - PHYSICS - 137a
George Mason - FINANACE - 301
Chapter 3Organizational CommitmentMcGraw-Hill/Irwin Copyright 2009 by The McGraw-Hill Companies, Inc. All rights reserved.Learning Goals What is organizational commitment? What is withdrawal behavior? How are the two connected? What are the three type
George Mason - FINANACE - 301
Chapter 4Job SatisfactionMcGraw-Hill/Irwin Copyright 2009 by The McGraw-Hill Companies, Inc. All rights reserved.Learning Goals What is job satisfaction? What are values, and how do they affect job satisfaction? People often evaluate their job satisfa
George Mason - FINANACE - 301
Chapter 9Personality and Cultural ValuesMcGraw-Hill/Irwin Copyright 2009 by The McGraw-Hill Companies, Inc. All rights reserved.Learning Goals What is personality, and how can it be distinguished from ability? What are cultural values? Is personality
George Mason - FINANACE - 301
Chapter 10AbilityMcGraw-Hill/Irwin Copyright 2009 by The McGraw-Hill Companies, Inc. All rights reserved.Learning Goals What is ability, and where do individual differences in ability come from? What are the various types of cognitive ability? What ar