Chapter Nine: Subprograms Lesson 09. What are they Modularized code Might return a value ...

20
Chapter Nine: Subprograms Lesson 09

Transcript of Chapter Nine: Subprograms Lesson 09. What are they Modularized code Might return a value ...

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

Chapter Nine: Subprograms

Lesson 09

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

What are they

Modularized code Might return a value

Functions Or not

Procedures Subroutines

In object orientation tend to be known as Methods

04/19/23Subprograms2

Subprograms are the fundamental building blocks of programs and are, therefore, among the most important concepts in programming language design

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

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

04/19/23Subprograms3

int sum(int a, int b){ return a + b;}

int sum(int a, int b){ return a + b;}

Definition

Header Parameter profile

Protocol

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

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

04/19/23Subprograms4

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

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

04/19/235 Subprograms

sumer(length = myLength, list = myArray, sum = mySum)

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

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)

04/19/236 Subprograms

void calc_y(double m = 1, double x = 0, double b = 0);

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

Ruby Blocks

def fibonacci(last) first, second = 1, 1 while first <= last yield first first, second = second, first + second end end

puts "Fibonacci numbers less than 100 are:" fibonacci(100) {|num| print num, " "} puts

04/19/237 Subprograms

Block

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

Models of Parameter Passing

04/19/238 Subprograms

In, out, inout

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

Pass Modes

04/19/23Subprograms9

Pass-by-Value (In Mode) Usually pass by copy

Pass-by-Reference (Inout Mode) Pass an access path

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

Modes continued

04/19/23Subprograms10

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-valuePass-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;}

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

Multidimensional Arrays as Parameters

No way to know the size, should pass

04/19/2311 Subprograms

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

Multidimensional Arrays as Parameters

Ada – not a problem Size is part of the array’s type

04/19/2312 Subprograms

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

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

04/19/2313 Subprograms

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

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.

04/19/23Subprograms14

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

Passing functions in C/C++

How do you suppose its done in C and C++? Why, pointers of course

04/19/23Subprograms15

int func(int a, int b){ return a + b;}

int (*f)(int a, int b);f = func;

Pointers

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

Example: function pointers Passing a function as an argument

04/19/23Subprograms16

#include <stdio.h>

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: 7multiplier result for 3 and 4: 12

Results

adder result for 3 and 4: 7multiplier result for 3 and 4: 12

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

Overloaded Subprograms Same name

Every version of an overloaded subprogram has a unique protocol (signature)

Ada, Java, C++, and C# allow

04/19/23Subprograms17

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

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

04/19/23Subprograms18

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

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 vectorsfunction "*" (A,B: in Vec_Type): return Integer isSum: Integer := 0;beginfor Index in A'range loop

Sum := Sum + A(Index) * B(Index)end loopreturn sum;

end "*";…c = a * b; -- a, b, and c are of type Vec_Type

04/19/2319 Subprograms

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

The end

04/19/2320 Subprograms