Syntax for Declaring Functions in Dev-C++

This is a discussion on Syntax for Declaring Functions in Dev-C++ within the C Programming forums, part of the General Programming Boards category; G'Day all, I'm trying to write programs that use multiple functions, but I can't seem to figure out the syntax ...

  1. #1
    Muz
    Muz is offline
    Registered User
    Join Date
    Jul 2007
    Posts
    4

    Syntax for Declaring Functions in Dev-C++

    G'Day all,

    I'm trying to write programs that use multiple functions, but I can't seem to figure out the syntax I need to use. I'm using the following syntax, and keep getting errors regarding undeclared functions:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        printf("1");
        Funct1();
        Funct2();
        system("PAUSE");
        return 0;
    }
    
    Funct1()
    {
        printf("2");
    }
    
    Funct2()
    {
        printf("3");
    }
    I've looked through the helpfiles of Dev-C++ and don't seem to be able to find much in the way of the correct syntax.
    Any help?
    Cheers.

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    /* function declarations - could be in your own header */
    void Funct1(void);
    void Funct2(void);
    
    /* function definitions */
    int main(void)
    {
        printf("1");
        Funct1();
        Funct2();
        system("PAUSE");
        return 0;
    }
    
    void Funct1(void)
    {
        printf("2");
    }
    
    void Funct2(void)
    {
        printf("3");
    }
    Functions have a return type and list of arguments you have to specify.
    If function does not return any value - use void as a return type.
    If function does not accepts any arguments in C - use void as arguments type list
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Muz
    Muz is offline
    Registered User
    Join Date
    Jul 2007
    Posts
    4
    Functions have a return type and list of arguments you have to specify.
    If function does not return any value - use void as a return type.
    Thanks alot, works fine now.
    Just a question though. When you say that my functions aren't returning a value, isn't the string counted as a value? Or is it only when I say "return 100;", or something similar?

    Can I declare the function as returning a string? How do I do that?

    Thanks alot, mate.

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    C-string is a char-array...
    array is a pointer to the first element
    so returning strings is possible - by returning pointer to the first element of the string...
    Problem here will be if the pointer points to the local variable of the function that does not exists after the function exits...

    so there are different solutions for this.

    1. You can return pointer to the string literal, like this:
    Code:
    const char* Bool2Str(int flag)
    {
       if(flag) return "true";
       return "false";
    }
    Because compiler saves the string literals in the read-only memory (at least most compilers) and these literals exist as long as your program run - it is safe to return const pointer to this literal.

    2. You can allocate dynamic memory for the string generated in the function and return pointer to this memory. In this case the calling function will free the memory after the string was used (like strdup does)

    3. The function can accept the buffer where will be the resulting string stored and return pointer to this buffer to indicate that the operation succeded (like strcpy) or null pointer to indicate that the operation failed (like fgets)

    4. You can declare some local variable static and return pointer to this variable - I cannot recomend to use this method because it has to many side effects, making your program not thread safe for example.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    > Just a question though. When you say that my functions aren't returning a value, isn't the string counted as a value?
    That's not a return value, that's a side effect - something the function does internally, _before_ it returns. The return value always corresponds to an explicit return statement, except in the following special cases:
    1) If a function returns void (i.e., no return value), then it returns automatically at the closing brace, even if there's no explicit "return;" statement there.
    2) main(), which returns int, will return 0 automatically at the closing brace in C++ or C99 (the newer but mostly unused C standard). In the more commonly used C90, if there is no explicit return statement, the return value is undefined, which is bad, so you should always use an explicit "return 0;" before the closing brace (a nonzero return value implies some kind of failure, so you should use 0 for success).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Avoiding Global variables
    By csonx_p in forum Windows Programming
    Replies: 32
    Last Post: 05-19-2008, 12:17 AM
  2. Includes making me insane >.<
    By IceDane in forum C Programming
    Replies: 14
    Last Post: 04-14-2008, 10:24 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  5. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 05:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21