Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Filini

macrumors newbie
Original poster
Jan 17, 2009
25
0
Hi guys, i can't find out how to work with external functions in obj-c,

In C:
Code:
#include<stdio.h>

//external function
int function1(int x, int y)
{
int sum;
sum=x+y;
return sum;
}

//main code
int main(int argc, char* argv[])
{
int a=5, b=10, aaa;
aaa=[B]function1[/B](a,b);
printf("Sum %d", aaa);
return 0;
}
How to convert this code in Objective-C???

Thx a lot.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
As robbieduncan stated, if you saved this file as a .m and compiled it as Objective-C, it would run just as you'd expect.

If you're wanting to do this in a more "Objective-C"-y way, you could make function1 a class method of a "utility" class. The signature in the class header would be:

Code:
+(int)sumInt:(int)a and:(int)b;

The implementation would be:

Code:
+(int)sumInt:(int)a and:(int)b {
  return a+b;
}

Let's say this is in a class called mathUtils. You would utilize this method like this:

Code:
...
#import "mathUtils.h"

int main(int argc, char *argv[]) {
  int x = 5;
  int y = 6;
  int w = 0;

  w = [mathUtils sumInt:x and:y];
  NSLog(@"The value of w is: %d",w);
}

-Lee
 

Filini

macrumors newbie
Original poster
Jan 17, 2009
25
0
If you're wanting to do this in a more "Objective-C"-y way, you could make function1 a class method of a "utility" class.
-Lee
Exactly, I meant how to do from this function - class method for Xcode, thanx a lot Pal:)

to robbieduncan
Thx too
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
Exactly, I meant how to do from this function - class method for Xcode, thanx a lot Pal:)

to robbieduncan
Thx too

It is worth noting that there is nothing wrong with using C functions in Objective-C/Cocoa code were appropriate. In fact there are some very good reasons why you might want to: primarily speed and code size. It is significantly cheaper to call a C function that it is to call an Objective-C method. And I believe that it will also result in somewhat smaller code (although this is unlikely to be an issue either way).

Whilst in this example the performance is unlikely to matter either way if you call the same method lots of times in a tight loop you will see significant performance advantages if you make it a C function instead of a class method...
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
It is worth noting that there is nothing wrong with using C functions in Objective-C/Cocoa code were appropriate. In fact there are some very good reasons why you might want to: primarily speed and code size. It is significantly cheaper to call a C function that it is to call an Objective-C method. And I believe that it will also result in somewhat smaller code (although this is unlikely to be an issue either way).

Whilst in this example the performance is unlikely to matter either way if you call the same method lots of times in a tight loop you will see significant performance advantages if you make it a C function instead of a class method...

You weren't saying this at all, but i just wanted to say, for the record, that I wasn't trying to say that the "right" way to do this particular task in Objective-C was a utility method, but thought that was what the OP was angling at.

As for code size, my understanding is that in your code, passing a message will result in a call to objc_msgsend, with one additional argument above and beyond the arguments being passed to the method (a pointer to the object the message is to be passed to). Therefore, you're using a tad more stack space, but no more "code". Obviously after that initial call there's a lot more code that has to get executed for the message to be passed, but in terms of your code, i think the size will be about the same.

-Lee
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
As for code size, my understanding is that in your code, passing a message will result in a call to objc_msgsend, with one additional argument above and beyond the arguments being passed to the method (a pointer to the object the message is to be passed to).

You are correct, that sounds about right.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.