pointers - Passing by reference or use global variable in C -


which 1 better if want modify value function , bring main? , saying passing reference in c, mean using pointers, example. should put 'a' global variable?

#include <stdio.h> #include <conio.h>  int sum;  int example(int *a,int b) {     sum = *a + b;     (*a)++; }  int main(void) {     int a, b;     printf("tell me value of , b: \n");     scanf("%d %d", &a, &b);     example(&a, b);     printf("the result: %d . , new value of a: %d", sum, a);      printf("\n\nthe end\n");     getch();     return 0; } 

i find helps think of global variables existing in sort of special space outside normal hierarchy of functions in c program. if drew diagram each function in box, , lines between boxes showing function calls others during execution of program, main() @ top (because has no callers), , other functions appear below main() on 1 level or another, lines leading other functions above them.

most data lives within 1 function--inside single box in diagram. passed 1 function another: parameters , return values in language c fit in category. global variables "outside boxes"--in place function can see , change them, , "openness" makes them dangerous--if code in function can change them, it's hard you, or other programmers working on same code, once gets real-world size, keep track of possible places global data changed. (this problem of "mutable state" common programming languages in general, not c, , object-oriented , functional programming paradigms address extent.)

in example, fact asking question

which 1 better if want modify value function , bring main?

is clue particular value should not stored in global variable. have conceptualized particular piece of data shared between 2 functions, main() , example(). following reasoning gave above, implies variable should be, if possible, passed parameter example(), , returned main() via return @ end of example(). passing pointer a example(), modifying a inside example(), change a persists after control returns caller, ordinary idiom in c. needs documented parameters subject modification in way, or else situation global data, not quite bad, it's hard keep track of a might change , might change to.

this isn't globals bad. exist reason. if many parts of code need read/write access same data, , efficiency important, globals may best way accomplish that. in environments c code run these days--"close metal," in embedded systems or driver code--there hardware resources such registers or special-purpose memory can efficiently accessed through large set of globals. or in programs might have lot of state keep track of--a whole set of variables values affect behavior throughout program--and passing values around not inefficient makes program less clear because have same set of many parameters being passed in large number of functions. (the classic example of game, current state of game world, player's position, number of lives, etc. relevant in way majority of code.) in these cases it's crucial global variables documented , named don't conflict other named locals or function parameters.

for more advanced related topic, see this answer brief explanation, or this page longer discussion, of how scope (i.e., visibility) of global in c can restricted single source file static keyword, or shared across multiple files. gives more fine-grained control on accessibility of global data , can alleviate of concerns free use of globals mention above.


Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -