Recursive InlineFunctions in C Programming with Example
Recursive InlineFunctions in C Programming with Example
When you divide a large program into various functions, it becomes easy to
manage each function individually. Whenever an error occurs in the program,
you can easily investigate faulty functions and correct only those errors. You
can easily call and use functions whenever they are required which
automatically leads in saving time and space.
1. Library functions
2. User-defined functions
A user-defined function is always written by the user, but later it can be a part
of 'C' library. It is a major advantage of 'C' programming.
In 'C' programming functions are divided into three activities such as,
1. Function declaration
2. Function definition
3. Function call
Function Declaration
Function declaration means writing a name of a program. It is a compulsory
part for using functions in code. In a function declaration, we just specify the
name of a function that we are going to use in our program like a variable
declaration. We cannot use a function unless it is declared in a program. A
function declaration is also called "Function prototype."
The function declarations (called prototype) are usually done above the main
() function and take the general form:
We consider the following program that shows how to declare a cube function
to calculate the cube value of an integer variable
#include <stdio.h>
/*Function declaration*/
int add(int a,b);
/*End of Function declaration*/
int main() {
Keep in mind that a function does not necessarily return a value. In this case,
the keyword void is used.
Function Definition
Function definition means just writing the body of a function. A body of a
function consists of statements which are going to perform a specific task. A
function body consists of a single or a block of statements. It is also a
mandatory part of a function.
Function call
A function call means calling a function whenever it is required in a program.
Whenever we call a function, it performs an operation for which it was
designed. A function call is an optional part in a program.
result = add(4,5);
#include <stdio.h>
int add(int a, int b); //function declaration
int main()
{
int a=10,b=20;
int c=add(10,20); //function call
printf("Addition:%d\n",c);
getch();
}
int add(int a,int b) //function body
{
int c;
c=a+b;
return c;
}
Output:
Addition:30
Function Arguments
A function's arguments are used to receive the necessary values by the
function call. They are matched by position; the first argument is passed to the
first parameter, the second to the second parameter and so on.
By default, the arguments are passed by value in which a copy of data is
given to the called function. The actually passed variable will not change.
5 + 10 = 15
Keep in mind that the values of a and b were passed to add function were not
changed because only its value was passed into the parameter x.
Variable Scope
Variable scope means the visibility of variables within a code of the program.
In C, variables which are declared inside a function are local to that block of
code and cannot be referred to outside the function. However, variables which
are declared outside all functions are global and accessible from the entire
program. Constants declared with a #define at the top of a program are
accessible from the entire program. We consider the following program which
prints the value of the global variable from both main and user defined
function :
#include <stdio.h>
int global = 1348;
void test();
int main() {
printf("from the main function : global =%d \n", global);
test () ;
return 0;}
Result:
When you use global variables, use them with caution because can lead to
errors and they can change anywhere in a program. They should be initialized
before using.
Static Variables
The static variables have a local scope. However, they are not destroyed
when exiting the function. Therefore, a static variable retains its value forever
and can be accessed when the function is re-entered. A static variable is
initialized when declared and needs the prefix static.
#include <stdio.h>
void say_hi();
int main() {
int i;
for (i = 0; i < 5; i++) { say_hi();}
return 0;}
void say_hi() {
static int calls_number = 1;
printf("Hi number %d\n", calls_number);
calls_number ++; }
Hi number 1
Hi number 2
Hi number 3
Hi number 4
Hi number 5
Recursive Functions
Consider the factorial of a number which is calculated as follow 6! =6* 5 * 4 * 3
* 2 * 1.
This calculation is done as repeatedly calculating fact * (fact -1) until fact
equals 1.
A recursive function is a function which calls itself and includes an exit
condition in order to finish the recursive calls. In the case of the factorial
number calculation, the exit condition is fact equals to 1. Recursion works by
"stacking" calls until the exiting condition is true.
For example:
#include <stdio.h>
int factorial(int number);
int main() {
int x = 6;
printf("The factorial of %d is %d\n", x, factorial(x));
return 0;}
int factorial(int number) {
if (number == 1) return (1); /* exiting condition */
else
return (number * factorial(number - 1));
}
Inline functions are mostly used for small computations. They are not suitable
when large computing is involved.
An inline function is similar to the normal function except that keyword inline is
place before the function name. Inline functions are created with the following
syntax:
inline function_name ()
{
//function definition
}
Output:
Addition: 30
Above program demonstrates the use of an inline function for addition of two
numbers. As we can see, we have returned the addition on two numbers
within the inline function only without writing any extra lines. During function
call we have just passed values on which we have to perform addition.
Summary
A function is a mini-program or a subprogram.
Functions are used to modularize the program.
Library and user-defined are two types of functions.
A function consists of a declaration, function body, and a function call
part.
Function declaration and body are mandatory.
A function call can be optional in a program.
C program has at least one function; it is the main function ().
Each function has a name, data type of return value or a void,
parameters.
Each function must be defined and declared in your C program.
Keep in mind that ordinary variables in a C function are destroyed as
soon as we exit the function call.
The arguments passed to a function will not be changed because they
passed by value none by address.
The variable scope is referred to as the visibility of variables within a
program
There are global and local variables in C programming