I want to define functions in Objectve C.
But I did not find any documentation that describes how to do it.
Can you give me hints where to find decriptiosn for it?
I want to define functions in Objectve C.
But I did not find any documentation that describes how to do it.
Can you give me hints where to find decriptiosn for it?
Can you give a bit more detail about what you're actually trying to accomplish?
Functions are more a C concept, whereas methods are Objective-C.
(You can define C functions in an Objective-C file but I'm not clear whether that's what you mean.)
Objective-C is a superset of C. Anything you can do in C, you can do in Objective-C in exactly the same way.
For example, this is perfectly valid Objective-C code:
int sum(int a, int b)
{
return a + b;
}
Thank You for Your answer!
I had tried this aleady. But it does not work: I got the messages
• 'no previous prototype for function sum'
and
• 'linker command failed with exit code 1'
Hans
The "no previous prototype" error comes from a compiler option that requires you to declare a prototype for every function. (For historical reasons in C, a function without a prototype was a ticking time bomb, sometimes. It's always better to declare a prototype, typically in a header file that users of the function can include. You can turn the message off in the build settings, but I don't recommend doing that.)
The "linker failed" error just indicates that something went wrong during linking. To find the actual error, look at the build transcript (Command-8), and expand the linker step's transcript. There'll be an error saying there's a missing symbol, or a duplicate symbol, or something like that.
Thank You very much! Probme solved:
(By ignorance?) I placed the function into an interface file instead of the appropriate implementation file.
Now I can proceed!!!
If the function is only to be used within the translation unit (source file) where it's defined, you should make it "static" so it has internal (rather than external) linkage. Just add the "static" keyword before the return type.
I have still problems:
• Calling a function from the same class: o.k.
• Calling a function from another class (even a subclass of the defining one):
•• x = f(int a) : ok
•• x = f(float a): print a within f --> garbage
•• same result with other input types
•• complex f ( wtih 'complex' defined by me): error message
This looks as if only int-functions work properly!
Well, it seems like you're not very familiar with C, and unfortunately this is not the right place for a tutorial. A couple of things:
— C functions don't belong to a class ("…from the same class…). They're global to the source file where the function is defined ( a "static" function) or to the entire application (a "public" function).
— C functions have one specific parameter type. There is no type overloading. So, if you defined a public function "f" that takes an int parameter, you can't call that function with a float or complex argument. You'll need a different function, with a different name, for each parameter type.
Normally, for a public function, you put a prototype in a header (.h) file, and #include this in every source file that references the function. That informs the compiler of the correct parameter type, and prevents you from incorrectly passing parameters of the wrong type.
So, there is a certain amount of book-keeping associated with C functions, whose purpose is to prevent errors (like the ones you've seen) when functions are used across multiple source files.
Your are right: I am absolutely unfamiliar with C.
Of course, I defined different functions 'f'.
What I overlooked in 'Kernighan - Ritchie': With C functions types of input variables as well as of return values always must be specified.
For the input variable my test ran. But the result was wrong:
In one class I defined the function - with 'double identDouble;' in the interface:
double identDouble(double z) {
printf("--> %f ", z);
double result = z ;
printf("result = %f ", result);
return result;}
And I called the function from within another class (connected with '#import ….h"):
double v = 0.33;
printf("identDouble: v = %f ", v);
double x = 2.0 * identDouble(v);
printf("--> %f\n",x);
The result was garbage:
identDouble: v = 0.330000 --> 0.330000 result = 0.330000 --> 38.000000
Apparently I must look into more details!
What is the prototype for "identDouble" in the .h file that you imported?
Note that C originally did not require function input parameters to have a declared type, since everything defaulted to "int". If your prototype doesn't match the actual function, you'll get the kinds of wrong answers that you got.
In accordance with the implementation file I defined: 'double identDouble;' in the interface file.
I thought this should be correct and enough.
No, that's not correct. What you have in the header is a declaration of a variable of type double, not a function. Since such a declaration would conflict with the definition of the function, I wonder if you're even importing the header file in the source file where you're defining or calling the function.
The correct declaration of the function would be:
double identDouble(double z);That is, it looks very similar to the definition except it has a semicolon instead of the code block that constitutes the body of the function. It needs at least the parentheses for the argument list and the types of the arguments. The names of the arguments are not required but can be helpful when reading the code. For a function with no arguments, you need to put "void" between the parentheses.
Thank You very much!!!
Finally; I understood the definition of functions.
To define functions in C I used the book of Kernighan and Ritchie. Besides a small misunderstanding this worked.
But I did not find the way a function is to be defined in Objective C. There is a lot of documentation in Xcode but I experienced difficulties to ge to the appropriate topics.
Once more: Thanks