Recursion
Recursion
WHAT IS RECRUSION
A function that calls itself is known as a recursive function. And, this technique is
known as recursion.
SUM OF NATURAL NUMBERS USING
RECURSION
#include <stdio.h> int sum(int n) {
int sum(int n); if (n != 0)
int main() { // sum() function calls itself
int number, result; return n + sum(n-1);
printf("Enter a positive integer: "); // sum() function calls itself
scanf("%d", &number); return n + sum(n-1); return n + sum(n-1);
result = sum(number); else
printf("sum = %d", result); return n;
return 0; }
} output :
Enter a positive integer 3
sum = 6
FIND FACTORIAL OF A NUMBER USING
RECURSION
#include<stdio.h>
long int multiplyNumbers(int n); Enter a positive integer:
int main() { 6
int n; Factorial of 6 = 720
printf("Enter a positive integer: ");
scanf("%d",&n);
printf("Factorial of %d = %ld", n, multiplyNumbers(n));
return 0;
}
long int multiplyNumbers(int n) {
if (n>=1)
return n*multiplyNumbers(n-1);
else
return 1;
}
ADD TWO NUMBERS USING FUNCTION IN C
PROGRAMMING
#include<stdio.h>
int main() {
int num1, num2, res; Enter the two numbers : 12 15
Addition of two number is : 27
printf("\nEnter the two numbers : ");
scanf("%d %d", &num1, &num2);
//Call Function Sum With Two Parameters
res = sum(num1, num2);
printf("nAddition of two number is : ");
return (0);
}
int sum(int num1, int num2) {
int num3;
num3 = num1 + num2;
return (num3);
}
WHAT IS A VARIABLE?
Each variable is defined and can be used within its scope and determines that wherein the
program this variable is available to use. The scope means the lifetime of that variable. It
means the variable can only be accessed or visible within its scope.
The scope of variables can be defined with their declaration, and variables are declared
mainly in two ways:
Global variables are those variables which are declared outside of all the functions or block
and can be accessed globally in a program.
It can be accessed by any function present in the program.
Once we declare a global variable, its value can be varied as used with different functions.
The lifetime of the global variable exists till the program executes. These variables are
stored in fixed memory locations given by the compiler and do not automatically clean up.
Global variables are mostly used in programming and useful for cases where all the
functions need to access the same data.
Example:
#include<stdio.h>
int a=50, b=40;
void main()
{
printf("a = %d and b=%d",a,b);
}
Variables that are declared within or inside a function block are known as Local variables.
These variables can only be accessed within the function in which they are declared.
The lifetime of the local variable is within its function only, which means the variable exists till the
function executes. Once function execution is completed, local variables are destroyed and no longer
exist outside the function.
The reason for the limited scope of local variables is that local variables are stored in the stack, which
is dynamic in nature and automatically cleans up the data stored within it.
But by making the variable static with "static" keyword, we can retain the value of local variable.
Example:
{
int x=50, y=40;
printf("x = %d and y=%d",x, y);
}
COMPARISON CHART BETWEEN GLOBAL
VARIABLE AND LOCAL VARIABLE
Global Variable Local Variable
Global variables are declared outside all the function blocks. Local Variables are declared within a function block.
The scope remains throughout the program. The scope is limited and remains within the function only in
which they are declared.
Any change in global variable affects the whole program, Any change in the local variable does not affect other functions
wherever it is being used. of the program.
A global variable exists in the program for the entire time the A local variable is created when the function is executed, and
program is executed. once the execution is finished, the variable is destroyed.
It can be accessed throughout the program by all the functions It can only be accessed by the function statements in which it is
present in the program. declared and not by the other functions.
If the global variable is not initialized, it takes zero by default. If the local variable is not initialized, it takes the garbage value by
default.
Global variables are stored in the data segment of memory. Local variables are stored in a stack in memory.
We cannot declare many variables with the same name. We can declare various variables with the same name but in
other functions.
#include<stdio.h>
// Global variables
int a;
int b;
int Add()
{
return a + b;
}
int Mul()
{
int c=10; //Local Variable
int d=20; ////Local Variable
return c*d;
}
void main()
{
int Ans1, Ans2, c=30;// Local variable
a = 50;
b = 70;
Ans1 = Add();
Ans2= Mul();
A static variable possesses the property of preserving its actual value even after it is out of its scope.
Thus, the static variables are able to preserve their previous value according to their previous scope,
and onUse of the Static Variable keyworde doesn’t need to initialize them again in the case of a new
scope.
USE OF THE STATIC VARIABLE KEYWORD
static Local Variable – When we declare a local variable with a static keyword, it is called a static
local variable. The static local variable’s memory stays valid throughout any program. But the scope
of the visibility of these variables is similar to that of the automatic local variables. But when the
static local variable gets modified by the function during the first function call, the modified value
here will also be available for the next function.
Static Global Variable – When we declare a global variable with a static keyword, then it is called a
static global variable. This variable gets declared at the top of any program. Added to this, its visibility
stays throughout any program.
Static Member Variables – When we declare the member variables with a static keyword in a class,
we call them static member variables. All the instances present in a class can easily access this type of
variables, and we don’t require a specific instance to do that.
EXAMPLE OF STATIC VARIABLE IN C
#include <stdio.h>
int main() { Output
The output generated here would be:
auto int x = -30; Value of the auto variable is : -30
static int y = 16; Value of the static variable y is : 16
Sum of the auto variable and static variable is : -18
printf(“Value of the given auto variable is : %d\n”, x);
printf(“Value of the given static variable y is : %d\n”,y);
if(x!=0)
printf(“Sum of the auto variable and static variable is : %d\n”,(x+y));
return 0;
}
WHAT IS MACRO AND PRE PROCESSOR
The C preprocessor is a macro preprocessor (allows you to define macros) that transforms your
program before it is compiled. These transformations can be the inclusion of header files, macro
expansions, etc.
All preprocessing directives begin with a # symbol. For example,
#define PI 3.14
#include <stdio.h> : The #include preprocessor is used to include header files to C programs.
You can also create your own header file containing function declaration and include it in your
program using this preprocessor directive.
#include "my_header.h"