Saturday, March 23, 2013

Function in C Programming



                                           function



A  function is an independent
section of program code that performs a certain task and has been assigned a name.

 By  referencing a function’s name, your program can execute the code in the function.
The
program also can send information, called arguments, to the function, and the function
can return information to the main part of the program. The two types of C functions are
library functions, which are a part of the C compiler package, and user-defined functions,
which you, the programmer, create.
For example

#include <stdio.h>
int val1, val2, val3;
int product(int x, int y);

  int main( void ) // line 8
  {
     /* Get the first number */
    printf("Enter a number between 1 and 100: ");
    scanf("%d", &val1);     /* Get the second number */
    printf("Enter another number between 1 and 100: ");
    scanf("%d", &val2);

     /* Calculate and display the product */
     val3 = product(val1, val2);
     printf ("%d times %d = %d\n", val1, val2, val3);

    return 0;

  }   // line23



  int product(int x, int y)
  {
     return (x * y);
 }
Enter a number between 1 and 100: 35
Enter another number between 1 and 100: 23
35 times 23 = 805



The main() Function (Lines 8 Through 23)

The only component that is required in every executable C program is the  main() func-tion. In its simplest form, the  main() function consists of the name main followed by a
pair of parentheses containing the word void ((void) ) and a pair of braces ({}). You  can
leave the word void out and the program will still work with most compilers. The ANSI
standard states that you should include the word  void so that you know there is nothing
being sent to the  main function.
Within the braces are statements that make up the main body of the program. Under nor-mal circumstances, program execution starts at the first statement in main() and termi-nates at the last statement in  main() .


The #include Directive (Line 2)

The #include directive instructs the C compiler to add the contents of an include
file into your program during compilation. An  include file is a separate disk file
that contains information that can be used by your program or the compiler. Several of
these files (sometimes called  header files ) are supplied with your compiler. You rarely
need to modify the information in these files; that’s why they’re kept separate from your
source code. Include files should all have an .h extension (for example, stdio.h).
You use the  #include directive to instruct the compiler to add a specific include file to
your program during compilation. In Listing 2.1, the #include directive is interpreted to

mean “Add the contents of the file stdio.h.” You will almost always include one or more
include files in your C programs.

The Variable Definition (Line 4)
A variable is a name assigned to a location in memory used to store information.
Your program uses variables to store various kinds of information during pro-gram execution. In C, a variable must be defined before it can be used. A variable defini-tion informs the compiler of the variable’s name and the type of information the variable
is to hold. In the sample program, the definition on line 4, int val1, val2, val3;,
defines three variables—named  val1, val2, and val3—that will each hold an integer
value

The Function Prototype (Line 6)
A function prototype provides the C compiler with the name and arguments of
the functions contained in the program. It appears before the function is used. A
function prototype is distinct from a  function definition, which contains the actual state-ments that make up the function. (Function definitions are discussed in more detail later
today.


Program Statements (Lines 11, 12, 15, 16, 19, 20, 22,
and 28)
The real work of a C program is done by its statements. C statements display information
on-screen, read keyboard input, perform mathematical operations, call functions, read
disk files, and all the other operations that a program needs to perform. Most of this book
s devoted to teaching you the various C statements. For now, remember that in your
source code, C statements are generally written one per line and always end with a semi-
colon. The statements in multiply.c are explained briefly in the following sections.

The printf() Statement
The printf() statement (lines 11, 15, and 20) is a library function that displays inform
tion on-screen. The printf() statement can display a simple text message (as in lines
and 15) or a message and the value of one or more program variables (as in line 20).

No comments:

Post a Comment