Archived

This topic is now archived and is closed to further replies.

frankiej

Help using Functions

Recommended Posts

Hello everyone Can someone just shed a bit of light on functions for me, I have been reading about functions and in one of the books it says; A PROGRAM IS EXECUTED LINE BY LINE IN THE ORDER IT APPEARS IN YOUR SOURCE CODE, UNTIL A FUNCTION IS CALLED. THEN THE PROGRAM BRANCHES OFF TO EXECUTE THE FUNCTION. WHEN THE FUNCTION FINISHES, IT RETURNS CONTROL TO THE LINE FOLLOWING WHERE THE PROGRAM CALLED THE FUNCTION. All I would like to know exactly what the "Function" does please help me and be gentle I am still learning???

Share this post


Link to post
Share on other sites
Hi mate,

Ok, u know that when u start a C program it looks like this:


void main( void )
{
return;
}


this is the basic function that Windows looks for when any old skool C/C++ program starts. This main allows the standard command line style interface.

Every function consists of:

- Return Type - In this case it is the "void" before "main". If you want a function to do some standard bit of maths or something for you, then you are going to want to be able to use the value it generates. For now umust understand it as the value is returned from the function. Void indicates no return value/type.

- Parameters - This is the bit in the "()" after "main". Parameters are the numbers/ letters/strings/pointers that you pass tothe function for it to work with.

- function body - this is what lies between the "{ }" and is where u place the code that does the work.

Calling a function

It is generally accepted that the main() function is function that calls other functions.


#include <stdio.h>

int addIntegers(int a, int b)
{
int c;
c = a + b;
return c;
}

void main( void )
{
int Result = 0;
Result = addIntegers(1,2);
printf("%d", Result);
}


ok, so in this code u have declared a variable in main() to hold the result that is returned by the function. Then on the following line, u have said Result = function. This means that the code branches off to do the function and returns a value, which Result will catch. So then u can operate on Result. On that same line u have said addIntegers(1,2). this means u are calling the function and passing two values to it. Now look at the function and see that in the function header "int addIntegers(int a, int b)", it is roughly the same format as the main() function header. U are saying, creat a function called addIntegers that returns a number of type int and accepts two values which get stored in a and b. This function then creates a third variable called c, adds a and b together, and returns it. Then back to the main() function, printf... prints this value as text to the screen.

Hope this helps

regards,

ace

Share this post


Link to post
Share on other sites
quote:
Original post by ace_lovegrove

void main( void )
{
return;
}

this is the basic function that Windows looks for when any old skool C/C++ program starts. This main allows the standard command line style interface.


Actually, the proper function signature in C++ at least is int main() or int main(int argc, char** argv) (though both may be written somewhat differently). Anything else is non-standard and should not generally be expected to work.

quote:

It is generally accepted that the main() function is function that calls other functions.


Any function can call any function (i.e. any other function, or itself: The latter is known as recursion but is a slightly more advanced topic); this is not unique to main. Of course, since the program always begins in main, all but the most trivial programs usually will call functions from main - but you will call all sorts of functions from elsewhere, as well.

Share this post


Link to post
Share on other sites
Thanks for the info everyone

Still trying to get my head around the whole function scenario even when it is explained clearly but I suppose thats the joy of programming just got to keep studying until its clear. With regards to Funtions Im reading several books and in one of them they give an example of what a funtion does here it is


Imaging you were drawing a face ie. eyes, ears, nose and the your pencil breaks you would then go off to sharpen your pencil and return to the point you were drawing.

Is this a good example of what a function does? and If so why do you have to call a function and it has to branch off, why cant it stay and be inserted with the normal code???


Sorry I hope this makes sense

Regards

A very confussed beginner???

Share this post


Link to post
Share on other sites
Functions allow you to name common tasks your program will do for ease. If you put all the code your program does into main it be one big mess. A function doesn''t branching off per say, more or less the program is just referencing some code you placed elsewhere for use.

"Life is a double edged sword, we always hope to strike though adversity with the shining side..."

jkettles16 of Venosoft

Share this post


Link to post
Share on other sites
Is this a good example of what a function does? and If so why do you have to call a function and it has to branch off, why cant it stay and be inserted with the normal code???

Because it is wasteful to generate a full copy of the function code each and every time you need it. Instead, you put its code in a known location, and refer to it by name. So, if your pencil breaks again, you just reuse the function''s code, instead of writing it again.

A better example would be a cookbook. Recipes are broken down in a series of steps (chop the vegetables, pre-warm the oven...) But some operations refered to in a recipe are not ''elementary''. They will tell you to prepare a "XYZ sauce" and expect you to know how to do it - or they will provide a page reference. You can consider that part of the recipe as a function call. They do not write out explicitely the steps necessary to making a "XYZ sauce" in each and every recipe that uses it.

Additionally, a function can take parameters. Two pencils can be sharpened in the same way - you just walk to the pencil sharpener with a different pencil in hand (you ''passed'' a different ''pencil'' to your function). Or you can bake your pizza at different temperatures, for a different amount of time. You wouldn''t want to have to write code all over for just a minor change like that. That''s one benefit of functions.


“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”
— Brian W. Kernighan

Share this post


Link to post
Share on other sites
quote:
Original post by frankiej
Is this a good example of what a function does? and If so why do you have to call a function and it has to branch off, why cant it stay and be inserted with the normal code???



Code that is used in separate places can be put in a function so it can be used several times. This also avoids errors if you update the code.
Take the function

int addIntegers(int a, int b)
{
int c;
c = a + b;
return c;
}


Now if you have code that goes

int x = addIntegers(10,10);
int y = addIntegers(20,20);
writeln("%d\n", y - x);

If you change the code for the addIntegers, you only have to do it in one place. If a function is 100 lines, then if it''s used 10 times it would add 1000 lines if you don''t use functions but put the code instead of the function call, and everything would be much harder to read.

Share this post


Link to post
Share on other sites
Ok, I''ll give you a really basic description.

Think of a program as a recipe. The computer follows instructions one after another until the program is done:

Pancake Recipe
- get blueberries
- get sugar
- heat blueberries and add sugar to make blueberry syrup
- get sugar
- get eggs
- get small bowl
- beat together eggs and sugar in small bowl
- etc...

Now do you see how the first 3 steps are just to make the blueberry syrup? Instead of cluttering up the recipe with all these smaller steps, I''ll make a mini-recipe that just describes how to create the blueberry syrup. The main recipe will just reference the mini-recipe:

Pancake Recipe:
- create Blueberry Syrup (do Blueberry Syrup Mini-Recipe)
- get sugar
- get eggs
- get small bowl
- beat together eggs and sugar in small bowl
- etc...

Blueberry Syrup Mini-Recipe:
- get blueberries
- get sugar
- heat blueberries and add sugar to make blueberry syrup

Do you see how this is cleaner and easier to read?

The Pancake Recipe is your main program and the Blueberry Syrup Mini-Recipe is like a function. It''s just a way of collecting steps into one bundle.

Regards,
Jeff

Share this post


Link to post
Share on other sites
Wow you people are good, Thanks for the break down it makes it so much easier to understand. I will be on here alot now as you guys are better than the books, can I call on you all soon when I get to the next chapter "Variables and constants" im dreading this chapter looks really scary as im not great at math

Many Thanks

Frankiej

Share this post


Link to post
Share on other sites