Вы находитесь на странице: 1из 2

Functions in C. Right from the first line of a working C program, we encounter this thing called Functions.

Remember our first program in tutorial 1? int main() { return 0; } Here 'main' was a function. I skimmed you out of the essentials of the function 'main', deliberately. We will look at main in detail at end of this tutorial. Functions? Have I heard the name somewhere? Definitely you have. Yes, these are exactly similar to the Functions of your mat hematics taught in your high school. Just the way of writing is a bit different. So we know that if in mathematics, we are given, Let x, y be two variables f(x) = x2 y = f(x)

Really? Simple Maths in Programming? We know that y is a function of x. In simple words, y changes as x changes, in s uch a way that y is always the square of whatever x is. Similarly in computers w e can write, int x,y; int f(int a) { return a^2;} y = f(x); If you match the mathematical & computer versions of the above example, you will find it to be totally similar, except the way of defining the function. By the way, the carat (^) sign is used to denote exponentials & the semicolon (;) in C denoted the end of a statement. Just for the sake of being together in the thought process, in the code version above, We declare that we need two data containers of type int (the fast 4 byte mem ory chunk, remember?) to hold two numbers in the arbitrary variables x & y We declare a function 'f' which returns the value - input variable (a) to th e power 2. We assign y the value returned by the function 'f' when passed x as input.

Ohh no, I only have 26 alphabets. Please note that I used the function name 'f' to illustrate functions more intui tively. But please avoid the use of single lettered function names or even varia ble names. There is some mythical reasoning by some people that larger variable/

Function names, take larger space. As we had discussed in tutorial part 2, C compiler is going to handle things bef ore the program is run. I had also hinted that compilation occurs in more than j ust the steps we discussed. C compiler also has a process where variables are to kenized. Simply said, all the fancy variable/function names are going to be shor tened before use. So please be liberal in using descriptive function names. Is there more to functions? As you might have noticed, the structure of function is: output data type } This is followed throughout & that is all that is to functions in C. However the terms are different. The output data type is called a return data type. There can be only one output result. The Input data is termed as arguments. More than one variable input valu es can be passed separated by comma & surrounded by the brackets (). For eg - Pa ssing a,b as input to get a+b as output. Mystery of the Main function! Many beginners often wonder about the use of main function in C at all. I rememb er being put on with a question of environment variables by a professor, few yea rs back. I guess I was ready because I had taken the little pain to understand m ain. A main function is the entry point for any C program. This means that whenever w e compile a C program, the compiler looks for the main function. Consider this. Everyone creates his/her own programs. All use their own set of variables. In su ch a case, How does a compiler know where to start compiling from? By using a pr edefined entry point. As described in tutorial 1, the output data (return data) returned by main can b e used by the operating system to know if our program ran successfully. That is why we put the last line of our main function as return 0. This means, that if o ur program returns 0, it has successfully reached the last line after completing all the preceding steps. The input values (arguments) passed to our main function are called environment variables. It might not be of use to us as of yet, but it is useful in developin g applications where the variables are passed at the invocation of our program. For eg, A system program may use the environment variables passed to it like sys tem type, file structure, etc to handle system jobs. function name ( Input ) { input to output conversion steps

Вам также может понравиться