Function in C Programming
Function in C Programming
Function Declaration
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
Variable Scope
Variable scope means the visibility of variables within a code
of 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 entire program.We consider the
following program which print the value of global variable
from both main and used defined function :
<pre>
#include <stdio.h>
int global = 1348;
void test();
int main() {
printf("from the main function : global =%d \n", global);
test () ;
return 0;}
void test (){
printf("from user defined function : global =%d \n",
global);}
</pre>
Result: <pre>
from the main function : global =1348
from user defined function : global =1348
</pre>
We discuss the program details:
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.
The following program uses a static variable:
<pre>
#include <stdio.h>
void say_hi();
int main() {
int i;
for (i = 0; i < 5; i++) { say_hello();}
return 0;}
void say_hi() {
static int calls_number = 1;
printf("Hi number %d\n", calls_number);
calls_number ++; } </pre>
The program displays :
<pre>
Hi number 1
Hi number 2
Hi number 3
Hi number 4
Hi number 5
</pre>
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 who 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:
<pre>
#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));
} </pre>
The program displays: <pre> The factorial of 6 is 720
</pre>
Here,we discuss program details:
A- We declare our recursive factorial function which takes an integer parameter and returns
the factorial of this parameter.
This function will call it self and decrease the number until the exiting or the base condition
is reached.When the condition is true,the previous generated values will be multiplied by
each other and the final factorial value is returned.
B- We declare and initialize an integer variable with value”6” and then print its factorial value
by calling our factorial function.
The recursive function process may be explained using the following chart to more understand
about recursive mechanism which concistes of calling the function its self until the base case or
stopping condition is reached and after that we collect the previous values:
Inline Functions
Functions are used to store the most frequently used
instructions. It is used for modularizing the program.
Whenever a function is called, the instruction pointer jumps
to the function definition. After executing a function,
instruction pointer falls back to the statement from where it
jumped to the function definition.
Whenever we use functions we require an extra pointer head
to jump to the function definition and return back to the
statement. To eliminate the need of such pointer heads, we
use inline functions.
In an inline function, a function call is directly replaced by an
actual program code. It does not jump to any block because
all the operations are performed inside the inline function.
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:
<pre>inline function_name ()
{
//function definition
}
</pre>
Let us write a program to implement an inline function.
<pre>
#include<stdio.h>
inline int add(int a, int b) //inline function
declaration
{
return(a+b);
}
int main()
{
int c=add(10,20);
printf("Addition:%d\n",c);
getch();
}
</pre>
Output:
<pre>Addition: 30
</pre>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 as the visibility of variables
within a program
There are global and local variables in C programming