Thread: C for beginners : Part 2 : Functions

1. C for beginners : Part 2 : Functions

The original "C programming for beginners" thread is located under Backtrack2 -> Tutorials (yes I made a mistake in putting it under Backtrack2, so if a moderator would care to move it to the programming forum I'd be much obliged).

---------------------------------

Let's take our original program for converting Fahrenheit to Celsius, now let's change it so that it converts five temperatures instead of just one. One way of doing that might be as follows:

Code:
```#include <stdio.h>

int main(void)
{
float i;

printf("Enter the temperature in Fahrenheit: ");
fflush(stdout);

scanf("%f", &i);

i -= 32;  /* Short-hand for  i = i - 32 */

i *= 5;   /* Short-hand for  i = i * 5 */

i /= 9;   /* Short-hand for i = i / 9 */

printf("Answer is: %f\n", i);

printf("Enter the temperature in Fahrenheit: ");
fflush(stdout);

scanf("%f", &i);

i -= 32;  /* Short-hand for  i = i - 32 */

i *= 5;   /* Short-hand for  i = i * 5 */

i /= 9;   /* Short-hand for i = i / 9 */

printf("Answer is: %f\n", i);

printf("Enter the temperature in Fahrenheit: ");
fflush(stdout);

scanf("%f", &i);

i -= 32;  /* Short-hand for  i = i - 32 */

i *= 5;   /* Short-hand for  i = i * 5 */

i /= 9;   /* Short-hand for i = i / 9 */

printf("Answer is: %f\n", i);

printf("Enter the temperature in Fahrenheit: ");
fflush(stdout);

scanf("%f", &i);

i -= 32;  /* Short-hand for  i = i - 32 */

i *= 5;   /* Short-hand for  i = i * 5 */

i /= 9;   /* Short-hand for i = i / 9 */

printf("Answer is: %f\n", i);

printf("Enter the temperature in Fahrenheit: ");
fflush(stdout);

scanf("%f", &i);

i -= 32;  /* Short-hand for  i = i - 32 */

i *= 5;   /* Short-hand for  i = i * 5 */

i /= 9;   /* Short-hand for i = i / 9 */

printf("Answer is: %f\n", i);

return 0;
}```
Now isn't that just a tiny bit annoying to have to write the same code out 5 times? Writing code out five times has three problems:
1) It's takes up a lot of room and takes time to read down through.
2) It's more difficult to maintain and more prone to bugs, because if you decide to change the code a little, you have to change it in five places.
3) The code takes up five times as much space in memory, and five times as much space in your executable file.

To get around this problem, someone devised the idea of having "reusable code" and putting it in a "function". To make the usable code run, you call the function. So let's take our code for converting Fahrenheit to Celsius and let's put it into a function:

Code:
```#include <stdio.h>

void ConvFtoC(void)
{
printf("Enter the temperature in Fahrenheit: ");
fflush(stdout);

scanf("%f", &i);

i -= 32;  /* Short-hand for  i = i - 32 */

i *= 5;   /* Short-hand for  i = i * 5 */

i /= 9;   /* Short-hand for i = i / 9 */

printf("Answer is: %f\n", i);
}```
The "void" at the start specifies that this function does not return a value. The "void" between the parentheses specifies that this function does not take any values. Between the two chain brackets (i.e. '{' and '}'), you have the code for the function. Now in "main", we can call this function five times:

Code:
```#include <stdio.h>

void ConvFtoC(void)
{
printf("Enter the temperature in Fahrenheit: ");
fflush(stdout);

scanf("%f", &i);

i -= 32;  /* Short-hand for  i = i - 32 */

i *= 5;   /* Short-hand for  i = i * 5 */

i /= 9;   /* Short-hand for i = i / 9 */

printf("Answer is: %f\n", i);
}

int main(void)
{
ConvFtoC();
ConvFtoC();
ConvFtoC();
ConvFtoC();
ConvFtoC();

return 0;
}```
Isn't that much nicer to read? You can read it quicker, you can make changes to it quicker, and also it takes up much less space in the exectuable file and also in RAM because the function code is only stored once and gets called five times.

Next we can play around with having a function take a value and also return a value. Let's write a function that gives you the square of a number:

Code:
```unsigned Sqr(unsigned i)
{
return i * i;
}```
At this point, I'm going to introduce the concept of "const". When you're defining a variable, you can make it "const" to say to the compiler that the value of this variable won't change after it has gotten its initial value. If you try to change its value afterwards, you'll get a compiler error. Our function "Sqr" above contains a variable called "i" and its value never changes, so we can make it "const":

Code:
```unsigned Sqr(unsigned const i)
{
return i * i;
}```
It's good practise to use const wherever you can, because:
1) It clearly expresses your intent that this value should not change
2) It reduces the possibility of bugs that come about from altering a value that shouldn't be altered.

So moving on, let's write a program that calculates the square of an unsigned integer:

Code:
```#include <stdio.h>

unsigned Sqr(unsigned const i)
{
return i * i;
}

int main(void)
{
unsigned i;

printf("Enter a non-negative integer value: ");
fflush(stdout);

scanf("%u", &i);  /* We use "%u" to get an unsigned int */

printf( "Answer is: %u\n", Sqr(i) );

return 0;
}```
Now in this example, the function "Sqr" is pretty stupid because it's handier just to write "i * i" directly in our "main" function, but I wanted to start off with an easy example.

Functions can only return one value, but they can take as many values as you want. (There is a way of getting a function to return more than one value, but we won't get into that now). We can write a function that calculates "a cubed, plus b squared, plus c":

Code:
```unsigned Func(unsigned a, unsigned b, unsigned c)
{
return a * a * a   +   b * b   +   c;
}

int main(void)
{
Func(4,5,9);

return 0;
}```

2. The original "C programming for beginners" thread is located under Backtrack2 -> Tutorials (yes I made a mistake in putting it under Backtrack2, so if a moderator would care to move it to the programming forum I'd be much obliged).
Done and very nice and well needed tutorial

3. Cool thanks. I'd like to know if there's many people actually following these tutorials, because "theberries" has offered me some webspace and it might be worthwhile putting a large C tutorial online, with a new topic every couple of days

So anyone out there, if you're following these tutorials please do a Quick Reply and say that you're listening

By the way this is actually giving me some good practise because I've applied for a job teaching computer programming in a local college here, so I'm kind of getting my lessons planned :P

4. Yep, Keep going.
Don't know much about c++ and my code after 1 month without working on it is impossable to understand.

5. Originally Posted by Virchanza
Cool thanks. I'd like to know if there's many people actually following these tutorials, because "theberries" has offered me some webspace and it might be worthwhile putting a large C tutorial online, with a new topic every couple of days

So anyone out there, if you're following these tutorials please do a Quick Reply and say that you're listening

By the way this is actually giving me some good practice because I've applied for a job teaching computer programming in a local college here, so I'm kind of getting my lessons planned :P
I am definitely listening, please continue!

I once attempted C reading "The C Programming Language Second Edition", but then Cisco CCNA started in school and I had no free time all of a
sudden :b. But your "tutorials" are great because they give an understanding without reading too much (: - and the ability to satisfy
yourself by writing a fully functional program. That I believe is important and you may consider in future tutorials .

6. Re:

I am listening!

7. Nice. I just received Hacking-The Art Of Exploitation. This is def. helping.

Thanks.

8. you have mail, virchanza

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•