how to write own defined function that includes an imported function? - function

I am learning Julia programming by reading the book Think Julia I am including the following:
enter image description here
forward is a function in the ThinkJulia module. It acts on the Turtle() object, to move it forward. Why, after the line using ThinkJulia , am I getting this error. Do I have to be more specific in Julia about importing functions? I thought using would give me access to all functions in that particular module?

You need to pass a value not to its type to forward, so define your function like this:
function forward_len(t::Turtle, d)
forward(t, d)
end
and things should work

The error message is clear, you do not have forward method matching your parameter types.
Try 'Forward' instead of 'forward';
function forward_len(t::Turtle, d)
Forward(t, d)
end
Source: https://juliagraphics.github.io/Luxor.jl/v0.11/turtle.html

Related

Visual Basic 6 - select appropriate function

I'm working on a visual basic 6 and we have product made of VB6 modules that use each other. Every module has it's own exe.
I'm having a problem when I'm referring to one function in one module, which works, and in another module it doesn't.
For instance, in one module I'm calling the original VB6 Round function which takes following params:
Round(number,0)
But in another module there's a function defined as
Function Round(ByVal X As Variant) As Variant
That should be called as
Round(number)
And that causes a compile time error and it says that function call has a wrong number of parameters, while on other modules where this function is undefined there are no errors.
Now, I could use it, but there are other places where I actually need to specify decimal point precision where I call it as
Round(number,2)
Round(number,3)
etc.
How do I disambiguate between these functions to call only and ONLY the original VB6 rounding function?
I would recommend to avoid such ambiguities by choosing better names for your methods. If you can´t change the method name you can use the full qualified name of the function.
VBA.Math.Round number, 2

Is there a specific name for a function that takes its output as input and does that parameter have a name?

I work with a BASIC programming language and have found it useful to write functions that rely on their output as a parameter. Such as
inOut = someFunction(inOut)
I'd like to call this a recursive function. but it doesn't seem right because it is not calling itself. Can someone tell me what the name of this type of function is and if the parameter/return has a special name?
Thanks!!
This is an ordinary function as any other. The thing you show is called reassingment. You can rename inOut on the left with newinOut and it will not change anything... there is absolutely nothing special about the function, it's a naming pattern, that's all.
In many languages (including VB, but not sure about classic BASIC) there's something called passing parameter by reference. It's not exactly what you posted, but rather simple
someFunction(inOut)
parameter is passed into the function, changed there and the change persists outside the function

Fortran: how to pass function name in a common block

In Fortran, is it possible to put a function in a common block as in:
COMMON /myblock/ func
(where x is some variable and func is a function).
My problem is that I would like to create a function s(x) that calls an external function func(x) but without passing func in s(x). For my project, s(x) has to be a function of only one variable, i.e., I do not want to do:
function s(x,func)
s=func(x)
Instead, I am hoping I could do:
function s(x)
common /myblock/ func
s=func(x)
Or, if someone has some other suggestion using modules or something, this will be great.
Thanks in advance for any help.
o.
and then have the same common (myblock) in the subroutine that calls s(x).
I don't believe that this is possible in any portable way. Some implementations may allow you to use some tricks to do it.
The modern way to do this is with a pointer to a function. The pointer could be passed as an argument or, for the design of this question, placed into a module. See, for example, Function pointer arrays in Fortran
I think you are not supposed to use common blocks for this, but modules. Put your function func in a module called myfunctions and then when needed insert at use myfunctions statement and thats it.
Modern fortran standards prohibit this. From 5.5.2 of Fortran 2003:
A common-block-object shall not be ... a function name, an entry name...
And at any rate, using global variables to pass around non-constant data is just a terrible, terrible idea. As ja72 points out, you could do this with modules, but I refuse to demonstrate it with sample code.

Why do I get a "Too many input arguments" error when not passing any?

I am working on some simple object-oriented code in MATLAB. I am trying to call one of my class methods with no input or output arguments in its definition.
Function definition:
function roll_dice
Function call:
obj.roll_dice;
When this is executed, MATLAB says:
??? Error using ==> roll_dice
Too many input arguments.
Error in ==> DiceSet>Diceset.Diceset at 11
obj.roll_dice;
(etc...)
Anyone have any ideas what could be causing it? Are there secret automatic arguments I'm unaware that I'm passing?
When you make the call:
obj.roll_dice;
It is actually equivalent to:
roll_dice(obj);
So obj is the "secret" automatic argument being passed to roll_dice. If you rewrite the method roll_dice to accept a single input argument (even if you don't use it), things should work correctly.
Alternatively, if you know for sure that your method roll_dice is not going to perform any operations on the class object, you can declare it to be a static method as Dan suggests.
For more information on object-oriented programming in MATLAB, here's a link to the online documentation.
I believe you can also get around this by declaring roll_dice to be a static method.

Accessing the Body of a Function with Lua

I'm going back to the basics here but in Lua, you can define a table like so:
myTable = {}
myTable [1] = 12
Printing the table reference itself brings back a pointer to it. To access its elements you need to specify an index (i.e. exactly like you would an array)
print(myTable ) --prints pointer
print(myTable[1]) --prints 12
Now functions are a different story. You can define and print a function like so:
myFunc = function() local x = 14 end --Defined function
print(myFunc) --Printed pointer to function
Is there a way to access the body of a defined function. I am trying to put together a small code visualizer and would like to 'seed' a given function with special functions/variables to allow a visualizer to 'hook' itself into the code, I would need to be able to redefine the function either from a variable or a string.
There is no way to get access to body source code of given function in plain Lua. Source code is thrown away after compilation to byte-code.
Note BTW that function may be defined in run-time with loadstring-like facility.
Partial solutions are possible — depending on what you actually want to achieve.
You may get source code position from the debug library — if debug library is enabled and debug symbols are not stripped from the bytecode. After that you may load actual source file and extract code from there.
You may decorate functions you're interested in manually with required metadata. Note that functions in Lua are valid table keys, so you may create a function-to-metadata table. You would want to make this table weak-keyed, so it would not prevent functions from being collected by GC.
If you would need a solution for analyzing Lua code, take a look at Metalua.
Check out Lua Introspective Facilities in the debugging library.
The main introspective function in the
debug library is the debug.getinfo
function. Its first parameter may be a
function or a stack level. When you
call debug.getinfo(foo) for some
function foo, you get a table with
some data about that function. The
table may have the following fields:
The field you would want is func I think.
Using the debug library is your only bet. Using that, you can get either the string (if the function is defined in a chunk that was loaded with 'loadstring') or the name of the file in which the function was defined; together with the line-numbers at which the function definition starts and ends. See the documentation.
Here at my current job we have patched Lua so that it even gives you the column numbers for the start and end of the function, so you can get the function source using that. The patch is not very difficult to reproduce, but I don't think I'll be allowed to post it here :-(
You could accomplish this by creating an environment for each function (see setfenv) and using global (versus local) variables. Variables created in the function would then appear in the environment table after the function is executed.
env = {}
myFunc = function() x = 14 end
setfenv(myFunc, env)
myFunc()
print(myFunc) -- prints pointer
print(env.x) -- prints 14
Alternatively, you could make use of the Debug Library:
> myFunc = function() local x = 14 ; debug.debug() end
> myFunc()
> lua_debug> _, x = debug.getlocal(3, 1)
> lua_debug> print(x) -- prints 14
It would probably be more useful to you to retrieve the local variables with a hook function instead of explicitly entering debug mode (i.e. adding the debug.debug() call)
There is also a Debug Interface in the Lua C API.