How to work with external functions in Obj-C

Discussion in 'Mac Programming' started by Filini, Oct 26, 2009.

  1. macrumors newbie

    Filini

    Joined:
    Jan 17, 2009
    #1
    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.
     
  2. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    Objective-C is a proper super-set of C. You can do anything you can do in C. So a function call is identical.
     
  3. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #3
    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
     
  4. thread starter macrumors newbie

    Filini

    Joined:
    Jan 17, 2009
    #4
    Exactly, I meant how to do from this function - class method for Xcode, thanx a lot Pal:)

    to robbieduncan
    Thx too
     
  5. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #5
    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...
     
  6. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #6
    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
     
  7. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #7
    You are correct, that sounds about right.
     
  8. cea
    macrumors newbie

    Joined:
    Oct 26, 2009
    #8
    Hello,
    I had the same problem before. But i got help from Lee. You do not need to convert C to Obj-C.
    You can download the example from here (post #5) to see how it works (i think it is what you looking for):
    http://forums.macrumors.com/showthread.php?t=809552
     

Share This Page