PDA

View Full Version : killer functions



SAMSEIED
05-17-2002, 01:27 PM
Hi:

I am reading this book called "Teach yourself C". This book was published in 1990 and author's name is Osborn.

He expalines the examples somehow very vague and difficult to understant. There must be a simple way to explain an example.

1. This program prints 123 on the screen.

/*This program has three fuctions.*/


#include <stdio.h>

main()

{

func2();

printf("3");
}

func2()
{
func1();
printf("2");

}

func1()
{
printf("1");

}


the explanation for this program accoring to author:

In this program, main() first calls fuc2(),
which then calls func1(). Next, func1() displays 1 and returns to fuc2(), which prints 2 and then returns to func2(), which prints2 and then returns to main(), which prints3.

I am sorry , you senior programmers, might think how stupid I am.
But the fact is, I am new to C and if this program had one function
I do understand it, I think, this program will give the same result if it had only one function. After main (), it should be: {
int num; num=123 then printf ("the value is %d", num");
}

but he has jump from one func to another and created a confusion. Is it OK, to read a C book that is 12 years old?

Thank you

Shiro
05-17-2002, 01:37 PM
I think he wanted to explain that functions are executed sequentially (?), is that correct English?

If in a function a second function is called, then that second function will be executed before the first function goes further.

So



function_a ()
{
function_b ()
printf ("a");
}

function_b ()
{
printf ("b");
}


has the same result as



function_a ()
{
printf ("b");
printf ("a");
}


C has changed through the years, so perhaps it is better to get a new book on C, one which also contains the last C standard. I can remember that in old C books the notation



int function (a, b)
int a;
int b;


was used. Currently we would do it like this



int function (int a, int b)


And the last C standard has some nice things like variable arrays etc.

SAMSEIED
05-17-2002, 02:52 PM
Thank you very much.It did help me alot.