COMPUTER SCIENCE 2P05
Programming Languages Subprograms
Brock University
Brock University (Subprograms) Programming Languages 1 / 38
COMPUTER SCIENCE 2P05 Programming Languages Subprograms Brock - - PowerPoint PPT Presentation
COMPUTER SCIENCE 2P05 Programming Languages Subprograms Brock University Brock University (Subprograms) Programming Languages 1 / 38 Subprograms (And control structures) Weve had plenty of experience with procedural abstraction by now,
Brock University (Subprograms) Programming Languages 1 / 38
Brock University (Subprograms) Programming Languages 2 / 38
◮ (We aren’t going to make a distinction between branching and jumping
Brock University (Subprograms) Programming Languages 3 / 38
◮ i.e. you might effectively ‘flip the question’ to see if you should skip
Brock University (Subprograms) Programming Languages 4 / 38
◮ Have a branch to decide whether or not to skip the block ◮ At the end of the block, have an unconditional branch to return back
◮ Outside of possible scoping rules, there might not even be a notion of
Brock University (Subprograms) Programming Languages 5 / 38
◮ Readability can be seriously impaired, especially when it comes to
◮ Consider try/catch/finally blocks. Think of the havoc you could
Brock University (Subprograms) Programming Languages 6 / 38
Brock University (Subprograms) Programming Languages 7 / 38
Brock University (Subprograms) Programming Languages 8 / 38
◮ Working on any discrete type is common ◮ Sometimes enumerated types might be an option as well ◮ Java eventually added String support
Brock University (Subprograms) Programming Languages 9 / 38
Brock University (Subprograms) Programming Languages 10 / 38
Brock University (Subprograms) Programming Languages 11 / 38
◮ Could you (easily) resolve a range to a single entry in the table? Brock University (Subprograms) Programming Languages 12 / 38
Brock University (Subprograms) Programming Languages 13 / 38
Brock University (Subprograms) Programming Languages 14 / 38
Brock University (Subprograms) Programming Languages 15 / 38
Brock University (Subprograms) Programming Languages 16 / 38
◮ If the right-hand size isn’t a single statement, we need braces
https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html)
Brock University (Subprograms) Programming Languages 17 / 38
Brock University (Subprograms) Programming Languages 18 / 38
◮ Like anonymous classes, this can also be useful for event processing ◮ Also possibly good for arbitrary queries, comparisons, etc.
◮ This is a theme you might run into for other languages, not to mention
Brock University (Subprograms) Programming Languages 19 / 38
Brock University (Subprograms) Programming Languages 20 / 38
◮ We’ll be revisiting the concept in a fancy way in a week-ish
Brock University (Subprograms) Programming Languages 21 / 38
Brock University (Subprograms) Programming Languages 22 / 38
◮ e.g. #define PI 3 ◮ What it’s really doing is, prior to compilation, finding every instance of
Brock University (Subprograms) Programming Languages 23 / 38
Brock University (Subprograms) Programming Languages 24 / 38
Brock University (Subprograms) Programming Languages 25 / 38
◮ e.g. e instanceof Employee ◮ But that doesn’t help us here
◮ Which gives us such delightful terms as parametrized type, type
◮ All of which we remember from first year
Brock University (Subprograms) Programming Languages 26 / 38
◮ That’s complicated ◮ Ostensibly, the type variables get replaced by Object (or a bounding
◮ This is known as type erasure
◮ Compile-time is when we need it the most! ⋆ Compare against what came before Java 5: just use Object for
⋆ That brief, compile-time carefulness is what affords us type safety Brock University (Subprograms) Programming Languages 27 / 38
Brock University (Subprograms) Programming Languages 28 / 38
◮ That reference has a fixed size (e.g. 64bits), so we know how much
Brock University (Subprograms) Programming Languages 29 / 38
◮ That parameter receives data (it acts as an input for the subprogram)
◮ For a language like C++, giving a struct (or union, class, etc.) as
◮ It’s easy to allocate suitably sized memory for a well-defined structure ◮ However, this introduces many problems ⋆ Can we think of a few? Brock University (Subprograms) Programming Languages 30 / 38
◮ We’ve discussed references before: two labels can share the same
◮ If you want the function to receive data, and return the result, just let
Brock University (Subprograms) Programming Languages 31 / 38
Brock University (Subprograms) Programming Languages 32 / 38
Brock University (Subprograms) Programming Languages 33 / 38
Brock University (Subprograms) Programming Languages 34 / 38
◮ Definitely, this is an option
◮ You’re still stuck with a specific upper-bound, plus it’d be a nightmare
Brock University (Subprograms) Programming Languages 35 / 38
Brock University (Subprograms) Programming Languages 36 / 38
◮ e.g. maybe we need to just check on something now and then ◮ The important point is we aren’t talking about function A that keeps
◮ It’s exactly as easy as it sounds
Brock University (Subprograms) Programming Languages 37 / 38
Brock University (Subprograms) Programming Languages 38 / 38