Monday, July 27, 2009

Please can anyone explain me this paragraph from c programming language by ritchie kerninghan?

"One method of communicating data between functions is for the calling function to provide a


list of values, called arguments, to the function it calls. The parentheses after the function


name surround the argument list. In this example, main is defined to be a function that expects


no arguments, which is indicated by the empty list ( )".

Please can anyone explain me this paragraph from c programming language by ritchie kerninghan?
this relates to the what is called scope of the data. In some programming languages, all data is declared globally. Which means that you can read and change the value from anywhere in the program. The downside to this is that in large programs you may modify the data in conflicting ways and not recall what piece is changing what.





So, in modern languages, they have scope. WHile you can still make globals, it is usually frowned upon. Any variable you declare within a function has scope local to that function. This means that once your outside that function, using the variable name is meaningless. However, in order to compensate for this, you need to have some way to transfer data in and out of functions. Thus, you can provide 0, 1, or more parameters to functions which give it its input (0, of course, meaning you give it none). You can also specify a return type of void (which means it doesn't return a value) or one specific type.





So, the format is [return type] [function name] ([function parameters]) and, if there is more than one function parameter, each is separated by a comma. In the paragraph you provided, they are talking about (note, dots just for spacing)





void main ()


{


...[some code here]


}





In this case, the return type is void, the function name is main, and it has no input values and therefore the function parameters are empty. Thus... (). It would have been just as proper to write





void main(void)


{


...[some code here]


}


to explicitly state that there are no parameters. So, now assume I have a function called foo.





int foo (int x, int y)


{


...int sum = x + y;


...return sum;


}





It obviously adds two numbers called x and y. Since all programs start at main(), in order to use the foo function, we have to call it from main, like so...





void main()


{


...int a = 4;


...int b = 3;


...int c = 0;


...c = x + y; // this is WRONG! main has no idea what variables x or y are


...c = foo(4, 3); // this is correct and c would be set equal to 7 after foo returns the value


}





Similarly, even though main calls foo, if foo did something with a, b, or c, you would get a compile error because foo doesn't know what variables a, b, or c are as they were never declared within the scope of foo. The scope is easily known by the opening and closing curly braces





int foo (int, int )


{


...[stuff here is within the scope of foo]


}





if foo did use a and b, then that a and b is different from the main a and b. For example,





int foo (int x, int y)


{


...int a = x + y;


...return a;


}





The a in foo would be set equal to 7. This, in turn would return the value and set the main variable of c to 7. What happens to main's variable a, which was originally 4? NOTHING!
Reply:because C is process oriented language same as DOs process oriented OS
Reply:Suppose you have 2 functions, functionA and functionB. functionA has 0 arguments and calls functionB, which has 2 arguments. You'd see something like this:





function functionA() {


int x = 5;


int y = 6;


return functionB(x,y)


}





function functionB(a,b){


return a + b;


}





I don't know the exact C syntax, but that's how it works. FunctionA creates 2 variables x %26amp; y and passes them to FunctionB, which adds them together and returns the result to FunctionA. In turn, FunctionA returns this result to whatever called FunctionA.





(I forgot to mention, functionA in this case would return 11.)
Reply:c command is DOS
Reply:Basically, the easy way to think of it is that the 'arguments' are the values that the function needs to operate on. If you have a simple addition function that is supposed to figure out the result of adding two numbers, you need to provide the 2 numbers that need to be added. So, when you 'call' the function, you would have to 'pass' the 2 values to it, like so :





// call the function


addMyNums (int num1, int num2)





...given that num1 and num2 are variables that have had values assigned to them already. Now in this function, you would add the 2 values that are passed to it together, and 'return' the result.

lotus flower

No comments:

Post a Comment