C Language | Function | Recursion | Start your journey with C | Part 7

Function in C :

Function is type of statement perform certain type of task in program , main() predefined function is the most important function in c program. 
A Function can be predefined or user defined.
All function predefined functions are declared in library file.
A function can return only on value at a time with return function.

A function have four(4) types of argument :

These are user defined function.
  • Function with no argument and no return value:                        

  Syntax :        

void fun(void);
#include<stdio.h>
main()
{
 .............
 .............
 fun();
}
void fun(void)
{
...........
...........
}

Sample :

Add two number
#include<stdio.h>
void fun(void); //global 
void main()
{
    printf("Enter two number to add :\n");
    fun();
}
void fun(void)
{
    int c,x,y;
    scanf("%d%d",&x,&y);
    c=x+y;
    printf("%d\n",c);
}
  • Function with no argument but return value: 

    Syntax :
data type fun(void)
#include<stdio.h>
main()
{
 .............
 .............
 fun();
}
 int fun(void)
{
 ...........
 return(variable);
} 

sample :

#include<stdio.h>
int fun(void); //global
void main()
{
 int s;
 printf("Enter two number to add :\n");
 s=fun();
 printf("%d\n",s);
}
int fun(void)
{
 int c,x,y;
 scanf("%d%d",&x,&y);
 c=x+y;
 return(c);
}
  • Function with argument but no return value:



void fun( variable );
#include<stdio.h>
main()
{
 .............
 .............
 fun(variable);
}
void fun(variable)
{
...........
...........
}
Sample :


 Add two number
#include<stdio.h>
void fun(int , int); //global
void main()
{
 int a,b;
 printf("Enter two number to add :\n");
 scanf("%d%d",&a,&b);
 fun(a,b);
}
void fun(int x ,int y)
{
 int c;
 c=x+y;
 printf("%d\n",c);
}
  • Function with argument and return value:

syntax :
data type fun(data type)
#include<stdio.h>
main()
{
 .............
 .............
 fun();
}
 data type fun(data type)
{
 ...........
 return(variable);
}  
  sample:
#include<stdio.h>
int fun(int , int); //global
void main()
{
 int a,b,s;
 printf("Enter two number to add :\n");
 scanf("%d%d",&a,&b);
 s=fun(a,b); printf("%d\n",s);
}
int fun(int x ,int y)
{
 int c;
 c=x+y;
 return (c);
} 

Recursion:

Recursion is a programming technique that allows the programmer to express operations in terms of themselves. In C, this takes the form of a function that calls itself. A useful way to think of recursive functions is to imagine them as a process being performed where one of the instructions is to "repeat the process". This makes it sound very similar to a loop because it repeats the same code, and in some ways it is similar to looping. On the other hand, recursion makes it easier to express ideas in which the result of the recursive call is necessary to complete the task. Of course, it must be possible for the "process" to sometimes be completed without the recursive call. One simple example is the idea of building a wall that is ten feet high; if I want to build a ten foot high wall, then I will first build a 9 foot high wall, and then add an extra foot of bricks. Conceptually, this is like saying the "build wall" function takes a height and if that height is greater than one, first calls itself to build a lower wall, and then adds one a foot of bricks.

Format :

void recursion() {
   recursion(); /* function calls itself */
}

int main() {
   recursion();
}

Recursive functions are very useful to solve many mathematical problems, such as calculating the factorial of a number, generating Fibonacci series, etc.
The following example calculates the factorial of a given number using a recursive function −
#include <stdio.h>

unsigned long long int factorial(unsigned int i) {

   if(i <= 1) {
      return 1;
   }
   return i * factorial(i - 1);
}

int  main() {
   int i = 12;
   printf("Factorial of %d is %d\n", i, factorial(i));
   return 0;
}
When the above code is compiled and executed, it produces the following result −
Factorial of 4 is 24

Comments

Popular posts from this blog

TOP 10 PROGRAMMING LANGAUAGES COMMING 2021 | ARTICLE

Write a C-program to swap two number:

Print Fibonacci Series using Recursion | C program :