Ethical Hacking Programming, Blogging, Hosting, All Computer Software, PC Software Download, JAVA in hindi, HTML, PHP, C, C++, Free Learning, Software's Download, Technical Videos, Technical Tricks and Tips, How Make Money

Functions in C programming Class 17

Functions in C programming


Work breaks small computing tasks into smaller parts, and enables people to start what they have created instead of starting from scratch. Suitable actions hide details of action from parts of the program, which they do not need to know, thus clarifying the whole, and easing the pain of making changes [KR88, page 67].

Functions are fundamental to writing modular code. They provide the basic mechanism for en-closing low-level source code, hiding algorithmic details and presenting instead an interface that describes more intuitively what the code actually does. Functions present a higher level of abstrac-tion and facilitate a divide-and-conquer strategy for program decomposition. When combined with file-modular design (see Sections 5.7 and 11.6), functions make it possible to build and maintain large-scale software systems without being overwhelmed by complexity.

4.1 Function Prototypes


A function must be declared before it is used. This means that either the function declaration or definition must exist in the source file above the place where it is first called by some other function.

A function declaration, or prototype, is an interface specification. It states the function name, the number and type of input arguments, and the return value type. It enables the compiler to perform type-checking—to ensure the argument types being passed to the function match the interface definition—which catches many coding errors at compile time. Some example prototypes are as follows.

void some_procedure(void);

int string_length(char *str);

double point_distance(double, double, double, double);

Notice that the variable names are optional in the declaration, only the types matter. However, variable names can help clarify how a function should be used.

4.2 Function Definition


A function definition contains the actual workings of the function—the declarations and statements of the function algorithm. The function is passed a number of input parameters (or arguments) and may return a value, as specified by its interface definition.

Function arguments are passed by a transaction termed “pass-by-value”. This means that the function receives a local copy of each input variable, not the variable itself. Thus, any changes made to the local variable will not a ect the value of the variable in the calling function. For example,
int myfunc(int x, int y)

/* This function takes two int arguments, and returns an int. */

{

x *= 3;

++y;

return x + y;

}

void caller_func(void)

/* A function that calls myfunc() */

{

int a=1, b=2, c, d;

c = myfunc(a,b); /* c = 6 */
d = a + b; /* d = 3 */

}

In this case, the values given to myfunc () are respectively x = 1 and y = 2, and they are converted into the following statements x = 3 and y = 3. However, the values of a and b are una ected and d = 1+2 = 3.

To obtain a value from a function, it may specify a return value. Calling function is free to ignore return value, 1 but insert a (zero) cast in front of the call is a good practice to make it clear, for example,

int an_algorithm(int, int); /* Prototype: two int arguments, and returns an int. */

void caller_func(void)

{

int a=1, b=2, c;

c = an_algorithm(a,b); /* use return value */

an_algorithm(a,b); /* ignore return value (implicitly) */

(void)an_algorithm(a,b); /* ignore return value (explicitly) */

}

int an_algorithm(int x, int y)

{

return x*2 + x/y;

}

The return value can be of any type, but there is a limitation that any function may only have at most one return value. To return multiple values it is necessary to either (i) return a compound type in the form of struct, or (ii) directly manipulate the values of the input variables using an approach termed “pass-by-reference”. These methods are discussed in Sections 11.2 and 7.3, respectively.

While a function can only have one return value, it may possess several return statements. These define multiple exit points from the function, from which program-control returns to the next statement of the calling function. If a function is to return a value of a certain type, all return statements must return a value of that type. But, if a function does not return a value, then an empty return; su ces, and this may be omitted altogether for a no-value return occurring at the end of the function block.



int isleapyear (int year)

The standard function printf() is a good example of a function that returns a value that is nearly always ignored. It returns an int value, which is the number of characters printed, or a negative error value if the print fails.
/* Return true if year is a leap-year */

 {

if ( year % 4 ) return 0; /* not divisible by 4 */

if ( year % 100 ) return 1; /* divisible by 4, but not 100 */

if ( year % 400 ) return 0; /* divisible by 100, but not 400 */

return 1; /* divisible by 400 */

  }


Functions in C are recursive, which means that they can call themselves. Recursion tends to be less e cient than iterative code (i.e., code that uses loop-constructs), but in some cases may facilitate more elegant, easier to read code. The following code examples show two simple functions with both iterative and recursive implementations. The first calculates the greatest common divisor of two positive integers m and n, and the second computes the factorial of a non-negative integer n.

/* Iterative GCD: Returns the greatest common divisor of m and n. */

  int gcd (int m, int n)

  {

while (n) {
int tmp= n;

n= m%n;
m= tmp;

}
return m;

 }



   /* Recursive GCD */

   int gcdr (int m, int n)

   {

if (n==0) return m;

return gcdr(n, m%n);

   }


Notice that the factorial functions below incorporate argument checking via the standard library macro assert, which causes the program to terminate with an error message if the conditional expression is FALSE.

/* Iterative factorial */

  int factorial (int n)

   {

int result= 1;

assert(n>=0);

while (n)

result *= n−−;
return result;

   }

   /* Recursive factorial */

   int factorial r (int n)

   {
assert(n>=0);

if (n==0) return 1;

return n * factorial r(n−1);

}

Share:

No comments:

Post a Comment

Follow On YouTube