Index Harry Potter IV Czara Ognia Abracham Cohen Talmud Muktananda Swami Dokąd idziesz Andrzej Sapkowski Pani Jeziora Hogan Operacja Proteusz Kratochvil Stanisław Psychoterapia Kierunki metody badania (4) elektronika praktyczna 2002 imiona artyk1 Sienkiewicz Henryk Krzyżacy |
[ Pobierz całość w formacie PDF ] .The address in the instruction pointer is incremented to the next instructionpast the function call.That address is then placed on the stack, and it will bethe return address when the function returns.2.Room is made on the stack for the return type you've declared.On a systemwith two-byte integers, if the return type is declared to be int, anothertwo bytes are added to the stack, but no value is placed in these bytes.3.The address of the called function, which is kept in a special area ofmemory set aside for that purpose, is loaded into the instruction pointer, so thenext instruction executed will be in the called function.4.The current top of the stack is now noted and is held in a special pointercalled the stack frame.Everything added to the stack from now until the functionreturns will be considered"local" to the function.5.All the arguments to the function are placed on the stack.6.The instruction now in the instruction pointer is executed, thus executingthe first instruction in the function.7.Local variables are pushed onto the stack as they are defined.When the function is ready to return, the return value is placed in the area ofthe stack reserved at step 2.The stack is then popped all the way up to the stackframe pointer, which effectively throws away all the local variables and the argumentsto the function.The return value is popped off the stack and assigned as the value of the functioncall itself, and the address stashed away in step 1 is retrieved and put into theinstruction pointer.The program thus resumes immediately after the function call,with the value of the function retrieved.Some of the details of this process change from compiler to compiler, or betweencomputers, but the essential ideas are consistent across environments.In general,when you call a function, the return address and the parameters are put on the stack.During the life of the function, local variables are added to the stack.When thefunction returns, these are all removed by popping the stack.In coming days we'll look at other places in memory that are used to hold datathat must persist beyond the life of the function.SummaryThis chapter introduced functions.A function is, in effect, a subprogram intowhich you can pass parameters and from which you can return a value.Every C++ programstarts in the main() function, and main() in turn can call otherfunctions.A function is declared with a function prototype, which describes the return value,the function name, and its parameter types.A function can optionally be declaredinline.A function prototype can also declare default variables for one or more ofthe parameters.The function definition must match the function prototype in return type, name,and parameter list.Function names can be overloaded by changing the number or typeof parameters; the compiler finds the right function based on the argument list.Local function variables, and the arguments passed in to the function, are localto the block in which they are declared.Parameters passed by value are copies andcannot affect the value of variables in the calling function.Q&AQ.Why not make all variables global?A.There was a time when this was exactly how programming was done.As programsbecame more complex, however, it became very difficult to find bugs in programs becausedata could be corrupted by any of the functions--global data can be changed anywherein the program.Years of experience have convinced programmers that data should bekept as local as possible, and access to changing that data should be narrowly defined.Q.When should the keyword inline be used in a function prototype?A.If the function is very small, no more than a line or two, and won't becalled from many places in your program, it is a candidate for inlining.Q.Why aren't changes to the value of function arguments reflected in the callingfunction?A.Arguments passed to a function are passed by value.That means that theargument in the function is actually a copy of the original value.This concept isexplained in depth in the "Extra Credit" section that follows the Workshop.Q.If arguments are passed by value, what do I do if I need to reflect the changesback in the calling function?A.On Day 8, pointers will be discussed.Use of pointers will solve this problem,as well as provide a way around the limitation of returning only a single value froma function.Q.What happens if I have the following two functions?int Area (int width, int length = 1); int Area (int size);Will these overload? There are a different number of parameters, but the firstone has a default value.A.The declarations will compile, but if you invoke Area with oneparameter you will receive a compile-time error: ambiguity between Area(int,int) and Area(int) [ Pobierz całość w formacie PDF ] |
||||
Wszelkie Prawa Zastrzeżone! Kawa była słaba i bez smaku. Nie miała treści, a jedynie formę. Design by SZABLONY.maniak.pl. | |||||