## Saturday, 29 March 2014

### Functions in VBScript

Functions are used to perform the specific task.Functions are used to increase the reusability of the code. Functions are similar to the procedures except one difference that functions can return value.

## Simple example of the function

Suppose you want to find the sum of 2 numbers. Without functions, you will write below code.

```a = 10
b = 20
c = a+ b
Msgbox c```

To add another 2 different numbers, you will use below code

```a = 33
b = 65
c = a+ b
Msgbox c```

In above examples, we have to add 2 numbers. So the operation is same (Repeating) So we can write the function which will take 2 parameters as shown below.

```function sum(byref a, byref b)

sum = cint(a) + cint(b)

end function```

## Calling functions in VBScript

To call the procedures we can use below statements.

```c = sum(10,20)
msgbox c
c =  sum(33,65)
msgbox c```

Thus we can call functions many times in the code. This reduces the lines of code  as well as maintainability of the code.

## Passing arguments to the function

We can pass the arguments to the function by 2 ways.
1. Pass by reference
2. Pass by value
Pass by reference

By default, values are passed to the function by reference. When we pass the values by reference the changes made to the variables in the called function are reflected in the calling function/procedure.

```a = 10
c = findsqr(a)
msgbox a  'prints 100
msgbox c  'prints 100

function findsqr(byref a)

a = a*a

findsqr = a

end function```

Pass by value

When we pass the values by value, the changes made to the variables in the called function are not reflected in the calling procedure or function.

```a = 10
c = findsqr(a)
msgbox a  'prints 10
msgbox c  'prints 100

function findsqr(byval a)

a = a*a

findsqr = a

end function```

## Other built-in functions in VBScript

1. msgbox
2. inputbox
3. eval
4. execute
msgbox function is used to show message to the user.
inputbox function is used to read the value from the user.

```a = inputbox("Enter the number")
msgbox a 'prints 33``` Inputbox function in vbscript
Eval and execute functions are used to execute any valid vbscript expression. Difference between 2 functions is that the operator = is handled in different ways.

```Eval always uses = as a comparison operator.
Execute always uses = as a assignment operator.```

`Example -`
```a=20
msgbox eval("a=10")  'will print false
msgbox a

a=20
execute("a=10")
msgbox a    'will print 10```
{"ft_lang_label":"__label__en","ft_lang_prob":0.82202846,"math_prob":0.9907343,"size":2738,"snap":"2021-04-2021-17","text_gpt3_token_len":689,"char_repetition_ratio":0.16276518,"word_repetition_ratio":0.35151514,"special_character_ratio":0.24433893,"punctuation_ratio":0.06967985,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99776775,"pos_list":[0,1,2],"im_url_duplicate_count":[null,2,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-01-26T16:14:36Z\",\"WARC-Record-ID\":\"<urn:uuid:d61ccea0-919e-4db6-a604-a88d6b053ceb>\",\"Content-Length\":\"49102\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:aaf11c7f-4270-42af-a112-714fe2b2fc88>\",\"WARC-Concurrent-To\":\"<urn:uuid:35b3e1e8-6e79-4c12-9c60-551f133eb706>\",\"WARC-IP-Address\":\"172.217.7.161\",\"WARC-Target-URI\":\"https://vbscript-interview-questions.blogspot.com/\",\"WARC-Payload-Digest\":\"sha1:BJITPCNTNZGC44OV45QQTNEWG2KMVGBD\",\"WARC-Block-Digest\":\"sha1:HMRNHJNUIOCWCQWG24PHSYAXAEWFYZ6T\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-04/CC-MAIN-2021-04_segments_1610704800238.80_warc_CC-MAIN-20210126135838-20210126165838-00069.warc.gz\"}"}