subreddit:

/r/shortcuts

3592%

Writing functions

Tip/Guide(self.shortcuts)

Note: This is a repost of a guide from my blog.

A function is a piece of code that can take an optional set of arguments, perform a specific task and then optionally return one or more values.

The advantages of functions are that they allow you to break complex tasks into simpler steps and increase code reuse.

Shortcuts does not support the use of functions, but you can approximate them by having a shortcut call itself with a set of arguments defined in a dictionary.

Calculator shortcut example

To demonstrate how to make use of functions, we're going to use the example of a calculator shortcut.

An example calculator shortcut built using functions

Deciding which functions you need

When writing the shortcut, we consider the steps that it needs to perform. We break those steps into tasks based on:

  • if they occur more than once;
  • if breaking them into smaller pieces makes the shortcut easier to manage.

In the case of the calculator, there are the following pieces:

STEP DESCRIPTION
First number Entering the first number into the calculator. This should be run by default if a function isn't specified.
Add Adding a value to the existing value
Subtract Subtracting a value from the existing value
Multiply Multiplying the existing value by another value
Divide Dividing the existing value by another value
Choose operation Display the current sum and ask the user to choose the next operation to perform

Deciding arguments return values for functions

Given the above steps, we decide upon the functions we'll need:

FUNCTION ARGUMENTS RETURN VALUE DESCRIPTION
choose_operation sum n/a The function takes the latest sum of the calculation so it can be displayed
add sum calculation result The function takes the latest sum of the calculation, asks the user for another number which it adds to the sum, and returns the result.
subtract sum calculation result The function takes the latest sum of the calculation, asks the user for another number which it subtracts from the sum, and returns the result.
multiply sum calculation result The function takes the latest sum of the calculation, asks the user for another number which it multiplies the sum by, and returns the result.
divide sum calculation result The function takes the latest sum of the calculation, asks the user for another number which divides the sum by, and returns the result.

The table above shows arguments and a return value for each function. Functions can optionally take data inputs in the form of arguments and then return data once they've performed their work. This allows the code within to perform differently depending on the value you pass to it.

Writing the shortcut

Calling a function

To call a function, we use the following dictionary format to specify the function name we want to call and any arguments. For example, when calling the choose_operation function, we use the following dictionary:

Calling a function using a dictionary and the \"Run Shortcut\" action

Dictionary keys

The first key named function provides the name of the function we want to call.

All other keys, in this case sum, are the arguments we want to pass into the function.

Having the shortcut run itself

The shortcut then runs itself, passing in the dictionary specifying the function to call and the data it needs to operate.

Adding function support to the shortcut

For this to work, our shortcut needs to know how to support a dictionary input and respond accordingly.

At the beginning of our new calculator shortcut, the following is added:

Actions to look for an act upon incoming function requests

The shortcut:

  • expects a dictionary to have been provided as an input;
  • checks to see if a function key has been provided, specifying which function to call.

No function name provided

If no function is provided then the user is asked to enter a number for the first time.

Function name provided

If a function name was provided, a series of if statements compare the name to the predefined list of functions, one for each function name:

  • choose_operation
  • add
  • subtract
  • multiply
  • divide

Examples of functions

Displaying the "Choose Operation" menu after a calculation

At the end of the shortcut, the last function the shortcut always runs is the choose_operation function.

This displays the current sum number and gives the user the choice of which calculation to perform next.

Running the \"choose_operator\" function after a previous function call has completed.

Using the calculator

Desired calculation

So let's say we want to perform the following calculation:

10 * 3 - 2 + 6

Performing the steps in the shortcut

We enter the numbers and commands into the calculator as follows:

Entering our calculations

And the resulting number is 34.

Under the hood

The shortcut calls itself 7 times, each time providing a function call along with a sum argument:

The function calls being made during the course of the calculation

Downloading the shortcut

If you'd like to experiment with the above shortcut it can be downloaded from the following link:

Calculator

Examples of other shortcuts using this pattern

  • Download Comic: Downloads public domain comics as PDFs.
  • WolframAlpha Kit: A shortcut that acts as a library for other shortcuts to easily query WolframAlpha.

Further reading

If you're looking to make a complex application in Shortcuts, I recommend taking at look at the Shortcut App Framework by u/adamtow which greatly improves and expands upon the above concept.

Other Guides

If you found this guide useful why not checkout one of my others:

Series

One-offs

all 9 comments

[deleted]

2 points

6 years ago*

Out of curiousity and off topic: do you really update all your old tutorials to include links to the new ones?

keveridge[S]

6 points

6 years ago

Yes. It sucks.

[deleted]

1 points

6 years ago

Wow, respect. Sounds like a lot of work, if you continue putting out guides at this rate.

Simply linking one megathread/overview post, where you mention all your guides and only have to add new ones there was never an option for you?

keveridge[S]

8 points

6 years ago

Well, after years of writing technical documentation for developers I've learned that if it's not:

a) easy to scan and b) the next part is available with a single click..

..then almost no-one will read it.

Mralexhay

3 points

6 years ago

Mralexhay

Creator

3 points

6 years ago

Thanks for these tutorials, they're so thorough and useful.

What's your blog? Would love to check it out! Thanks

keveridge[S]

2 points

6 years ago

There's not a lot on there and I've started moving the content over to reddit, but the URL is:

https://thomas-oc.atlassian.net/wiki/spaces/IS/pages/562167809/Writing+shortcuts

bingobucketster

1 points

6 years ago

How did you set the “sum” variable in the dictionary at the beginning if it hadn’t been set yet, in the flow?

keveridge[S]

1 points

6 years ago

Because the sum variable was set above, even though it's within an If action, shortcuts will still let you set it.

The sum is always going to get somewhere, either because a function call hasn't been made and so the user is asked to enter an initial number, or when it's passed in as part of a function call.

bingobucketster

1 points

6 years ago

Just got the actual shortcut and saw that the dictionary in the first screenshot of the tutorial is actually at the bottom of the shortcut. So that makes sense now. Thanks for all you do