Thread: How to create a C function

  1. #1
    ! |-| /-\ +3 1337 Yawgmoth's Avatar
    Join Date
    Dec 2002
    Posts
    187

    How to create a C function

    How do I create a C function? Something so that I can define a certain word, say help, as a function that is equivalent to:
    printf("[Help stuff here]");

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    Oh my...

  3. #3
    Registered User Pioneer's Avatar
    Join Date
    Dec 2002
    Posts
    59
    I don't understand what you want, can you try explaining it a different way?

  4. #4
    ! |-| /-\ +3 1337 Yawgmoth's Avatar
    Join Date
    Dec 2002
    Posts
    187
    You see, my problem is that I have a long bunch of code that appears repeatedly in my program. I don't want to type it over and over, and copying and pasting is too much work. So how can I make it so that I type one word instead of a whole bunch. E.g. the follwing 2 pieces of code are equivalent:

    1. calculate

    2. int x;
    printf("blahblahblah");
    int y;
    printf("blahblahblah");
    scanf("%d",x);
    printf("blahblahblah");
    scanf("%d",y);
    int z;
    z=x+y;
    printf("blahblahblah);

    So how can I do that?

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    4
    You can create a function before the main function, eg

    Code:
    int calculate()
    {
       int a, b;
       /* prompt a*/
       scanf("%d", &a);
       /* prompt b */
       scanf("%d", &b);
    
       return a+b;
    }
    
    main()
    {
       int sum;
        ...
        sum = calculate();
        ...
        return 0;
    }
    or you can define it at the end of main (conventional way) in which case you need to have a PROTOTYPE before main eg,

    Code:
    int calculate();   /* <-- prototype */
    
    main()
    {
       int sum;
        ...
        sum = calculate();
        ...
        return 0;
    }
    
    int calculate()
    {
       int a, b;
       /* prompt a*/
       scanf("%d", &a);
       /* prompt b */
       scanf("%d", &b);
    
       return a+b;
    }
    Hope this helps.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    For the record, you can prototype functions inside of other functions:
    Code:
    int main( void )
    {
        int myfunc( char * );
        int x;
    
        x = myfunc( "Hello" );
    
        return !!x; /* Just for fun. */
    }
    
    int myfunc( char * s )
    {
        printf( "%s\n", s );
        return 5;
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Building upon what Quzah just said:

    Code:
    #include <ctype.h>
    
    int main( void )
    {
        int myfunc( char *s) {
            for(;*s;s++)
                 *s = toupper(*s);
        }
        char s[] = "hello";
    
        myfunc(s);
        puts(s);
    
        return ~-1; /* Just for fun. */
    }

  8. #8
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    yeah, nested function definitions are not a part of the standard

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  3. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  4. Replies: 5
    Last Post: 02-08-2003, 07:42 PM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM