Functions and Subroutines - Modularizing code in QTP


When you see job requirements, one of the most common thing that I see as a condition nowadays when recruiters look for QTP Automation Engineers is that the candidate must not be doing just playback and record in QTP. Well, the fact is that people who have just started working on QTP would most likely just have used the playback and record feature but once you get experienced, they would have started writing more code and when you start writing more code, you would definitely have started to write functions and subroutines as well. This technique of using functions and subroutines is actually what we call code modularization. This is not specific to QTP, its more of a general programming concept where we divide our program into repeatable and logical units so that our test becomes more organizedunderstandable, readable and maintanable (is there such a word...h-m-m...you got what I meant anyway !!).
Too much of theory, right ? So, as usual, lets get into an example. But before that lets talk about the one difference between functions and subroutines. Functions can return a value but a subroutine cannot. I am sure that this is what you have read everywhere but I will show you what this really means. Lets first write a simple function to add two numbers..

















Line 1 : This makes sure that we dont mistype any variable names
Lines 4 - 8 : Function Definition
Line 12 : Function is called and its return value is stored

Couple of things to note over here is that

  • A function returns a value by assigning the value to the function name. So Line 6 is where the function is storing the return value.


  • Another thing is that this assignment of (function name)=(value) need not be the last line in the function. Aha.....never thought of that , eh ? Yes, its possible. Let me show you another piece of code.





























OK... what did we just do above ? Lets say that I never expect the function AddNumbers to return -1 and I have so many lines of code before the final addition. For some reason, for any reason, for whatever reason, lets say the function just terminated in between, the Line 23 would actually get the value -1 if at least the first line of the function had been executed. So all that I wanted to show was that (function name)=(value) need not be the last line in the function. The code I showed above is just a technique I use, not that its the best technique but it definitely is a good one !!! But ideally we would not have any statements after this because we would generally assume that the function did its job once the value is returned.

  • Third thing to note is that we need not declare the variables or the function arguments intA and intB in the function. So our Option Explicit will not complain.


  • Another point to note is that it is not necessary that a function has to return a value. Which means even the below is possible





















But then if you dont want your function to return anything, then why even use a function !!!! Thats where we can make use of subroutines. However, in the beginning, I mentioned that functions can return a value whereas a subroutine cannot. What this means and what this only means is that if AddNumbers was a subroutine, you could never write Line 14 as above. However, you can write a subroutine also to obtain the sum, and in this case, the only thing is that you would have to pass an additional argument.


















OK, so we did the trick with a subroutine as well. I intentionally passed intSum to the subroutine and had the function argument as intFnSum just to show that we do not need to pass the same argument name. It could be just anything.
Notice the call to the subroutine here. We did not use any parantheses here as this is the VB script syntax to call a subroutine. However if you want to use it , there is still a way. Replace Line 12 above with

Call AddNumbers (208, 392, intSum)

Beginners to VB script would be thinking that a function can only return one value. By definition, even though this is true, dont restrict your mind to think that its possible only to return one value. You can make use of multiple arguments as shown above to get multiple values. Yes, just a question of logical programming. I guess I can keep on writing about functions and their interesting facts but I guess this should be sufficient as far as this topic is concerned. You know what... when you play with it more, you learn more and more interesting facts !!!!
As you would have noticed, I wrote the function definition in my action itself. This makes the function available only within the action. What if I want to make this function available publicly i.e. to anyone !! Thats where we would make use of Function Libraries which I will cover in another topic.

I dont want to have any sort of copyright or copyleft in this site but if you are reusing this code in any other site, I would appreciate you to provide this page as a link so that others get a more descriptive explanation of this concept. Your suggestion and comments are always welcome. Cheers !!!
As always,
Your friend in need,

George, Reju

Read Users' Comments (0)

0 Response to "Functions and Subroutines - Modularizing code in QTP"

Post a Comment

Visitors

Website Counter