Functions are a big part of JavaScript and expressions. You may have even been using them without knowing.
Ever used wiggle() ? wiggle is a function written by people at Adobe.
It's a block of code that WILL do something, but only when you call it.
From time to time you might encounter code where the function declaration exists AFTER the function has been called, like so:
Code like this will still run just fine because function declarations are moved to the top of their scope before the computer runs the code. That process is called hoisting.
So this is the basic syntax for writing a function. You as a motion designer have the power to make the decision to learn more about this topic.
If you are interested in expanding your knowledge about functions check out this tutorial from W3Schools.
For now, let's focus on practical examples so we can get the hang of it quickly.
Create a function that multiplies a number by 2
this function takes a number and multiplies it by 2.
Create a function that gets the comp center
This function gets the comp size and multiplies it by half.
Check if keyframes exist
// this function will check if keyframes exist using the numKeys variable built into After-Effects. If the number of keys is bigger than 0, the answer is true.
Here's a fun idea, let's use this function we have created to fix the built-in loopOut() function.
Using loopOut() will throw an error if keyframes don't exist. But now we can use our new function to check if keyframes exist before we use loopOut() at all.
Or in an even shorter form:
Check if something is in view
if either dimension is smaller than 0 or bigger than its corresponding comp dimensions, it's outside the view.