Created
February 8, 2013 19:42
-
-
Save romyilano/4741429 to your computer and use it in GitHub Desktop.
A beautiful example of blocks in Big Nerd Ranch.
I like how they describe blocks as a hybridization of method handling in procedural and object-oriented programming languages.
Methods in C and procedural langauges can be accessed anywhere but methods in object-oriented progrmaming langauges can only be accessed by classes and instances of classe…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // block variables - this one isn't defined | |
| // block variable named adder that takes in an int parameter and an int b parameter | |
| int (^adder)(int a, int b); | |
| // defining block literals - here's the syntax | |
| ^int (int x, int y) { | |
| return x + y; | |
| }; | |
| int (^adder) (int, int); | |
| // block variable named foo | |
| // it can point to a block that takes no arguments and returns no values | |
| void (^foo)(void); | |
| // block variable named jump. it can point to a block | |
| // that takes one Hurlde * and returns no value | |
| void (^jump)(Hurdle *); | |
| // a block variable named doubler; it can point to a block that takes one int | |
| // and returns an int | |
| int (^doubler)(int); | |
| // defining block literals - the definition of a block is called a block literal | |
| // caret, return type and then parenthesis containing arguments (like C) | |
| ^int (int x, int y) | |
| { | |
| return x + y; | |
| }; | |
| int (^adder)(int, int) = ^int(int x, int y) { | |
| return x + y; | |
| }; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment