0

I'm working my way through a programming book which uses pseudocode for all of its examples and I came across Float: function() as a function parameter, like so:

Float: UseTrapezoidRule(Float: function(), Float: xmin, Float: xmax, Integer: num_intervals) // Calculate the width of a trapezoid. Float: dx = (xmax - xmin) / num_intervals // Add up the trapezoids' areas. Float: total_area = 0 Float: x = xmin For i = 1 To num_intervals total_area = total_area + dx * (function(x) + function(x + dx)) / 2 x = x + dx Next i Return total_area End UseTrapezoidRule

I have seen parameters such as Float: x which I understand, but I don't know what the first parameter Float: function() means or does exactly. FWIW, I'm a JS developer. I never have to declare function param types but I understand the concept.

Am_I_Helpful
  • 18,735
  • 7
  • 49
  • 73
Spencer Carnage
  • 2,056
  • 2
  • 28
  • 41
  • Tag the language of course, to me it seems as if Float is a parameter passed to the Function,e.g., `function(Float)`; – Am_I_Helpful Jan 05 '15 at 05:35
  • @shekharsuman The language is `pseudocode` which is tagged in the post. – Spencer Carnage Jan 05 '15 at 05:43
  • At a guess: `MyFunction` returns a `Float`, and it takes one argument: a function with no arguments that returns a `Float`. Is `function` used anywhere inside `MyFunction`? –  Jan 05 '15 at 05:50
  • @Evert I updated the example function with the actual function from the book, which should help clear things up a little bit. – Spencer Carnage Jan 05 '15 at 05:57
  • Yes, so the first argument is indeed a function (simply called `function`); it does, however, take one argument (a Float), which I guess for simplicity/brevity is not specified at the top, and only shows up in the actual code. In practice, `function` is of course the function you want to integrate from `xmin` to `xmax`. –  Jan 05 '15 at 06:15

1 Answers1

2

Given the nature of two Function calls Function() and UseTrapezoidRule(), it's been confirmed that the term Float acts as the return type of the Function().

As you can see, Float: UseTrapezoidRule() returns total_area which is of type Float,so the notation which you asked about follows the same pattern.

Float: UseTrapezoidRule(Float: function(), Float: xmin, Float: xmax, Integer: num_intervals)
// here UseTrapezoidRule() returns "total_area" at the end which declares that this function's return value is of type Float. 

` Hence, the one which you asked about acts as the return type & value of the function(),the nature of the return-variable acting as a Float variable.

Float: function() will return a variable of type Float which acts as an input argument to the other function UseTrapezoidRule(Float,Float,Float,Integer).

Am_I_Helpful
  • 18,735
  • 7
  • 49
  • 73
  • When `function()` is being used, like in `(function(x) + function(x + dx))` is that just taking the value of `x` and `x + dx` and converting them into `Float` values, much like `parseInt` does with JavaScript? If `x` = `"5"`, `function(x)` would convert x to `5`? – Spencer Carnage Jan 06 '15 at 01:48
  • @SpencerCarnage-It depends on how Function is defined,what it's doing internally! But, at the last,it'll return some Float vaue for sure, it might be clear after getting the code of Function() as to what it is returning! – Am_I_Helpful Jan 06 '15 at 01:55