1 of 11

Program your own Functions

Thunkable

DragAndDropCode.com, David Wolber

2 of 11

Introducing Programmer-Defined Functions

Your apps have consisted of:

​

  • a set of event handlers

​

  • The responses to events consist of a sequence of possibly conditional, possibly repeated operations. �
  • An operation is either a “set” or a call to a built-in Thunkable function.�
  • Now we introduce programmer-defined functions-- you can create your own blocks and call them.

DragAndDropCode.com, David Wolber

3 of 11

Refactoring Code

  • Refactoring is the process of restructuring computer code without changing or adding to its external behavior and functionality. �
  • From the user’s perspective, the app works exactly the same.�
  • The goal is better readability and maintainability-- so you can extend, modify, and remix the code more easily.

DragAndDropCode.com, David Wolber

4 of 11

DEMO: Refactoring “Meet My..”

To create a function:

  • From functions folder, drag out a “to” block
  • change the name from “do something” to a name for your action.
  • Place some blocks within the “to”
  • Check the functions folder-- you have a new call block!

​

​

​

DragAndDropCode.com, David Wolber

5 of 11

Refactoring to eliminate redundancy

when E1

op1

op2

opx

opy

when E2

op1

op2

opa

opb

when E1

when E2

func

call func

call func

opx

opy

op1

op2

opa

opb

DragAndDropCode.com, David Wolber

6 of 11

An Updated Grammar:

  • Your app’s behavior consists of a set of event handlers and a set of functions
  • EHs can call functions. Functions can also call other functions (e.g., funcA is calling funcB)�
  • In what order are the operations e1,e2,a1,a2,b1,b2, and b3 performed? How is an event handler similar to and different than a function (do worksheet problems 2,3)

DragAndDropCode.com, David Wolber

7 of 11

How do Functions Help?

  1. Eliminate/Reduce redundancy,
    • makes code maintenance easier
    • If you need to fix a bug or extend code, you make the change in one place, instead of multiple places
      • example: if op1 needs be changed:

​

when E1

op1

op2

opx

opy

when E2

op1

op2

opa

opb

when E1

when E2

func

call func

call func

opx

opy

op1

op2

opa

opb

DragAndDropCode.com, David Wolber

8 of 11

How do Functions Help?

​

2. Break down the app into smaller, more manageable chunks

    • Divide and Conquer--our brains are better with <7 items
    • Better for teams: can parcel out functions and event handlers to team members�

​

DragAndDropCode.com, David Wolber

9 of 11

Abstraction

Abstraction (short definition):

Give a name to some detailed information.

​

“Brush your teeth” analogy

​

Facilitates communication: at times you and your team can ignore details, at other times dig into them

​

DragAndDropCode.com, David Wolber

10 of 11

Functional Abstraction

The ability to name and later call a set of instructions like [GenerateNumbers] is one of the key tools in computer science and is referred to as procedural abstraction. It is called abstraction because the caller of the procedure (who, in real-world projects, is likely to be different from the author of the procedure) only needs to know what the procedure does(generate a list), not how it does it (by making calls to the random-number generator, and insert calls). Without procedural abstraction, big computer programs would not be possible, because they contain too much code for one person to hold in his head at a time. This is analogous to the division of labor in the real world, where, for example, different engineers design different parts of a car, none of them understanding all of the details, and the driver only has to understand the interface (e.g., pressing the brake pedal to stop the car), not the implementation.

​

modified from Ellen Spertus, App Inventor

​

note: “Procedure” aka “function”

DragAndDropCode.com, David Wolber

11 of 11

Functional Abstraction

Some advantages of procedural abstraction over copying and pasting code are:

  • It is easier to test code if it is neatly segregated from the rest of the program.
  • If there's a mistake in the code, it only needs to be fixed in one place.
  • To change the implementation,such as modifying how many items to generate for the list, you only have to modify the code in one place.
  • Procedures can be collected into a library and used in different programs. (Unfortunately, this functionality is not currently supported in App Inventor/Thunkable)
  • Breaking code into pieces helps you think about and implement the application (divide and conquer)
  • Choosing good names for procedures helps document the code, making it easier for someone else (or you, a month later) to read.

​

From Ellen Spertus, App Inventor

​

DragAndDropCode.com, David Wolber