f(x) = x

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).

^{2}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).

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

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

ReplyDeleteThese 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 :)

ReplyDeleteMaybe 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.

ReplyDeleteInformative for learning functions and basics.

ReplyDeleteHelpful stuff, thanks =]

ReplyDeleteYey! 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.)

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

ReplyDeleteThanks for this, keep it up!

ReplyDeleteI am studying maths now, especially functions and integrals so this helps, thanks!

ReplyDeletegreat explanation!

ReplyDeleteyeah very great, learned something new today :P

ReplyDeleteSome time ago I wanted to learn C, maybe its time to start again! Thanks man!

ReplyDelete