C Programming Functions and Recursion
C Programming Functions and Recursion
C Programming Functions and Recursion
A C program has at least one function main( ) . Without main() function, there is
technically no C program.
Types of C functions
There are two types of functions in C programming:
Library function
User defined function
Library function
main()
printf()
scanf()
Visit this page to learn more about library functions in C programming language.
User defined function
#include <stdio.h>
void function_name(){
................
................
int main(){
...........
...........
function_name();
...........
...........
As mentioned earlier, every C program begins from main() and program starts
executing the codes inside main() function. When the control of program reaches
to function_name() inside main() function. The control of program jumps to void
function_name() and executes the codes inside it. When all the codes inside that
user-defined function are executed, control of the program jumps to the
statement just after function_name() from where it is called. Analyze the figure
below for understanding the concept of function in C programming. Visit this
page to learn in detail about user-defined functions .
In the above example, int add(int a, int b); is a function prototype which provides
following information to the compiler:
function_name(argument(1),....argument(n));
Function definition
Function definition contains programming codes to perform specific task.
//body of function
1. Function declarator
Syntax of function declaration and declarator are almost same except, there is
no semicolon at the end of declarator and function declarator is followed by
function body.
2. Function body
In above example two variable, num1 and num2 are passed to function during
function call and these arguments are accepted by arguments a and b in
function definition.
Arguments that are passed in function call and arguments that are accepted in
function definition should have same data type. For example:
If argument num1 was of int type and num2 was of float type then, argument
variable a should be of type int and b should be of type float,i.e., type of
argument during function call and function definition should be same.
Return Statement
Return statement is used for returning a value from function definition to calling
function.
return (expression);
For example:
return a;
return (a+b);
In above example, value of variable add in add() function is returned and that
value is stored in variable sum in main() function. The data type of expression in
return statement should also match the return type of function.
Let's take an example to find whether a number is prime or not using above 4
categories of user defined functions.
Function prime() is used for asking user a input, check for whether it is prime of
not and display it accordingly. No argument is passed and returned
form prime() function.
Here, check_display() function is used for check whether it is prime or not and
display it accordingly. Here, argument is passed to user-defined function but,
value is not returned from it to calling function.
Here, check() function is used for checking whether a number is prime or not. In
this program, input from user is passed to function check() and integer value is
returned from it. If input the number is prime, 0 is returned and if number is not
prime, 1 is returned.
C Programming Recursion
A function that calls itself is known as recursive function and this technique is
known as recursion in C programming.
Example of recursion in C programming
#include <stdio.h>
int sum(int n);
int main(){
int num,add;
printf("Enter a positive integer:\n");
scanf("%d",&num);
add=sum(num);
printf("sum=%d",add);
}
int sum(int n){
if(n==0)
return n;
else
return n+sum(n-1); /*self call to function sum() */
}
Output
15
In, this simple C program, sum() function is invoked from the same function.
If n is not equal to 0 then, the function calls itself passing argument 1 less than
the previous argument it was called with. Suppose, n is 5 initially. Then, during
next function calls, 4 is passed to function and the value of argument decreases
by 1 in each recursive call. When, n becomes equal to 0, the value of n is
returned which is the sum numbers from 5 to 1.
sum(5)
=5+sum(4)
=5+4+sum(3)
=5+4+3+sum(2)
=5+4+3+2+sum(1)
=5+4+3+2+1+sum(0)
=5+4+3+2+1+0
=5+4+3+2+1
=5+4+3+3
=5+4+6
=5+10
=15
Every recursive function must be provided with a way to end the recursion. In
this example when, n is equal to 0, there is no recursive call and recursion ends.
Recursion is more elegant and requires few variables which make program
clean. Recursion can be used to replace complex nesting code by dividing the
problem into same problem of its sub-type.
Every variable in C programming has two properties: type and storage class.
Type refers to the data type of variable whether it is character or integer or
floating-point value etc. And storage class determines how long it stays in
existence.
There are 4 types of storage class:
1. automatic
2. external
3. static
4. register
auto
Variables declared inside the function body are automatic by default. These
variable are also known as local variables as they are local to the function and
doesn't have meaning outside that function
Since, variable inside a function is automatic by default, keyword auto are rarely
used.
In case of large program, containing more than one file, if the global variable is
declared in file 1 and that variable is used in file 2 then, compiler will show error.
To solve this problem, keyword extern is used in file 2 to indicate that, the
variable specified is global variable and declared in another file.
void Check(){
++a;
/* ----- Variable a is not declared in this function but, works in any function as
they are global variable ------- */
printf("a=%d\n",a);
}
Output
a=10
register
register int a;
Register variables are similar to automatic variable and exists inside that
particular function only.
In case of larger program, variables that are used in loops and function
parameters are declared register variables.
Since, there are limited number of register in processor and if it couldn't store
the variable in register, it will automatically store it in memory.
Static Storage Class
The value of static variable persists until the end of the program. A variable can
be declared static using keyword: static . For example:
static int i;
Output
0 5 10
During first function call, it will display 0. Then, during second function call,
variable c will not be initialized to 0 again, as it is static variable. So, 5 is
displayed in second function call and 10 in third call.
If variable c had been automatic variable, the output would have been:
0 0 0
http://www.programiz.com/c-programming/c-functions-examples