Table of content
How Function works in C Programming?
C programming is modular programming language. We must divide C program in the different modules in order to create more readable, eye catching ,effective, optimized code. In this article we are going to see how function is C programming works ?
Explanation : How function works in C Programming ?
- Firstly Operating System will call our main function.
- When control comes inside main function , execution of main starts (i.e execution of C program starts)
- Consider Line 4 :
num = square(4);
- We have called a function square(4). [ See : How to call a function ? ].
- We have passed “4” as parameter to function.
Note : Calling a function halts execution of the current function , it will execute called function , after execution control returned back to the calling function.
- Function will return 16 to the calling function.(i.e main)
- Returned value will be copied into variable.
- printf will gets executed.
- main function ends.
- C program terminates.
Understanding Parameter Passing :
- We have something which is accessible inside calling function. (variable)
- Whenever we are inside called function we loose control on the data declared in the previous function.
- If we have to perform certain operations on the data then we pass a xerox copy of the data to the called function so that called function can perform certain operations on the data and will return us a result.
- Means in the program we have two copies of the data
- Original Copy [ Calling function have this Original Copy]
- Xerox Copy [Called function have Xerox Copy]