computer science 2p05
play

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,


  1. COMPUTER SCIENCE 2P05 Programming Languages Subprograms Brock University Brock University (Subprograms) Programming Languages 1 / 38

  2. Subprograms (And control structures) We’ve had plenty of experience with procedural abstraction by now, but it wouldn’t hurt to expand upon that a bit. But before we get to that, we might want to briefly revisit our various control strutures : anything that controls the execution sequence (e.g. branching, conditionals, etc.). Simply subdividing instructions up into blocks isn’t really adequate for most well-defined solutions; we need to be able to selectively choose which blocks to employ for given data Brock University (Subprograms) Programming Languages 2 / 38

  3. Conditionals If we feel like talking about this, then we should Many of you have already taken 2P12, so you might already be familiar with branch statements: The current value of the program counter is changed to a new address of code to execute ◮ (We aren’t going to make a distinction between branching and jumping here) The simplest branch is an unconditional branch , sometimes written as B , and has limited use There are typically other branches like BEQ (or BZ ), BNE (or BNZ ), etc., which will only change to a new location if some test passes. Otherwise, it simply proceeds to the next instruction within the code Brock University (Subprograms) Programming Languages 3 / 38

  4. Conditionals? How is this related? What matters is what it’s performing that test on. e.g. if you’ve just performed a test on a value in a register (or accumulator), then there may be condition codes still set from that (or, of course, you might be directly comparing them) For an if statement, you might (might!) perform an operation on the value in which you’re interested, and then follow that with a branch statement to not enter the condition’s block ◮ i.e. you might effectively ‘flip the question’ to see if you should skip past the block’s contents Brock University (Subprograms) Programming Languages 4 / 38

  5. Does this help with repetition? How do we translate this to loops? Loops end up only minimally different: while loops are only a minor modification of the conditional: ◮ 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 to the top do ... while loops are actually even easier ◮ Outside of possible scoping rules, there might not even be a notion of ‘entering the loop’; just have a conditional branch at the end to return to the top for loops are slightly more complicated, but not much when you consider how they work when you compile them in high-level languages Brock University (Subprograms) Programming Languages 5 / 38

  6. What about GOTO ? No. But seriously, no. From a functional standpoint, they’re simple (create a label, and then later jump to that label), but they can introduce several problems: ◮ Readability can be seriously impaired, especially when it comes to confirming which values have been initialized ◮ Consider try / catch / finally blocks. Think of the havoc you could wreak through arbitrary jumps! Brock University (Subprograms) Programming Languages 6 / 38

  7. Final thought about conditionals... Are we familiar with the dangling else problem? Consider the following Java code: i f (A) i f (B) System . out . p r i n t l n (” neat ! ” ) ; e l s e System . out . p r i n t l n (” nay ! ” ) ; For what values of A and B will we see "neat" ? What about "nay!" ? Brock University (Subprograms) Programming Languages 7 / 38

  8. Let’s switch things up a little, shall we? We’re all familiar with Java’s switch statement. e.g.: java . u t i l . Scanner i n=new java . u t i l . Scanner ( System . i n ) ; i n t value=i n . n e x t I n t ( ) ; switch ( value ) { case 1: System . out . p r i n t l n (”Number 1 ! ” ) ; case 2: System . out . p r i n t l n (” Twice as good ! ” ) ; break ; case 3: System . out . p r i n t l n (”A crowd ? ” ) ; break ; d e f a u l t : System . out . p r i n t l n (” Whatevs ” ) ; } Let’s just briefly make sure we know how to trigger each of these cases. Brock University (Subprograms) Programming Languages 8 / 38

  9. Switchy disclaimer We probably already know this, but while C and C++ follow largely the same rules for switch es, not every language does. (e.g. considering Ada: the break isn’t necessary, but the default case becomes mandatory) There’s also the issue of what types you allow the switch to operate on ◮ Working on any discrete type is common ◮ Sometimes enumerated types might be an option as well ◮ Java eventually added String support Does this give a hint as to how it might be implemented? Brock University (Subprograms) Programming Languages 9 / 38

  10. Implemented? What’s there to implement? Let’s take some guesses. What method for ‘implementing’ a switch first comes to mind? Brock University (Subprograms) Programming Languages 10 / 38

  11. Yes, implemented! And not everyone does it the same! For whatever you guessed, how do you think it’d hold up? How efficient is it? How many conditions would need to be evaluated if there were many options? As a hypothetical, suppose (in C) you had an array of 5 function pointers , and a number with a possible value of 0 .. 4. What could you do with that? Brock University (Subprograms) Programming Languages 11 / 38

  12. Multiple selection (That’s the formal name, by the way) We don’t want to resort to treating it like if/elsif/elsif/.../else if we can avoid it for computational reasons. Instead, we can create a branch table , or branch array An array can be possible when the options are all densely-packed, but something like a hash table is more appropriate for the general case This is why multiple selections normally rely on being well-defined values, rather than e.g. ranges : ◮ Could you (easily) resolve a range to a single entry in the table? Brock University (Subprograms) Programming Languages 12 / 38

  13. What about Python? Do we have multiple selection? We don’t have switch , but we have the tools to simulate something similar. Remember our dictionaries ? Since functions are first-class objects , it’s trivial to stuff several into a dictionary, and then index the dictionary as normal (invoking the result with whatever parameters we want) This does raise the question of how to handle default cases... eh... can we look at a super-quick example? Brock University (Subprograms) Programming Languages 13 / 38

  14. Clarification on functions as value/objects Python (and javascript, etc.) functions are first-class objects, meaning they may be used like any other object or value. As such, they can be initialized, re-defined, assigned to variables, passed as parameters, etc. Even though C/C++ has function pointers , this is not the same thing! The actual code itself will be static in C/C++ We can’t redefine the primary name of the function in C/C++ When assigning to variables or passing as parameters, technically we’re creating a type for that signature , and using the address of the function as the value (We also can’t assign members the same way, etc.) Brock University (Subprograms) Programming Languages 14 / 38

  15. Anonymous functions And lambda functions You’ve probably heard both of these phrases, and may not know the difference between them. Sometimes, a distinction isn’t made. But we will Both of these concern defining a block of code, that ostensibly receives arguments, to return a value, that may be treated as an object or expression. Let’s look at the idea in three languages: javascript, Java, and Python Brock University (Subprograms) Programming Languages 15 / 38

  16. Anonymous functions in JavaScript JavaScript actually has two versions of this! (Though they’re nearly identical) f = f u n c t i o n (a , b) { console . log ( ’ hi ’ ) ; r e t u r n a+b ; } ; We also have arrow functions : f = (a , b)= > { console . log ( ’ hi ’ ) ; r e t u r n a+b ; } ; The biggest difference: these don’t have the this keyword, which is sometimes a good thing. Brock University (Subprograms) Programming Languages 16 / 38

  17. Anonymous/lambda functions in Java Prior to Java 8, the closest you’d get was an anonymous class with a single function. For 8+, we hava lambda expressions. If we have an interface that only requires an implementation for a single function, why couldn’t we just provide that? We make use of the -> arrow ◮ If the right-hand size isn’t a single statement, we need braces This assumes Java can tell what type it’s trying to coerce it into Example time? Example time. (btw: https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html ) ((Also: here)) (((Also, I’m slightly lying. Do we know what syntactic sugar is?))) Brock University (Subprograms) Programming Languages 17 / 38

  18. Lambda functions in Python Python’s lambda functions are simple to write, and limited in functionality. The syntax is simple: lambda v1 [, v2 , v3 ,...] : expression You can assign it to a variable, pass it as an argument, or even invoke it right away (though... why?) This’ll make more sense with examples, and... we probably need a couple. Brock University (Subprograms) Programming Languages 18 / 38

Recommend


More recommend