Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Top Posters

Who's Online (0)

Powered by Vanilla. Made with Bootstrap.
C for newcomers, Part 7
  • chroniccommand
    Posts: 1,389
    In this C for newcomers section, I will be going over creating functions in C, and using them in your program.

    Part 1
    So what is a function? It's basically a custom int main() you can call to the program from anywhere, anytime, because the function is defined. This cleans up code and allows you to repeat without copy + pasting and making your code super messy. Here is the very basic syntax of a function:

    void func()
    {
    statement(s);
    }

    This will create the function, func. We can now use func anywhere we want in the program. Let's make a very simple program that we will be expanding on later in this chapter.

    #include <stdio.h>

    int main()
    {
    printf(\"Do you know who's mean?\n\");
    bill();
    printf(\"Yes, bill is mean. But do you know who else is mean?\n\");
    jack();
    printf(\"Yes, you're right!\n\");
    return(0);
    }

    void bill()
    {
    printf(\"Bill is mean!\n\");
    }

    void jack()
    {
    printf(\"Jack is mean!\n\");
    }

    This is a simple code, that calls two function's in this program; jack() and bill()
    In C, functions must of course end with a semicolon, as it is a C statement.
    Remember that these functions are very simple functions. If you take a look at longer, more advanced C code, the functions are much more complex.

    So save, compile and run this simple program. Depending on your compiler you may get some errors. Ignore these for now and run it. You should get the output desired. Great!

    Fixing those damn warnings
    Now you may want to fix those pesky warnings the compiler is probably giving you when compiling the above program. This is fairly simple to accomplish. All we do is something called 'prototyping'. To prototype, we tell the compiler that the function will be happening somewhere in the program. To do this, look at this code:

    #include <stdio.h>
    void bill(void);
    void jack(void);
    int main()

    And the code would continue. But see the line
    void bill(void);

    This is telling the compiler that there is a function called "bill" and "jack" coming up. And we have those "void"'s in there because it's telling it that it's not returning any value. So now we prototyped the function, which should relieve the program of any warnings.

    Avoiding prototyping
    There is a simple way to avoid prototyping that many C programmers do. The answer is simple. Instead of declaring functions after the main function, declare them before! How genius! Now take a look at this fixed code:

    #include <stdio.h>


    void bill()
    {
    printf(\"Bill is mean!\n\");
    }

    void jack()
    {
    printf(\"Jack is mean!\n\");
    }

    int main()
    {
    printf(\"Do you know who's mean?\n\");
    bill();
    printf(\"Yes, bill is mean. But do you know who else is mean?\n\");
    jack();
    printf(\"Yes, you're right!\n\");
    return(0);
    }

    See? No warnings and no need to prototype. The compiler already knows that those functions exist from the beginning so they don't have to search for them after the main function. Take a look at this code I found on the internet. I will explain it after you take a look at it.

    #include <stdio.h>
    #define COUNT 20000000 /* 20,000,000 */
    void dropBomb(void); /* prototype */
    void delay(void);
    int main()
    {
    char x;
    printf(“Press Enter to drop the bomb:”);
    x=getchar();
    dropBomb();
    printf(“Key code %d used to drop bomb.\n”,x);
    return(0);
    }
    void dropBomb()
    {
    int x;
    for(x=20;x>1;x--)
    {
    puts(“ *”);
    delay();
    }
    puts(“ BOOM!”);
    }
    void delay()
    {
    long int x;
    for(x=0;x<COUNT;x++)
    ;
    }

    Nothing is really new here, except maybe the #define thingy in there. But I'll go over that in the next section, creating header files. The basic's of that is, it's creating a global constant "COUNT" that can be used anywhere.
    Now we can see the prototype's in this program. They are telling the compiler that the functions will come soon.
    Now we use the main function, create the functions and call them in main. Nothing new and nothing fancy at all. You should know all of this.

    Now these are the very basics of functions in C. When I go over function in my C for the intermediate, I will go over stuff such as using functions to declare int's and data types in the parenthesis.
    The next section will be creating your own Header files!

    --Chroniccommand
  • Xin
    Posts: 3,251
    Good job Chronic :) keep it up
    Xin
  • Bursihido
    Posts: 406
    thanks bro
    waiting for ur next part