Monday, October 19, 2015

More on Functions


void function

void is a function that has no return value.

/* Prints a bad, I mean really bad, pun. */

#include <stdio.h>

main()
{
            print_pun();
            return 0;
            /* return 0 means end of program */
}

void print_pun(void)
{
            printf(“To C, or not to C:  That is the question. \n”);
}

General Form of Functions


return-type function name (parameters)
{
            declarations
            statements
}

The return type of a fucntion is the type of value that the function returns.  The following rules govern the return type:

·      Functions may not return arrays, but there are no other restrictions on the return type.
·      If the return type is omitted, the function is presumed to return a value type int.
·      Specifying the return type is void indicates that the function doesn’t return a value.

Local Variables
A variable declared in the body of a function is said to be local to the function.  In the following function, log is the local variable:


int log2(int n)
{
            int log = 0;  /* local variable */

            while (n>1) {
                        n /= 2;
            log++;
              }
            return log;
}

By default, local variables have the following properties:
·      Automatic storage duration.  The storage duration (or extent) of a variable is the portion of program execution during which storage for the variable exists.  Storage for a local variable is automatically allocated when the enclosing function is called and deallocated when the function returns, so the variable is said to have automatic storage duration.  A local variable doesn’t retain its value when its enclosing function returns.  When the function is called again, there is no guarantee that the variable will still have its old value.
·      Bock scope.  The scope of a variable is the portion of the program text in which the variable can be referenced.  A local variable has block scope:  It is visible from its point of declaration to the end of the enclosing function body only.  Since the scope of a local variable doesn’t extend beyond the function to which it belongs, other functions can use the same name for other purposes.

Global or External Variables
Passing  arguments is one way to transmit information to a function.  Functions can also communicate through global variables – variables that are declared outside the body of any function:

#include <stdio.h>
int x, y;  /* global variables */
double k, z; /* global variables */

main()
{
            declarations
            statements
}

return-type function name (parameters)
{
declarations
statements
}


The properties of global variables are different from those of local variables:
·      Static storage duration.  A value stored in a global variable wil stay there indefinitely until program termination.
·      File scope.  A global variable has file scope:  It is visible from its point of declaration to  the end of the enclosing file.  As a result, a global variable can be accessed by all functions that follow its declaration.

Pros and Cons of Global Variables

In most cases, it is better for functions to communicate through parameters rather than by sharing values.  Here’s why:
·      If we change a global variable during program modification, we’ll need to check every function in the same file to see how the change affects it.
·      If a global variable is assigned an incorrect value, it may be difficult to identify the guilty function.
·      Functions that rely on global variables are hard to reuse in other programs.  A function that depends on global variables is not self-contained.

Pre-Processor Directives
·      Macro definition.  The #define directive defines a macro; the #undef directive removes a macro definition.
·      File inclusion.  The #include directive causes the contents of a specified file to be included in a program.
·      Conditional compilation.  The #if, #ifdef, #ifndef, #elif, #else, and #endif directives allow blocks of text to be either included in or excluded from a program, depending on conditions that can be tested by the pre-processor.

Simple Macros

The definition of a simple macro has the form

            #define identifier  replacement-list

replacement-list is any sequence of C tokens:  It may include identifier, keyworkds, number, character constants, string literal, operators, and punctuation.  When it encounters a macro definition, the pre-processor makes anote that the identifier represents replacement-list .  Wherever identifier appears later in the file, the pre-processor substitutes replacement-list .

Don’t put any symbols in the macro definition – they’ll become part of the replacement list.  Putting the = symbol in a macro definition is a common error:

            #define N = 100  /*****WRONG!!!*****/

            #define N  100;  /*****WRONG!!! Semi-colon is not supposed to be used*****/

            #define N  100  /** CORRECT**/


Sample good macros:
            #define STR_LEN 80
            #define TRUE 1
            #define pi 3.14159
            #define CR ‘\r’
            #define MEM_ERR “Error:  not enough memory.”

Parameterized Macros

The definition of a parameterized macro has the form

            #define identifier (x1, x2, ..., xn)  replacement-list

Example:

            #define IS_EVEN(n)  ((n)%2==0)

So when
            if (IS_EVEN(i)) i++ ;

appears later in the program, the preprocessor will replace this line with:

            if (((n)%2==0)) i++ ;
           
                                                                       




No comments:

Post a Comment