What is the difference between a function and a subroutine?

前端 未结 11 1793
攒了一身酷
攒了一身酷 2021-01-30 06:32

What is the difference between a function and a subroutine? I was told that the difference between a function and a subroutine is as follows:

A function takes parameters

11条回答
  •  Happy的楠姐
    2021-01-30 07:23

    I am writing this answer from a VBA for excel perspective. If you are writing a function then you can use it as an expression i. e. you can call it from any cell in excel.

    eg: normal vlookup function in excel cannot look up values > 256 characters. So I used this function:

    Function MyVlookup(Lval As Range, c As Range, oset As Long) As Variant
      Dim cl As Range
      For Each cl In c.Columns(1).Cells
      If UCase(Lval) = UCase(cl) Then
      MyVlookup = cl.Offset(, oset - 1)
      Exit Function
      End If
      Next
    End Function
    

    This is not my code. Got it from another internet post. It works fine.

    But the real advantage is I can now call it from any cell in excel. If wrote a subroutine I couldn't do that.

提交回复
热议问题