C langugae : Other Function

C langugae : Inline Function
All the instructions of a C program are contained in functions . Each function performs a certain task.All other functions are subroutines of the main( ) function (or otherwise dependent procedures, such as call-back functions), and can have any names you wish.The definition of a function consists of a function head (or the declarator), and a function block . The function head specifies the name of the function, the type of its return value, and the types and names of its parameters.
Normally, a function call has overhead. That means it takes execution time to set up the call, pass arguments, jump to the function code, and return. Reducing that time is one of the justifications for using function-like macros. C99 has an alternative, inline functions. The C99 standard has this to say: "Making a function an inline function suggests that calls to the function be as fast as possible. The extent to which such suggestions are effective is implementation-defined." So making a function an inline function may shortcut the usual function call mechanism, or it may do nothing at all.
inline int max( int x, int y )
{ return ( x >= y ? x : y ); }
The definitions of inline functions the compiler must have the function definition at hand in order to insert the inline code. For this reason, function definitions with inline are customarily written in header files.
Inline functions are ordinary functions, except for the way they are called in machine code. Like ordinary functions, an inline function has a unique address. If macros are used in the statements of an inline function, the preprocessor expands them with their values as defined at the point where the function definition occurs in the source code. However, you should not define modifiable objects with static storage duration in an inline function that is not likewise declared as static.
For the compiler to make inline optimizations, it has to know the contents of the function definition. This means the inline function definition has to be in the same file as the function call. For this reason, an inline function ordinarily has internal linkage. Therefore, if you have a multifile program, you need an inline definition in each file that calls the function. The simplest way to accomplish this is to put the inline function definition in a header file and then include the header file in those files that use the function. An inline function is an exception to the rule of not placing executable code in a header file. Because the inline function has internal linkage, defining one in several files doesn't cause problems
To create an in-line function, precede its definition with the inline keyword. For example, in this program, calls to the function max( ) are optimized:
#include < stdio.h >
inline int max(int, int);
inline int max(int a, int b)
{
return a > b ? a : b;
}
int main(void)
{
int x=5, y=10;
printf("Max of %d and %d is: %d\n", x, y, max(x, y));
return 0;
}
For a typical implementation of inline, the preceding program is equivalent to this one:
#include < stdio.h >
int main(void)
{
int x=5, y=10;
printf("Max of %d and %d is: %d\n", x, y, (x>y ? x : y));
return 0;
}
The reason that inline functions are important is that they help you create more efficient code while maintaining a structured, function-based approach. As you probably know, each time a function is called, a significant amount of overhead is generated by the calling and return mechanism. Typically, arguments are pushed onto the stack and various registers are saved when a function is called, and then restored when the function returns. The trouble is that these instructions take time. However, when a function is expanded in line, none of those operations occur. Although expanding function calls in line can produce faster run times, it can also result in larger code size because of duplicated code. For this reason, it is best to inline only very small functions. Further, it is also a good idea to inline only those functions that will have significant impact on the performance of your program.
C langugae : Recursive Function
A recursive function is one that calls itself, whether directly or indirectly. Indirect recursion means that a function calls another function (which may call a third function, and so on), which in turn calls the first function. Because a function cannot continue calling itself endlessly, recursive functions must always have an exit condition.
Recursion often can be used where loops can be used. Sometimes the loop solution is more obvious; sometimes the recursive solution is more obvious. Recursive solutions tend to be more elegant and less efficient than loop solutions.
A simple example of a recursive function is factr( ), which computes the factorial of an integer. The factorial of a number n is the product of all the whole numbers between 1 and n. For example, 3 factorial is 1 × 2 × 3, or 6. Both factr( ) and its iterative equivalent are shown here:
#include< stdio.h >
#include< conio.h >
void main()
{
int r,x;
printf("enter the value of r in between 1 to 7");
scanf("%d",&r);
if(r<1 || r>=8)
{
printf("invalid values");
exit(1);
}
x=factr(r);
printf("Factorial = %d",x);
getch();
}
int factr(int n)
{
int answer;
if(n==1) return(1);
answer = factr(n-1)*n;
return(answer);
}
The operation of the recursive factr( ) is a little more complex. When factr( ) is called with an argument of 1, the function returns 1. Otherwise, it returns the product of factr(n-1)*n. To evaluate this expression, factr( ) is called with n-1. This happens until n equals 1 and the calls to the function begin returning.
When a function calls itself, a new set of local variables and parameters are allocated storage on the stack, and the function code is executed from the top with these new variables. A recursive call does not make a new copy of the function. Only the values being operated upon are new. As each recursive call returns, the old local variables and parameters are removed from the stack, and execution resumes immediately after the recursive call inside the function. Recursive functions could be said to ''telescope" out and back.
Recursive functions depend on the fact that a function's automatic variables are created anew on each recursive call. These variables, and the caller's address for the return jump, are stored on the stack with each recursion of the function that begins.
Recursive functions are a logical way to implement algorithms that are by nature recursive, such as the binary search technique, or navigation in tree structures. In such cases, the iterative solution is generally faster in execution than the recursive function


Free Web Hosting