difference of datatypes used before main() function [duplicate] - program-entry-point

This question already has answers here:
Difference between void main and int main in C/C++? [duplicate]
(8 answers)
Closed 8 years ago.
What is the beneficial effects and difference between using main() function as
void main()
or
int main(void)

A main function is called by a pre-load library that runs upon execution by the operating system. Many operating systems handle the result of an executable - and declaring main to return a type of int means that the main function can return a value. Obviously declaring main to return a type of void means that the main function cannot return a value - and some compilers will not permit this depending on what the target architecture of your build is.
The difference between declaring a function () or (void) is documented many times elsewhere on StackOverflow.

if you want your application to have the ability to return a value, you must use
int main(void).

First is wrong, second is correct. End of story.

Related

Call function from DLL based on some dynamic determined ordinal?

Can I call a function from LabVIEW that is at certain ordinal in some DLL, while the ordinal is determined at run-time?
I'm also interested if there is something similar to function pointers, like in 'C' language, which hold some dynamic function address?
If your intention is to call function by address, you will have to develop a wrapper in C by compiling a DLL from a code like that:
typedef int (*real_func_type)(int);
int wrapper(size_t address, int param1)
{
return ((real_func_type)address)(param1);
}
where real function proto is
int real_func(int param);
If your second question is whether LabVIEW has something similar to a function pointer, then the answer is that the closest thing is a VI reference. There are different types of VI references and different ways of creating and using them, so you would need to read up on that.
In any case, VI references are purely a LabVIEW construct. There's no mechanism for interacting with C function pointers directly and you can't create a function pointer to a VI and give that to the DLL function. For something like that you would also need some wrappers.
In your block diagram, create a case structure that takes your ordinal value. In each frame of the case structure, invoke the appropriate function from the DLL.

Definition of Method, Function, Operation, Procedure, Subroutine whats the exact difference? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Are there official definitions for these terms in OOP?
Or have they just evolved over time and depending on your former computer science education (or even your age) you use one or the other?
So far i found good definitions of method vs. function:
Difference between a method and a function
A function is a piece of code that is called by name.
... All data that is passed to a function is explicitly passed.
A method is a piece of code that is called by name that is associated
with an object.
And function vs. procedure:
What is the difference between a "function" and a "procedure"?
A function returns a value and a procedure just executes commands.
A procedure is a set of command which can be executed in order.
In most programming languages, even functions can have a set of
commands. Hence the difference is only in the returning a value part.
Even if there are subtle differences in the definition by the authors the main aspect seems to be: A method is always something which operates on an object in contrast to a function which gets all data passed to it by its parameters. If a function does not return a value it is called a procedure.
But how are subroutine and especially operation linked to these terms?
EDIT: Since this seems to be too broad here is an attempt to narrow it down: method, procedure and function are pretty clear from my former research. Also subroutine is not that vague anymore.
So the question is: What is an operation in the field of computer science?
The following is my understanding. Note that these are all "soft" definitions - there is no official definition of any of these, and so the exact meaning may vary between languages. (For example, "static methods" in Java and C# don't fit my definition of "method", unless you consider classes to be objects)
A subroutine is "some code that you can call whenever". It's literally a routine that is called by other routines (hence a sub-routine). "Subroutine" and "routine" seem to be quite archaic terms.
Think of BASIC's GOSUB instruction: (never mind that BASIC is a terrible language)
10 PRINT "Hello"
20 GOSUB 100
30 PRINT "Goodbye"
40 GOSUB 100
50 END
100 PRINT "World"
110 RETURN
This prints:
Hello
World
Goodbye
World
In Visual Basic, a subroutine or Sub is a function which doesn't return anything.
A procedure is the same thing as a subroutine, but less archaic.
A procedure can also mean a function which doesn't return anything.
A function is like a procedure, but instead of just being a list of commands, it can take parameters and return a value. You should be familiar with functions from many languages, but here's an example in C++:
#include <iostream>
using namespace std;
int calculate(int a, int b) {
return a + b - 2;
}
int main() {
cout << calculate(5, 2) << endl;
cout << calculate(100, 20) << endl;
int i = calculate(5, 1);
for(int k = 0; k < i; k++) // repeat the next line i times
cout << "Hello" << endl;
}
This prints:
5
118
Hello
Hello
Hello
Hello
Notice that the function calculate doesn't do the printing itself - instead, it returns a number which main can choose to print, or to do something else with. The third call to calculate returns 2, but the 2 isn't printed - instead, it determines how many times "Hello" is printed.
A function with no arguments, that returns nothing, is equivalent to a procedure (or subroutine).
A method is a function which can be called on an object. You might be familiar with non-static methods from Java or C#. A method has access to the state of the object it was called on (which is called the receiver).
Unlike with functions, methods are often polymorphic on the type of the receiver - it's not possible to directly see which sequence of commands will run when you make a method call.
A simple example of methods in Java, demonstrating polymorphism:
class Car {
int getNumberOfWheels() {return 4;}
void printNumberOfWheels() {
System.out.println(getNumberOfWheels());
}
}
class HondaCivic extends Car {
// no code here
}
class ReliantRobin extends Car {
int getNumberOfWheels() {return 3;}
}
class Main {
public static void main(String[] args) {
Car h = new HondaCivic();
Car r = new ReliantRobin();
h.printNumberOfWheels();
r.printNumberOfWheels();
}
}
This prints:
4
3
even though you might think that printNumberOfWheels always prints 4 - subclasses can "intercept" the getNumberOfWheels method call.

lua modules - what's the difference between using ":" and "." when defining functions? [duplicate]

This question already has answers here:
Difference between . and : in Lua
(3 answers)
Closed 8 years ago.
I'm still playing around with lua modules and I've found the following "interesting" issue that occurs depending on how you create your methods / functions inside a module.
Note the following code in a file called test_suite.lua:
local mtests = {} -- public interface
function mtests:create_widget(arg1)
print(arg1)
-- does something
assert(condition)
print("TEST PASSED")
end
return mtests
Using the above code, arg1 is always nil, no matter what I pass in when calling create_widget(). However, if I change the definition of the function to look like this:
function mtests.create_widget(arg1) -- notice the period instead of colon
print(arg1)
-- does something
assert(condition)
print("TEST PASSED")
end
then, the system displays arg1 properly.
This is how I call the method:
execute_test.lua
local x = require "test_suite"
x.create_widget(widgetname)
Can you tell me what the difference is? I've been reading: http://lua-users.org/wiki/ModuleDefinition
But I haven't come across anything that explains this to me.
Thanks.
All a colon does in a function declaration is add an implicit self argument. It's just a bit of syntactic sugar.
So if you're calling this with (assuming you assign the mtests table to foo), foo.create_widget(bar), then bar is actually assigned to self, and arg1 is left unassigned, and hence nil.
foo = {}
function foo:bar(arg)
print(self)
print(arg)
end
Calling it as foo.bar("Hello") prints this:
Hello
nil
However, calling it as foo:bar("Hello") or foo.bar(foo, "Hello") gives you this:
table: 0xDEADBEEF (some hex identifier)
Hello
It's basically the difference between static and member methods in a language like Java, C#, C++, etc.
Using : is more or less like using a this or self reference, and your object (table) does not have a arg1 defined on it (as something like a member). On the other way, using . is just like defining a function or method that is part of the table (maybe a static view if you wish) and then it uses the arg1 that was defined on it.
. defines a static method / member, a static lib, Which means you can't create a new object of it. static methods / libs are just for having some customized functions like printing or download files from the web, clearing memory and...
: Is used for object members, members that are not static. These members change something in an object, for example clearing a specified textbox, deleting an object and...
Metamethod functions(Functions that have :) can be made in lua tables or C/C++ Bindings. a metamethod function is equal to something like this on a non-static object:
function meta:Print()
self:Remove()
end
function meta.Print(self)
self:Remove()
end
Also, with . you can get a number/value that doesn't require any call from a non-static or static object. For example:
-- C:
int a = 0;
-- Lua:
print(ent.a)
-- C:
int a()
{
return 0;
}
-- Lua:
print(ent:a())
same function on a static member would be:
print(entlib.a())
Basically, each non-static object that has a function that can be called will be converted to : for better use.

What does ^ in objective C mean? [duplicate]

This question already has answers here:
Caret in objective C
(3 answers)
Closed 7 years ago.
What does ^ character in Objective C mean? like in the following code
TWTweetComposeViewControllerCompletionHandler
completionHandler =
^(TWTweetComposeViewControllerResult result) {
switch (result)
{
...
}
[self dismissModalViewControllerAnimated:YES];
};
It denotes a "block", a piece of code that can be packaged into an object and used later. In your example it specifies a completion handler to be called later, presumably when the user clicks "OK" or some similar button to close an alert.
Blocks can also be used with Grand Central Dispatch and in that case they are used to produce a unit of code that can be run on another thread, both synchronously and asynchronously.
This is a "block", you can read about this in the Apple-Developer-doc, it's mainly used for Multithreading.
It's used as part of the definition of a block:
^(return_type retval) {
statements;
}
You'll see it as part of declarations of blocks too.

Calling a function by name input by user

Is it possible to call a function by name in Objective C? For instance, if I know the name of a function ("foo"), is there any way I can get the pointer to the function using that name and call it? I stumbled across a similar question for python here and it seems it is possible there. I want to take the name of a function as input from the user and call the function. This function does not have to take any arguments.
For Objective-C methods, you can use performSelector… or NSInvocation, e.g.
NSString *methodName = #"doSomething";
[someObj performSelector:NSSelectorFromString(methodName)];
For C functions in dynamic libraries, you can use dlsym(), e.g.
void *dlhandle = dlopen("libsomething.dylib", RTLD_LOCAL);
void (*function)(void) = dlsym(dlhandle, "doSomething");
if (function) {
function();
}
For C functions that were statically linked, not in general. If the corresponding symbol hasn’t been stripped from the binary, you can use dlsym(), e.g.
void (*function)(void) = dlsym(RTLD_SELF, "doSomething");
if (function) {
function();
}
Update: ThomasW wrote a comment pointing to a related question, with an answer by dreamlax which, in turn, contains a link to the POSIX page about dlsym. In that answer, dreamlax notes the following with regard to converting a value returned by dlsym() to a function pointer variable:
The C standard does not actually define behaviour for converting to and from function pointers. Explanations vary as to why; the most common being that not all architectures implement function pointers as simple pointers to data. On some architectures, functions may reside in an entirely different segment of memory that is unaddressable using a pointer to void.
With this in mind, the calls above to dlsym() and the desired function can be made more portable as follows:
void (*function)(void);
*(void **)(&function) = dlsym(dlhandle, "doSomething");
if (function) {
(*function)();
}