What is the difference between a function and a subroutine?

前端 未结 11 1586
攒了一身酷
攒了一身酷 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-30 07:34

    Both function and subroutine return a value but while the function can not change the value of the arguments coming IN on its way OUT, a subroutine can. Also, you need to define a variable name for outgoing value, where as for function you only need to define the ingoing variables. For e.g., a function:

    double multi(double x, double y) 
    {
      double result; 
      result = x*y; 
      return(result)
    }
    

    will have only input arguments and won't need the output variable for the returning value. On the other hand same operation done through a subroutine will look like this:

    double mult(double x, double y, double result) 
    {
      result = x*y; 
      x=20; 
      y = 2; 
      return()
    }
    

    This will do the same as the function did, that is return the product of x and y but in this case you (1) you need to define result as a variable and (2) you can change the values of x and y on its way back.

提交回复
热议问题