C working of function

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 ?

How function works in C Programming ?

Explanation : How function works in C Programming ?

Understanding Flow

  1. Firstly Operating System will call our main function.
  2. When control comes inside main function , execution of main starts (i.e execution of C program starts)
  3. Consider Line 4 :
num = square(4);
  1. We have called a function square(4). [ See : How to call a function ? ].
  2. 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.

  1. Function will return 16 to the calling function.(i.e main)
  2. Returned value will be copied into variable.
  3. printf will gets executed.
  4. main function ends.
  5. C program terminates.

Understanding Parameter Passing :

  1. We have something which is accessible inside calling function. (variable)
  2. Whenever we are inside called function we loose control on the data declared in the previous function.
  3. 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.
  4. 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]
  5. The original copy of the data is called as “Actual Parameter / Actual argument“.
  6. The Xerox copy of the data is called as “Formal Parameter / Formal Argument“.
  7. Any operations , any extortion made inside formal argument will not modify actual copy of the data.