Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
01/12/01 15:12
Read: times


 
#8154 - RE: does anyone know the reason
Variables are passed to functions, as Cory mentioned, to provide a certain level of "encapsulation." In theory, you create functions as units that solve a specific task given a number of inputs (the function parameters) and return a specific answer as the return value.

This method, when properly implemented, is very stable in the sense that once you know a function works, you can use it with confidence knowing that the rest of the program isn't going to mess it up. You can still create "functions" that solve a given problem when using global variables, but they aren't usually as portable--and there is more risk that another part of the program can mess it up by playing with variables that are used within the function.

Normally, a well-designed, encapsulated program doesn't need to have a thousand global variables, nor does it normally have to pass a given variable to a dozen different levels of functions. When that happens, it usually means the process itself should be optimized. I tend to have a small main() function, a number of process functions that are called by main(), and a library of specific functions that are called by the process functions. Rarely does my function nesting go beyond that level.

Personally, when I write in 'C' I pass parameters to functions rather than using global variables. In assembly language, I try to pass the parameters to the function in registers, if there are enough registers. If not, I resort to "global" variables.

Craig Steiner




List of 19 messages in thread
TopicAuthorDate
does anyone know the reason            01/01/70 00:00      
RE: does anyone know the reason            01/01/70 00:00      
RE: does anyone know the reason            01/01/70 00:00      
RE: does anyone know the reason            01/01/70 00:00      
RE: does anyone know the reason            01/01/70 00:00      
RE: does anyone know the reason            01/01/70 00:00      
RE: does anyone know the reason            01/01/70 00:00      
RE: does anyone know the reason            01/01/70 00:00      
RE: does anyone know the reason            01/01/70 00:00      
RE: does anyone know the reason            01/01/70 00:00      
RE: does anyone know the reason            01/01/70 00:00      
RE: does anyone know the reason            01/01/70 00:00      
RE: does anyone know the reason            01/01/70 00:00      
RE: does anyone know the reason            01/01/70 00:00      
RE: does anyone know the reason            01/01/70 00:00      
RE: does anyone know the reason            01/01/70 00:00      
RE: does anyone know the reason            01/01/70 00:00      
RE: does anyone know the reason            01/01/70 00:00      
RE: does anyone know the reason            01/01/70 00:00      

Back to Subject List