.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Melody Anderson

Home >> Articles >> General >> Post New Resource Bookmark and Share   

 Subscribe to Articles


Posted By:Shashi Ray       Posted Date: March 31, 2009    Points: 25    Category: General    URL: http://www.dotnetspark.com  



A function has a name, a list of arguments which it takes when called, and the block of code it executes when called. C functions are defined in a text file and the names of all the functions in a C program are lumped together in a single, flat namespace. The special function called "main" is where program execution begins. Some programmers like to begin their function names with Upper case, using lower case for variables and parameters, Here is a simple C function declaration. This declares a function named Twice which takes a single int argument named num. The body of the function computes the value which is twice the num argument and returns that value to the caller.



Computes double of a number.

Works by tripling the number, and then subtracting to get back to double.


static int Twice(int num) {

int result = num * 3;

result = result - num;





The keyword "static" defines that the function will only be available to callers in the file where it is declared. If a function needs to be called from another file, the function cannot be static and will require a prototype -- see prototypes below. The static form is convenient for utility functions which will only be used in the file where they are declared. Next , the "int" in the function above is the type of its return value. Next comes name of the function and its list of parameters. When referring to a function by name in documentation or other prose, it's a convention to keep the parenthesis () suffix,

so in this case I refer to the function as "Twice()". The parameters are listed with their types and names, just like variables.


Inside the function, the parameter num and the local variable result are "local" to the function -- they get their own memory and exist only so long as the function is executing.


This independence of "local" memory is a standard feature of most languages.

The "caller" code which calls Twice() looks like...


int num = 13;

int a = 1;

int b = 2;

a = Twice(a); // call Twice() passing the value of a

b = Twice(b + num); // call Twice() passing the value b+num

// a == 2

// b == 30

// num == 13 (this num is totally independent of the "num" local to Twice()


Things to notice...

(vocabulary) The expression passed to a function by its caller is called the "actual parameter" -- such as "a" and "b + num" above. The parameter storage local to the function is called the "formal parameter" such as the "num" in "static int Twice(int num)".


Parameters are passed "by value" that means there is a single copying assignment operation (=) from each actual parameter to set each formal parameter. The actual parameter is evaluated in the caller's context, and then the value is copied into the function's formal parameter just before the function begins executing. The alternative parameter mechanism is "by reference" which C does not implement directly, but which the programmer can implement manually when needed (see below). When a parameter is a struct, it is copied.


The variables local to Twice(), num and result, only exist temporarily while Twice() is executing. This is the standard definition for "local" storage for functions.


The return at the end of Twice() computes the return value and exits the function. Execution resumes with the caller. There can be multiple return statements within a function, but it's good style to at least have one at the end if a return value needs to be specified. Forgetting to account of a return somewhere in the middle of a function is a traditional source of bugs.



Shashi Ray

 Subscribe to Articles


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend