Friday, July 10, 2009

Functions defination in C Programming Language

Functions

Functions are easy to use; they allow complicated programs to be parcelled up into small blocks, each of which is easier to write, read, and maintain. We have already encountered the function main and made use of I/O and mathematical routines from the standard libraries. Now let's look at some other library functions, and how to write and use our own.

Calling a Function

The call to a function in C simply entails referencing its name with the appropriate arguments. The C compiler checks for compatibility between the arguments in the calling sequence and the definition of the function.

Library functions are generally not available to us in source form. Argument type checking is accomplished through the use of header files (like stdio.h) which contain all the necessary information. For example, as we saw earlier, in order to use the standard mathematical library you must include math.h via the statement

#include <>
at the top of the file containing your code. The most commonly used header files are
<>  -> defining I/O routines
<> -> defining character manipulation routines
<> -> defining string manipulation routines
<> -> defining mathematical routines
<> -> defining number conversion, storage allocation
and similar tasks
<> -> defining libraries to handle routines with variable
numbers of arguments
<> -> defining time-manipulation routines
In addition, the following header files exist:
<> -> defining diagnostic routines
<> -> defining non-local function calls
<> -> defining signal handlers
<> -> defining constants of the int type
<> -> defining constants of the float type
Appendix B in the K & R book describes these libraries in great detail.

Writing Your Own Functions

A function has the following layout:
return-type function-name ( argument-list-if-necessary )
{
...local-declarations...

...statements...

return return-value;
}
If return-type is omitted, C defaults to int. The return-value must be of the declared type.

A function may simply perform a task without returning any value, in which case it has the following layout:

void function-name ( argument-list-if-necessary )
{
...local-declarations...

...statements...
}

As an example of function calls, consider the following code:



/* include headers of library */
/* defined for all routines */
/* in the file */
#include <>
#include <>
/* prototyping of functions */
/* to allow type checks by */
/* the compiler */

void main()
{
int n;
char string[50];
/* strcpy(a,b) copies string b into a */
/* defined via the stdio.h header */
strcpy(string, "Hello World");

/* call own function */
n = n_char(string);
printf("Length of string = %d\n", n);
}

/* definition of local function n_char */
int n_char(char string[])
{
/* local variable in this function */
int n;
/* strlen(a) returns the length of */
/* string a */
/* defined via the string.h header */
n = strlen(string);
if (n > 50)
printf("String is longer than 50 characters\n");

/* return the value of integer n */
return n;
}


Arguments are always passed by value in C function calls. This means that local ``copies'' of the values of the arguments are passed to the routines. Any change made to the arguments internally in the function are made only to the local copies of the arguments. In order to change (or define) an argument in the argument list, this argument must be passed as an address, thereby forcing C to change the ``real'' argument in the calling routine.

As an example, consider exchanging two numbers between variables. First let's illustrate what happen if the variables are passed by value:




#include <>

void exchange(int a, int b);

void main()
{ /* WRONG CODE */
int a, b;

a = 5;
b = 7;
printf("From main: a = %d, b = %d\n", a, b);

exchange(a, b);
printf("Back in main: ");
printf("a = %d, b = %d\n", a, b);
}

void exchange(int a, int b)
{
int temp;

temp = a;
a = b;
b = temp;
printf(" From function exchange: ");
printf("a = %d, b = %d\n", a, b);
}


Run this code and observe that a and b are NOT exchanged! Only the copies of the arguments are exchanged. The RIGHT way to do this is of course to use pointers:



#include <>

void exchange ( int *a, int *b );

void main()
{ /* RIGHT CODE */
int a, b;

a = 5;
b = 7;
printf("From main: a = %d, b = %d\n", a, b);

exchange(&a, &b);
printf("Back in main: ");
printf("a = %d, b = %d\n", a, b);
}

void exchange ( int *a, int *b )
{
int temp;

temp = *a;
*a = *b;
*b = temp;
printf(" From function exchange: ");
printf("a = %d, b = %d\n", *a, *b);
}







1. A First Program

2.
Let's Compute

3.
Loops

4.
Symbolic Constants

5.
Conditionals

6.
Pointers

7.
Arrays

8.
Character Arrays

9.
I/O Capabilities

10.
Functions

11.
Command-line Arguments

12.
Graphical Interfaces: Dialog Boxes

No comments:

Blog List