hello hi and welcome to today's video um where we're going to quickly talk about static versus temporary or temp variables um in your program i have prepared a little program not big i'm quickly going to explain it but i've prepared a very very easy simple function block why it's a function block i'll quickly explain later um but yeah this function block is just called in here right so in the main function i put my function block that has one input which is a an input well just a boolean input a button there i've got in
my simulation i can turn it on and off that's what the function call does and inside the function this uh input right this input uh with an edge control so so with a positive edge a detection whenever we activate the input we receive the positive edge and we are adding one on top of the temporary variable that i've created and then we're pushing it to the temporary one so do the same so we're basically increa incrementing this variable adding one on top of itself so if i press this button it's right now zero if i
press the button we will add one on top of zero so we should have one i'm doing this once on top here with a temporary variable that's why i call the temp and we are also doing it once with a static variable that's what i've called the static the rest is exactly the same reacting to the same input they're using different edge memories because they use diff they need different edge values to work but the rest is exactly the same so twice the same functionality once with the temporary ones with the static the temporary and
static variables you define in the um also called actually on top there in english on the schnitzel in the variable interface interface of your function and function block the temporary one you see in temporary i have created count value temp it's an integer it's this one here it's a temporary one and i've got one count value static that i've used for the second network for the static one so in theory without knowing how static and temporary variables work they both should work fine if i press the input if i activate the input they should both
go to a value that is one right let's see i've got my input here and you don't see it but i press it off screen because well this just doesn't fit uh well it could fit if i do this and that now it fits now you can see it if i activate the input and i click the input boop you see it turns green it sent a positive edge so this was activated now what we can see the temporary value here is still zero the static one is one that's strange let's try it again let's
do it again the temporary one is zero the static one is two let's do it again three four five six seven let's do it a couple of times huh very strange even though they are exactly the same function the static one works and the temporary one does not work but why is that why is it because it's the same functionality why is that the case um we cannot see it we cannot see it but for a very very short amount of time for one almost one program cycle the temporary variable would actually be one it
would be one for one cycle in the cycle i add one on top of it it would be one but as soon as the cycle ends it goes back to zero this is the case because we're using temporary variables a temporary variable only has a temporary amount of lifetime as soon as this temporary amount of lifetime is over it's gone we don't have access to the value anymore that's why it's temporary we only have this temporary amount of lifetime that temporary amount of lifetime by the way is as long as we are executing this function
here as soon as this function is over as soon as we are hitting network three in this case which is the last network in this function the value is lost we're going back into our main function we're going back into the next plc cycle we're starting back on top value is lost so we're starting back back at zero in this case it could also be some random value we actually start with but the value we had before is lost different for static ones the static ones they are permanently in the storage as soon as you
download your program as soon as it sits in the plc this static variable sits in always in the same space in your memory so it doesn't get lost it's always in the same space it's reserved right that's the huge difference the static one has a reserved memory space the temporary one does not right that's that's the only difference that that is the difference how does that look like i have prepared a short short very short power point for this wait wait well wrong way around uh like this yes um very very short for the temporary
if we are using temporary variables we are using something called the local data stack every function that we are calling every ob every fc every fb has its own temporary data right like ob1 the first thing that happens in your program is ob1 is called by your operating system that means ob1 the temporary data of ob1 gets pushed into this local data stack and then it sits there as long as ob1 has not completely finished it sits there then inside of ob1 we could be calling a function or a function block right so we are
calling a function in this case we're calling fc1 a function can have temporary data so the function it's for a foreign edition or for whatever um takes its temp data it's temporary data and puts it on top of this local data stack it sees oh here there's some open space i put my data on top of it as soon as fc1 is done completely done right everything in fc1 is done the operating system realizes hey fc1 is done i do not need the temporary data anymore so it releases this the space in your memory meaning
the space that the memory area that fc1 used for its temporary data is free to use by anything else so we're done with fc1 the memory area is free then we're going on with ob1 that hasn't finished yet so the memory is still reserved we're going into fc2 right so in function 2 we also have temporary memory that might be a little bit more so it just fc2 grabs a part of this local data stack and puts a data on top of it and you see it grabs exactly the same part that fc1 used before
so the data that was there is gone it's overwritten by fc2 that is how the local data stack works we we're giving away we're taking storage memory areas for a short amount of time as soon as we don't need it anymore as soon as the function is done memory area is released and the next function can take over right and so on as soon as fc2 is done it releases the memory array again and then we're well ob1 is done then we're restarting the plc cycle from the beginning then fc1 gets called again it grabs
the next free local that local data stack area releases it again and so on and so on this is very efficient if when it comes to [Music] memory usage because everything uses the exact same amount that it needs right and it gives it away it frees the memory as soon as it's done so it's the most efficient data usage memory usage because we're not blocking any memory we just we just need memory as soon as we need it that's it right in contrast to uh static variables right this is temporary when we need memory areas
we take it if we don't need it it's released that's why the counter always goes back to zero because it calculates it is one way and it calculates but then it's done and the storage is released something else overrides it with a zero in this case and then it's gone different with the static one if we go to the static the static one is as soon as you download your program to the plc the plc and your program know how much memory this fc uh this fb actually fcs do not have a static memory that's
that's wrong i didn't i i i did an oopsy fcs do not have a static memory let me just change that in the slide here for a second sorry fcs functions do not have a static memory oh my goodness just give me give me 12 seconds someone counting 12 seconds uh 11 10 i'm done see not even 12 seconds exactly for fbs function blocks fcs they do not have static memory but fcs have temporary so ffb's can use the temporary storage but they can also use the static one for an fb as soon as it's
compiled and downloaded it blocks a memory area in your plc right it blocks this static memory area because it needs it that's the difference between an fb and an fc the fb1 has this fixed amount of memory area that is reserved from beginning on so this is called the data work memory as soon as you download it some memory area is reserved fixed it's never given free again it's always there just for fb1 and fb2 in our case and ob1 as well so the whole time we're executing our program here with the local data stack
it always changes just the memory errors that are needed for the data work memory here's the plc cycle there's the plc segment it's always the same the memory are reserved right uh very inefficient inefficient for some things if you don't need the memory anymore for for in between calculations for example you could uh you could give away that memory area again you could free the memory area again for static variables you're not doing that some of these memory areas might never be used but you reserve them right because you might need them you don't want
to lose the values even if you're not losing them that's the main difference right if we look at both of them um we've got the temporary ones which are only there when you're executing the function um or a function block and we've got the static ones which are always reserved so you see these of course take a little bit more permanent data work memory um but you can save a value over more than one psc cycle basically forever the local data stack the temporary variables they do not um take that much storage like permanent storage
as soon as they're done they're done um but you cannot have permanently stored variables in them that is the difference between static and temporary variables that is also why my temporary value up here the temporary control will never work like this while there's there's some things that could make it run but not in in general not going to work the static one works because it it has this memory error permanently saved permanently reserved this one does not so if i would like to fix this problem here i could just take my static variable here my
temporary variable and just put in a static area i don't know i'm just going to use this and that's going to work now but we are now reserving more permanent storage in the data work memory for this function that's how it is for this function block all right if i click this now you see this counter also works now both work fine yeah this is also the difference between an fc and an fb i've explained that in another video but very quickly nfb a function block you see it here up here if nfb has the
static variables so it can have this reserved memory area and fc a function this one here if we look up here it does not have the static area that's the difference between these two you see this one does have the static area this one does not have the static area it only has the temporary area that's the difference um yeah that's also why the fc does not need an data block and fb needs a data block because well if you want to permanently store something in your storage in your memory you need to define where
and now this one here this fb stores its data in db1 at least this coin so not going into details with fbs nfc calls and stuff like this there's another video on this this was just about static versus temporary variables um yeah let's let's look at this beautiful slideshow again as the uh end of the video perfect look at this perfect it took me five minutes to create it i love it um yeah if this video was any helpful leave a like leave a comment say hi in the comments um don't forget to subscribe if
you want to toss some coin my direction very quick there's a link in the description you can toss some coin my direction using the link you can donate now there we go just five seconds done um yeah thank you for watching um stay safe stay healthy and i will definitely see you in the next video [Music] if you want some content just put them in the comments below um i created this because there was this question came up a lot what is temporary what is static blow there we go just because of you just for
you [Laughter] thanks for watching and bye bye