Friday 18 March 2011

Simple Functions

Functions are the basis for writing programs in C.  They allow you to efficiently run repeated lines of code. For example, take a look at this mathematical function:

f(x) = x2

This function takes any real number x and squares it. Another way of writing this function could be:

f(x) = x*x

In C. Functions have 3 "Parts":
1) The function prototype which tells the program what to expect when the function is defined.
2) The function definition which tells the program how to execute the function.
3) The function call, which actually runs the function.

For this example, let's look at this square example. To declare a function, you must specify its return type (more on this is a sec) and then give the function an identifier and specify its parameter type. The parameters are the values that get passed into and used by the function (in f(x), our parameter is x). Here's a function prototype for our square function.

int square(int);

Later in your program, usually after your main method (unless you decide to declare it before, in which case a function prototype is not necessary), you will include your function definition. Here's the definition for square.

int square(int x)
{
    return x*x;
}

A really simple function. The program will take whatever value gets passed into the function. That value is now represented by x. The function now calculates the square of x and returns the value back to wherever the function was called. Here's an example showing how to call this function

#include<stdio.h>


int square(int);


int main()
{
    y=2;
    printf("The square of %d is % d.\n",y,square(y));
    return 0;
}


int square(int x)
{
    return x*x;
}


Compile this and you'll see that the output of the program is:
The square of 2 is 4.

Notice how to call the function, you simply write the function name and then the values you want to pass into the function (These are called the arguments).

13 comments:

  1. Informative as always thanks for all the helpful posts I'm learning a lot

    ReplyDelete
  2. I'm learnig more from this blog then I did in three years of art school.

    ReplyDelete
  3. These are same as javascripts? I've been thinking to learn to script Amnesia: the Dark Descent maps but couldn't find good guides, i'll follow your blog for further guidance :)

    ReplyDelete
  4. Maybe if I keep reading these i'll be able to actually learn C. Hopefully thats what you're shooting for? Keep up the good work.

    ReplyDelete
  5. Informative for learning functions and basics.

    ReplyDelete
  6. Yey! I can actually understand some of this! (Took a semester of Python, C++,and Java each...Then found out that while I love the idea of programming, I suck at it.)

    ReplyDelete
  7. I'm looking to start learning C++, i'll be using your website for help.

    ReplyDelete
  8. Thanks for this, keep it up!

    ReplyDelete
  9. I am studying maths now, especially functions and integrals so this helps, thanks!

    ReplyDelete
  10. yeah very great, learned something new today :P

    ReplyDelete
  11. Some time ago I wanted to learn C, maybe its time to start again! Thanks man!

    ReplyDelete