Functions In C

You might also call rand this way:

if (rand() > 100)

Or this way:

rand();

In the latter case, the value returned by rand is discarded. You may never want to do this with rand, but many functions return some kind of error code through the function name, and if you are not concerned with the error code (for example, because you know that an error is impossible) you can discard it in this way.
You create procedures (in the Pascal sense) by giving the function a void return type. For example:

void print_header()
{
printf("Program Number 1\n");
printf("by Marshall Brain\n");
printf("Version 1.0, released 12/26/91\n");
}

This function returns no value, so it is a procedure. You can call it with the following statement:

print_header();

You must include () in the call. If you do not, the function is not called, even though it will compile correctly on many systems.
C functions can accept parameters of any type. For example:

int fact(int i)
{
int j,k;

j=1;
for (k=2; k<=i; k++)
j=j*k;
return j;
}

returns the factorial of i, which is passed in as an integer parameter. Separate multiple parameters with commas:

int add (int i, int j)
{
return i+j;
}

C has evolved over the years. You will frequently see functions such as add written in the "old style," as shown below:

int add(i,j)
int i;
int j;
{
return i+j;
}

It is important to be able to read code written in the older style. There is no difference in the way it executes; it is just a different notation. You should use the "new style," (known as "ANSI C") with the type declared as part of the parameter list, unless you know you will be shipping the code to someone who has access only to an "old style" (non-ANSI) compiler.
It is now considered good form to use function prototypes for all functions in your program. A prototype declares the function name, its parameters, and its return type to the rest of the program in a manner similar to a forward declaration in Pascal. To understand why function prototypes are useful, enter the following code and run it:

#include

void main()
{
printf("%d\n",add(3));
}

int add(int i, int j)
{
return i+j;
}

This code compiles without giving you a warning, even though add expects two parameters but receives only one, because C does not check for parameter matching either in type or count. You can waste an enormous amount of time debugging code in which you are simply passing one too many or too few parameters. The above code compiles properly, but it produces the wrong answer.
To solve this problem, C lets you place function prototypes at the beginning of (actually, anywhere in) a program. If you do so, C checks the types and counts of all parameter lists. Try compiling the following:

#include

int add (int,int); /* function prototype for add */

void main()
{
printf("%d\n",add(3));
}

int add(int i, int j)
{
return i+j;
}

The prototype causes the compiler to flag an error on the printf statement.
Place one prototype for each function at the beginning of your program. They can save you a great deal of debugging time, and they also solve the problem you get when you compile with functions that you use before they are declared. For example, the following code will not compile:

#include

void main()
{
printf("%d\n",add(3));
}

float add(int i, int j)
{
return i+j;
}

Related Links :

No comments:

Post a Comment


If you face any Problem in viewing code such as Incomplete "For Loops" or "Incorrect greater than or smaller" than equal to signs then please collect from My Web Site CLICK HERE


More Useful Topics...

 

History Of C..

In the beginning was Charles Babbage and his Analytical Engine, a machine
he built in 1822 that could be programmed to carry out different computations.
Move forward more than 100 years, where the U.S. government in
1942 used concepts from Babbage’s engine to create the ENIAC, the first
modern computer.
Meanwhile, over at the AT&T Bell Labs, in 1972 Dennis Ritchie was working
with two languages: B (for Bell) and BCPL (Basic Combined Programming
Language). Inspired by Pascal, Mr. Ritchie developed the C programming
language.

My 1st Program...


#include
#include
void main ()
{
clrscr ();
printf ("\n\n\n\n");
printf ("\t\t\t*******Pankaj *******\n");
printf ("\t\t\t********************************\n");
printf ("\t\t\t\"Life is Good...\"\n");
printf ("\t\t\t********************************");
getch ();
}

Next Step...


#include
#include

void main ()
{
clrscr ();
printf ("\n\n\n\n\n\n\n\n");
printf ("\t\t\t --------------------------- \n\n");

printf ("\t\t\t | IGCT, Info Computers, INDIA | \n\n");
printf ("\t\t\t --------------------------- ");

getch ();

}

Hits!!!