Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter Nine: Subprograms Lesson 09. What are they  Modularized code  Might return a value  Functions  Or not  Procedures  Subroutines  In object.

Similar presentations


Presentation on theme: "Chapter Nine: Subprograms Lesson 09. What are they  Modularized code  Might return a value  Functions  Or not  Procedures  Subroutines  In object."— Presentation transcript:

1 Chapter Nine: Subprograms Lesson 09

2 What are they  Modularized code  Might return a value  Functions  Or not  Procedures  Subroutines  In object orientation tend to be known as  Methods 9/19/2015Subprograms2 Subprograms are the fundamental building blocks of programs and are, therefore, among the most important concepts in programming language design

3 Anatomy of a subprogram  Whole thing is the subprogram definition  When use or invoke it is known as a subprogram “call”  A subprogram header is the first part  Includes name, the kind of subprogram, and the formal parameters  The parameter profile (aka signature)  The number, order, and types of its parameters  If include return type then known as the protocol 9/19/2015Subprograms3 int sum(int a, int b){ return a + b; } int sum(int a, int b){ return a + b; } Definition HeaderParameter profile Protocol

4 Prototypes  Function declarations in C and C++ are often called prototypes  Useful to the compiler—allows visibility of subprogram form without “seeing” body  Allows independent compilation subprograms  Object files that become linked 9/19/2015Subprograms4

5 Actual/Formal Parameter Correspondence  Positional: Java, C/C++  Keyword  Advantage: Parameters can appear in any order, thereby avoiding parameter correspondence errors  Disadvantage: User must know the formal parameter’s names  Python, R, Matlab  Can mix and match  If know order, can use order, if don’t can use names in any order 9/19/20155Subprograms sumer(length = myLength, list = myArray, sum = mySum)

6 Formal Parameter Default Values  In certain languages (e.g., C++, Python, Ruby, Ada, PHP), formal parameters can have default values (if no actual parameter is passed)  Variable numbers of parameters  C# (must be same type)  Ruby (employs a hash)  Python (passes a list)  Lua (syntax is …, and loop through them with a for loop) 9/19/20156Subprograms void calc_y(double m = 1, double x = 0, double b = 0);

7 Ruby Blocks def fibonacci(last) first, second = 1, 1 while first <= last yield first first, second = second, first + second end puts "Fibonacci numbers less than 100 are:" fibonacci(100) {|num| print num, " "} puts 9/19/20157Subprograms Block

8 Models of Parameter Passing 9/19/20158Subprograms  In, out, inout

9 Pass Modes 9/19/2015Subprograms9  Pass-by-Value (In Mode)  Usually pass by copy  Pass-by-Reference (Inout Mode)  Pass an access path

10 Modes continued 9/19/2015Subprograms10  Pass-by-Result (Out Mode)  Empty variable location placed on stack at call (because OUT)  Compiler must assign value on stack back into the original variable upon return  Pass-by-Value-Result (inout Mode)  No empty start enforcement  Pass by copy, then compiler assigns back into original variable upon return Default method: pass-by-value Pass-by-reference is specified by preceding both a formal parameter and its actual parameter with ref Default method: pass-by-value Pass-by-reference is specified by preceding both a formal parameter and its actual parameter with ref void doIT(out int x, int index){ x = 17; index = 14; } void doIT(out int x, int index){ x = 17; index = 14; }

11 Multidimensional Arrays as Parameters  No way to know the size, should pass 9/19/201511Subprograms

12 Multidimensional Arrays as Parameters  Ada – not a problem  Size is part of the array’s type 9/19/201512Subprograms

13 Multidimensional Arrays as Parameters  Similar to Ada  Arrays are objects; they are all single-dimensioned, but the elements can be arrays  Each array inherits a named constant ( length in Java, Length in C#) that is set to the length of the array when the array object is created 9/19/201513Subprograms

14 Subprograms as parameters  When might one wish to do this?  Action listener callback functions  Could pass a sort function along with some data to be sorted  Sort it differently depending upon the situation  Example: the LISP map function  takes as its arguments a function and a list, and returns the list formed by applying the function to each member of the list. 9/19/2015Subprograms14

15 Passing functions in C/C++  How do you suppose its done in C and C++?  Why, pointers of course 9/19/2015Subprograms15 int func(int a, int b){ return a + b; } int (*f)(int a, int b); f = func; Pointers

16 Example: function pointers  Passing a function as an argument 9/19/2015Subprograms16 #include int adder(int a, int b){ return a+b; } int multiplier(int a, int b){ return a*b; } int binary(int (*passedFunc)(int a, int b), int c, int d){ return (*passedFunc)(c,d); } int main(int argc,char *argv[]){ printf("adder result for 3 and 4: %d\n",binary(adder,3,4)); printf("multiplier result for 3 and 4: %d\n",binary(multiplier,3,4)); return(0); } #include int adder(int a, int b){ return a+b; } int multiplier(int a, int b){ return a*b; } int binary(int (*passedFunc)(int a, int b), int c, int d){ return (*passedFunc)(c,d); } int main(int argc,char *argv[]){ printf("adder result for 3 and 4: %d\n",binary(adder,3,4)); printf("multiplier result for 3 and 4: %d\n",binary(multiplier,3,4)); return(0); } Results adder result for 3 and 4: 7 multiplier result for 3 and 4: 12 Results adder result for 3 and 4: 7 multiplier result for 3 and 4: 12

17 Overloaded Subprograms  Same name  Every version of an overloaded subprogram has a unique protocol (signature)  Ada, Java, C++, and C# allow 9/19/2015Subprograms17

18 Generic Subprograms  A generic or polymorphic subprogram takes parameters of different types on different activations  Common in object oriented programming languages  Subprograms are expected to take any child objects  Project 6: will convert your stack class into a template: can declare a stack of any type  Overloaded subprograms: a form of polymorphism  In Java and C#, called a generic method 9/19/2015Subprograms18

19 User-Defined Overloaded Operators  Operators can be overloaded in Ada, C++, Python, and Ruby  An Ada example: turn the * operator into a dot product operator for two vectors function "*" (A,B: in Vec_Type): return Integer is Sum: Integer := 0; begin for Index in A'range loop Sum := Sum + A(Index) * B(Index) end loop return sum; end "*"; … c = a * b; -- a, b, and c are of type Vec_Type 9/19/201519Subprograms

20 The end 9/19/201520Subprograms


Download ppt "Chapter Nine: Subprograms Lesson 09. What are they  Modularized code  Might return a value  Functions  Or not  Procedures  Subroutines  In object."

Similar presentations


Ads by Google