Thread: Making a function to shorten my code?

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    19

    Making a function to shorten my code?

    How can i make a function that does specific code like:

    system("cls");
    printf ("Testing...\n");
    sleep(1);
    system("cls");

    Because in my program, i have hundreds of lines like these but i would rather define it in the beginiing as say "Test" and just write like the function "Test" everytime i want it do do these functions. How would i do this?

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    You're kidding, right? Any basic introductory text or online on C takes you through what is needed to create functions.

    Have a look here for one of many online tutorials. That happens to be the first link I found by supplying "c function tutorial" to google.

    Note that sleep() is not a standard C function and system("cls") is not guaranteed to always give the same results with different host systems. If you are already calling it, you're obviously don't care about that.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    It looks like Test() is for debugging purposes. In this case one nice trick is to do something like this

    Code:
    void Test_noop_(void) {}
    void Test_actual_(void) {printf("...Testing stuff goes here...\n");}
    
    #ifdef NDEBUG
    #define Test(...) Test_noop_()
    #else
    #define Test(...) Test_actual_()
    #endif
    Then, whenever you use Test() in your program, it will do your testing commands. Then later when you compile in release mode (e.g. with the -DNDEBUG switch), those debugging commands will be magically silenced without having to change your source code.

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    19
    I wanted to use "test" as an example of a name for a function. But the commands would be the same if i named the function like "hello" or something?

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Basically, naming functions is the same as naming variables. "hello" is as valid a name as "test" or "Test".
    The only time the name matters is if the names you choose a name which clashes with other names elsewhere. E.g. suppose you name a variable or a function "printf" - then you're asking for trouble.

    Also a better way to answer this question is to try it out yourself. Can you see any difference in your program if you name the function "hello" as opposed to "test"?

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    19
    i get this error:
    error: expected ';', ',' or ')' before '=' token
    for this line:
    void hello(const char *message, const char *testMsg="Testing...\n")

  7. #7
    Registered User
    Join Date
    Jun 2012
    Location
    Here
    Posts
    23
    Code:
    void hello(const char *message, const char *testMsg="Testing...\n")
    Aren't default parameters illegal in 'C'? However, in 'C++' they are perfectly acceptable.

  8. #8
    Registered User
    Join Date
    Nov 2012
    Posts
    19
    How could I re write it for c?

  9. #9
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    In C every function has a fixed number of parameters. Well, there are variadic functions but if you look at the header files these are actually macros which themselves call plain functions that, in the end, have a fixed number of arguments.

    Anyway, if you really want to have "default arguments" a simple way is to specify a "wrapper function" that converts some default value, for example NULL in the case of a const char* into an appropriate default value of your choice. Example:

    Code:
    int main()
    {
        hello("my message", NULL);  // use default argument for argument 2
        hello("my message", "my test msg");
        return 0;
    }
    So how do you write hello() in such a way that NULL is converted into "Testing...\n" ? Here is one way

    Code:
    void hello_ (const char *message, const char *testMsg) {
        printf("message is %s\n", message);
        printf("testMsg is %s\n", testMsg);
    }
    
    // void hello(const char *message, const char *testMsg = "Testing...\n");
    void hello(const char *message, const char *testMsg)
    {
        if (testMsg == NULL)    hello_(message, "Testing...\n");
        else                    hello_(message, testMsg);
    }
    If this case NULL is a magic value that you are using to mean to the function "I dont care about this parameter so just give me the default"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Anyway to shorten this if statement?
    By dayalsoap in forum C Programming
    Replies: 3
    Last Post: 08-08-2011, 11:19 AM
  2. help to shorten my code.
    By hugoguan in forum C Programming
    Replies: 7
    Last Post: 12-01-2010, 02:19 AM
  3. [C] How to shorten this program?
    By milky in forum C Programming
    Replies: 13
    Last Post: 11-07-2009, 10:44 AM
  4. Need to simplify/shorten this code. Help.
    By Lonck in forum C++ Programming
    Replies: 5
    Last Post: 11-08-2007, 04:23 AM
  5. shorten the string
    By beon in forum C Programming
    Replies: 28
    Last Post: 11-14-2006, 12:56 AM