The Power Platform is an amazing application platform and is aimed at low-code and no-code individuals, while also providing the opportunity to provide additional support for those with a pro-code background. However, if you’re starting out in the industry with no-code and even a low-code background, being exposed to variables, collections, parameters, parallelism etc can be a little daunting. Have no fear, I’m here to help with this! #PowerWhat #LessCodeMorePower
Let’s look at Functions & Parameters! So, we want to understand what a function is and how to use them while also wondering what a parameter is. Across the Power Platform there are many different types of functions available to its users, so what are they? why do they exist and how do they benefit me?
If you’re writing code as a developer and you want to, for example, check if a variable (if you do not know what a variable is check out the Variables blog post) contains no value, then rather than having to write the code yourself, you can use a function to check it for you. The code is pre-written by someone else, there’s no need to rewrite logic that has a solution already! We can just call that function, pass the variable in as a parameter and continue with what you’re doing.
So, we have decided to use a function and we then use the name of the function with the parenthesis () to call the function. An example of this which we will use in the Power Automate and Power Apps examples is utcNow(), this will be represented by the yellow section in the image above. This function would return the current date and time in UTC without me having to ‘code’ anything. The code has been written, but what is important to remember is that functions in different programming languages or platforms (Power Automate/Power Apps) can be named differently. Let’s have a look at a diagram showing the utcNow() process.
The logical process to what you want to do has probably been thought of before, especially if it’s something simple like checking the status of a variable or getting the current date and time. As you can see from above, when you use utcNow() it simply calls prewritten code by a Power Platform developer and returns the date to you. You do not need to know how it’s doing it, you just care about the end returned result.
So that’s how functions work, what about parameters?
Parameters (also known as arguments) are a value passed to a function. You can pass values such as strings, ints, floats and so on into a function so that it can be used within the pre-written code. Imagine that we had a function that checks that multiplies two numbers and provides a result, the function could be called mathMultiply(). Well we may have something that needs multiplying, such as a 2 x 4 but how do we pass both 2 and 4 into the mathMultiply() function? We would simply place them within the parenthesis and separate each parameter by a comma e.g. mathMultiply(2, 4).
This would then run the code within the function and set the first number as the first parameter and then set the second number as the second parameter. Finally, once the calculation is complete we’re returned the result.
So we now have an understanding of what functions and parameters are used for, now let’s take a look at this visually within the Power Platform.
The Power Automate example above shows a variable being created (if you do not know what a variable is check out the Variables blog post) and set to a String type. We’re then setting its value using a function called utcNow(). Within Power Automate, you can see an expression column popup when you select an input field. Within this column there’s a huge list of functions you can use or you can simply start typing the function you want.
So what does utcNow() do? How does this function actually function?! What happens is when you call utcNow() and the Flow gets run, this kicks off a process where code held somewhere (we don’t know or need to worry about that!) under the name of utcNow() is run and it returns the UTC date and time to us. We then can set the returned data to a variable (as shown above) and use that data. We do not need to create a way of obtaining the date and the time ourselves as Microsoft has already done this for us!
For the PowerApps section, we will look at the Time() function so that we can use parameters and see them in action too.
Firstly, the Time() function in Power Apps converts individual Hour, Minute, and Second values to a Date/Time value. So if we pass in a parameter of 1 for the hour it will signify 1 am. We can see below an example of this:
The example above uses the Time() function and passes 1 for the hour 3 for the minutes and 0 for the seconds; used as Time(1, 3, 0). The label selected shows the returned time format of 01:03.