---
description: This video tutorial will help you to create user defined functions for your code and make it easy to maintain and execute.
title: How to Use Functions in QTP/UFT in 3 Easy Steps
image: https://www.guru99.com/images/how-to-use-functions-in-qtp-uft.png
---

[Skip to content](#main)

**⚡ Smart Summary**

Functions in QTP/UFT let testers package reusable VBScript logic inside library files that any test can call, cutting duplication, easing maintenance and speeding automation development across UFT and QTP projects.

- 📚 **Definition:** A function library is a .qfl or .vbs file of reusable VBScript functions any UFT test can call.
- 🛠️ **Creation:** Build a library from File > New > Function Library and write standard VBScript Function…End Function blocks inside it.
- 🔗 **Association:** Attach the library via File > Settings > Resources > Associate Function Library to expose its functions to the test.
- ▶️ **Calling:** Invoke a library function by typing its name with arguments inside the test script, exactly like a built-in UFT statement.
- 🗂️ **File formats:** Choose .qfl for the native UFT editor or .vbs for a plain-text file readable anywhere, including version control.
- ⚙️ **Run-time loading:** Use LoadFunctionLibrary or ExecuteFile to attach a library programmatically instead of the Resources dialog.
- 🧭 **Naming discipline:** Give every function a unique name across associated libraries, because UFT executes the last-loaded definition when names collide.
- 📊 **Reporting:** Replace MsgBox calls with Reporter.ReportEvent so pass, fail and warning statuses appear directly inside the UFT results tree.

Read More

![](https://www.guru99.com/images/how-to-use-functions-in-qtp-uft.png)

If you have segments of code that you need to use several times in your tests, you may want to create a user-defined function. By using user-defined functions, your tests are shorter, and easier to design, read, and maintain.

Your own function libraries in Micro Focus UFT can contain [VBScript](https://www.guru99.com/vbscript-tutorials-for-beginners.html) functions, subroutines, modules etc.

You need to follow only 3 simple steps to use a function from a library in your test.

A function library differs from an [action](https://www.guru99.com/uft-qtp-actions.html) in UFT: a function has no object repository or datasheet of its own, yet any action or test can call it once the library is associated.

Copy-pasting the same steps into every action multiplies maintenance work, because a single change then has to be repeated across every copy. A function library solves this by centralizing that logic in one place that any action or test in the project can call.

## 3 Simple Steps to Use a Function from a Library in UFT/QTP

**Step 1)** Create a new function library in UFT or QTP by selecting File > New > Function Library. It opens as a new tab in [QTP](https://www.guru99.com/quick-test-professional-qtp-tutorial.html).

Let’s create a very simple function that shows a message box.

```
Function Demo

MsgBox "This is a Test Function"

End Function

```

Whenever this function is called, a message box appears. A single file can hold multiple functions, so save the file once and reuse it across many tests. UFT saves a function library with a **.qfl** extension by default, though a plain **.vbs** file works too, as the next section explains.

**Step 2)** Associate the library with your test. Open File > Settings > Resources > Associate Function Library, click **Add**, select the function library file, and click **OK**.

**Step 3)** The last step is to call the function inside the test script. Once a library is associated, calling a function is as simple as typing its name and passing any required arguments, as shown below.

```
' Call the function from the associated library
Demo()

```

UFT locates **Demo** in the associated library and runs it exactly as though it were written locally inside the test.

**Important Notes**

- Using COM, DCOM objects you can create very advanced Functions
- In fact, many of the features provided by QTP can be coded using VBScript
- We have seen automation engineers who make it more of a VB project rather than automation project
- Our recommendation is to focus on 100% automation rather than flaunting your VB skills

Click [here](https://www.guru99.com/faq#faq1) if the video is not accessible  

## Function Library File Types: .qfl vs .vbs

UFT does not force you into a single file format for a function library. The Function Library editor saves new files with a **.qfl** extension by default, and UFT also accepts a plain-text **.vbs** file created in any code editor, because a QTP/UFT function library is nothing more than ordinary VBScript stored on disk; the two formats behave identically at run time.

| ASPECT | .QFL FILE | .VBS FILE |
| --- | --- | --- |
| Created with | UFT Function Library editor | Any text or code editor |
| Opens outside UFT | No | Yes, as plain text |
| Version control friendliness | Limited | Better, since diffs are readable |
| Association method | Same for both | Same for both |

The choice mostly comes down to team workflow rather than functionality. A one-off library built quickly inside UFT works fine as **.qfl**, while libraries maintained in Git, SVN or another version-control system are usually kept as **.vbs** files, because plain text produces far more readable diffs during code review. Both formats support the full VBScript feature set, including classes, error handling and COM object creation, and both associate with a test in exactly the same way, as the next section explains.

Renaming the file extension does not change what is inside it, so converting an existing **.qfl** file to **.vbs**, or the other way around, is just a file rename followed by re-associating the renamed file with the test.

**Don't Miss:**

- [How to Record and Playback UFT/QTP Script](https://www.guru99.com/uft-qtp-scripts.html)
- [Actions in QTP/UFT with Example](https://www.guru99.com/uft-qtp-actions.html)
- [Test Automation Framework: Architecture and Types](https://www.guru99.com/test-automation-framework.html)
- [Top 100 QTP / UFT Interview Questions & Answers in 2026](https://www.guru99.com/qtp-interview-questions.html)

## Ways to Associate a Function Library with a Test

Step 2 above uses the Resources dialog, but UFT actually offers several ways to associate a function library with a test, and picking the right one depends on whether the association should be permanent or apply only for a single run. A test can have more than one library associated at the same time, and UFT loads them in the order they appear in the Resources list.

1. **Associate Function Library dialog (GUI):** Open File > Settings > Resources > Associate Function Library, click Add, and select the file. UFT stores this association inside the test itself, so the library loads automatically every time the test opens. This is the method shown in Step 2 above and works well as the default choice for any library that every test in a project needs.
2. **LoadFunctionLibrary statement:** Call LoadFunctionLibrary “path” from inside a script to attach a library at run time without touching the Resources dialog. UFT treats the library exactly like a dialog-based association for the remainder of that run, which is convenient when the library path is decided dynamically.
3. **ExecuteFile statement:** ExecuteFile “path” runs a script file and exposes its functions to the current run, but UFT does not register the file as a tracked resource, so it never appears under Associated Function Libraries in the test settings.
4. **Automation Object Model (AOM):** An external VBScript file can launch UFT, open a test, and add a library to Test.Settings.Resources.Libraries programmatically. Build or deployment scripts commonly use this approach to configure the same library across dozens of tests in one pass.

```
' Load a library at run time from inside a script
LoadFunctionLibrary "C:\Automation\CommonFunctions.qfl"

' ExecuteFile works the same way, but UFT does not
' track the file as an associated resource
ExecuteFile "C:\Automation\CommonFunctions.qfl"

```

Whichever method you choose, a library only needs to be associated once per test for the GUI and AOM methods, while LoadFunctionLibrary and ExecuteFile must run again every time the script executes, since they are ordinary VBScript statements rather than saved settings.

When a library is no longer needed, remove it from the Resources list rather than leaving it associated, since an unused library can still shadow a function with the same name that you add to a different library later.

## Best Practices for Writing UFT/QTP Function Libraries

A function library only pays off if the whole team can trust and maintain it. The following habits keep a library reliable as it grows.

- **Use Option Explicit:** Add it as the first line of every library so UFT flags a misspelled variable instead of silently creating a new one.
- **Keep function names unique:** If two associated libraries define a function with the same name, UFT calls whichever one loaded last, so duplicate names cause silent, hard-to-trace bugs.
- **Return values instead of hardcoding them:** Accept parameters and return a result, as in the example below, so the same function serves multiple tests instead of one.
- **Avoid MsgBox in production scripts:** A message box pauses execution until someone clicks OK. Use [Reporter.ReportEvent](https://www.guru99.com/uft-qtp-reporter-reportevent.html) instead so pass, fail and warning statuses are logged straight into the results tree without stopping the run.
- **Group by domain, not by convenience:** Keep login functions, database functions and UI helper functions in separate files so any test can associate only what it actually needs.
- **Keep libraries under version control:** Store function library files in the same repository as the rest of the automation project so a rollback of the project also reverts any shared function.

```
Function AddNumbers(ByVal num1, ByVal num2)
    AddNumbers = num1 + num2
End Function

Dim result
result = AddNumbers(10, 20)
Reporter.ReportEvent micPass, "Sum Calculation", "The result is " & result

```

Applying these habits early costs little, but retrofitting them into a large, unmanaged library later is far more expensive.

## FAQs

🧩 What is the difference between a Function and a Sub in VBScript?

A Function can return a value to the caller and a Sub cannot. Use a Function when a script needs a result back, such as a calculated value or a pass/fail flag, and use a Sub for a self-contained action like clicking a button.

⚠️ What happens if two associated function libraries contain a function with the same name?

UFT does not raise an error. It silently calls whichever definition loaded last, based on the order libraries appear in Resources settings, so identical names in two libraries can produce confusing, hard-to-trace results.

🔗 Can one function library call functions defined in another library?

Yes. Once both libraries are associated with the same test, UFT loads all their functions into a single shared namespace, so any function in one library can call a function defined in another.

🤖 Can AI tools generate UFT/QTP function library code automatically?

Yes. AI assistants can draft VBScript functions from a plain-language description of the required steps, though a tester should still review the generated code for correct object references and error handling before use.

🧠 Can AI help migrate legacy VBScript function libraries to newer automation frameworks?

AI tools can translate VBScript logic into languages like Python or Java as a starting draft, but a QA engineer must verify object identification and business logic, since automated translation rarely preserves both perfectly.

#### Summarize this post with:

ChatGPT Perplexity Grok Google AI

**Stay Updated on AI** **Get Weekly AI Skills, Trends, Actionable Advice.**

##### Sign up for the newsletter

Subscribe for Free

You have successfully subscribed.  
Please check your inbox.

![AI-Newsletter]() Chosen by over **350,000+** professionals

[Scroll to top](#wrapper)

×

Search for:

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://www.guru99.com/#organization","name":"Guru99","sameAs":["https://www.facebook.com/Guru99Official","https://twitter.com/guru99com"],"logo":{"@type":"ImageObject","@id":"https://www.guru99.com/#logo","url":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","contentUrl":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","caption":"Guru99","inLanguage":"en-US"}},{"@type":"WebSite","@id":"https://www.guru99.com/#website","url":"https://www.guru99.com","name":"Guru99","publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US"},{"@type":"ImageObject","@id":"https://www.guru99.com/images/how-to-use-functions-in-qtp-uft.png","url":"https://www.guru99.com/images/how-to-use-functions-in-qtp-uft.png","width":"700","height":"250","caption":"How to use Functions in QTP/UFT","inLanguage":"en-US"},{"@type":"BreadcrumbList","@id":"https://www.guru99.com/uft-qtp-functions-library-call.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":"1","item":{"@id":"https://www.guru99.com","name":"Home"}},{"@type":"ListItem","position":"2","item":{"@id":"https://www.guru99.com/qtp","name":"UFT"}},{"@type":"ListItem","position":"3","item":{"@id":"https://www.guru99.com/uft-qtp-functions-library-call.html","name":"How to Use Functions in QTP/UFT in 3 Easy Steps"}}]},{"@type":"WebPage","@id":"https://www.guru99.com/uft-qtp-functions-library-call.html#webpage","url":"https://www.guru99.com/uft-qtp-functions-library-call.html","name":"How to Use Functions in QTP/UFT in 3 Easy Steps","dateModified":"2026-10-10T17:10:16+05:30","isPartOf":{"@id":"https://www.guru99.com/#website"},"primaryImageOfPage":{"@id":"https://www.guru99.com/images/how-to-use-functions-in-qtp-uft.png"},"inLanguage":"en-US","breadcrumb":{"@id":"https://www.guru99.com/uft-qtp-functions-library-call.html#breadcrumb"}},{"@type":"Person","@id":"https://www.guru99.com/author/editorial","name":"Editorial Team","url":"https://www.guru99.com/author/editorial","image":{"@type":"ImageObject","@id":"https://www.guru99.com/images/favicon-new-logo.png","url":"https://www.guru99.com/images/favicon-new-logo.png","caption":"Editorial Team","inLanguage":"en-US"},"sameAs":["https://www.guru99.com/editorial-team"],"worksFor":{"@id":"https://www.guru99.com/#organization"}},{"articleSection":"UFT","headline":"How to Use Functions in QTP/UFT in 3 Easy Steps","description":"This video tutorial will help you to create user defined functions for your code and make it easy to maintain and execute.","keywords":"qtp","speakable":{"@type":"SpeakableSpecification","cssSelector":[".entry-title",".summary"]},"@type":"Article","author":{"@id":"https://www.guru99.com/author/editorial","name":"Editorial Team"},"dateModified":"2026-10-10T17:10:16+05:30","image":{"@id":"https://www.guru99.com/images/how-to-use-functions-in-qtp-uft.png"},"copyrightYear":"2026","name":"How to Use Functions in QTP/UFT in 3 Easy Steps","subjectOf":[{"@type":"FAQPage","mainEntity":[{"@type":"Question","name":"What is the difference between a Function and a Sub in VBScript?","acceptedAnswer":{"@type":"Answer","text":"A Function can return a value to the caller and a Sub cannot. Use a Function when a script needs a result back, such as a calculated value or a pass/fail flag, and use a Sub for a self-contained action like clicking a button."}},{"@type":"Question","name":"What happens if two associated function libraries contain a function with the same name?","acceptedAnswer":{"@type":"Answer","text":"UFT does not raise an error. It silently calls whichever definition loaded last, based on the order libraries appear in Resources settings, so identical names in two libraries can produce confusing, hard-to-trace results."}},{"@type":"Question","name":"Can one function library call functions defined in another library?","acceptedAnswer":{"@type":"Answer","text":"Yes. Once both libraries are associated with the same test, UFT loads all their functions into a single shared namespace, so any function in one library can call a function defined in another."}},{"@type":"Question","name":"Can AI tools generate UFT/QTP function library code automatically?","acceptedAnswer":{"@type":"Answer","text":"Yes. AI assistants can draft VBScript functions from a plain-language description of the required steps, though a tester should still review the generated code for correct object references and error handling before use."}},{"@type":"Question","name":"Can AI help migrate legacy VBScript function libraries to newer automation frameworks?","acceptedAnswer":{"@type":"Answer","text":"AI tools can translate VBScript logic into languages like Python or Java as a starting draft, but a QA engineer must verify object identification and business logic, since automated translation rarely preserves both perfectly."}}]}],"@id":"https://www.guru99.com/uft-qtp-functions-library-call.html#schema-595969","isPartOf":{"@id":"https://www.guru99.com/uft-qtp-functions-library-call.html#webpage"},"publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US","mainEntityOfPage":{"@id":"https://www.guru99.com/uft-qtp-functions-library-call.html#webpage"}},{"@type":"VideoObject","embedUrl":"https://www.youtube.com/embed/vZKzykFsyfg","name":"How to Use Functions in QTP/UFT in 3 Easy Steps","description":"This video tutorial will help you to create user defined functions for your code and make it easy to maintain and execute.","uploadDate":"2020-01-22T00:00:00+05:30","thumbnailUrl":"https://i.ytimg.com/vi/vZKzykFsyfg/maxresdefault.jpg","hasPart":[],"width":"640","height":"390","@id":"https://www.guru99.com/uft-qtp-functions-library-call.html#schema-595971","isPartOf":{"@id":"https://www.guru99.com/uft-qtp-functions-library-call.html#webpage"},"publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US"}]}
```
