Unit-5 Imp
Unit-5 Imp
Call by Value
The original value of an actual parameter is passed to the formal parameter.
A copy of the original value is created and passed to the function.
Changes made to the formal parameter do not affect the actual argument.
The original value remains unchanged after the function execution.
This technique does not allow the function to modify the original variable.
Example
#include<stdio.h>
void swap(int x, int y);
void main()
{
int a,b;
printf(“Enter a,b values: “);
scanf(“%d%d”,&a,&b);
swap(a, b);
printf("a = %d, b = %d\n", a, b);
}
void swap(int x, int y)
{
int temp;
temp = x;
x = y;
y = temp;
}
Call by Reference
The memory address of an actual parameter is passed to the formal parameter.
Changes made to the formal parameter directly affect the actual parameter.
The function can modify the original variable.
A copy of the original value is not created.
The changes made inside the function are reflected outside the function.
Example
#include <stdio.h>
void swap(int* x, int* y);
void main( )
{
int a, b;
printf("Enter a, b values: ");
scanf("%d %d", &a, &b);
swap(&a, &b);
printf("a = %d, b = %d\n", a, b);
}
void swap(int* x, int* y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}
Keyword auto
Example Program
#include<stdio.h>
int main()
{
int a=10;
{
int a=20;
printf("%d",a);
}
printf(" %d",a);
return 0;
}
2. External storage class
The default storage class of all global variables (variables declared outside function) is external
storage class. Variable of external storage class has the following properties...
Property Description
Keyword extern
Keyword static
Default Zero
Value
Scope Local to the block in which the variable is defined
Life time The value of the persists between different function calls
Example Program
#include <stdio.h>
void increment()
{
static int x = 0; // Static variable
x++;
printf("Value of x: %d\n", x);
}
void main()
{
increment(); // Output: Value of x: 1
increment(); // Output: Value of x: 2
increment(); // Output: Value of x: 3
}
4. Register storage class
The register storage class allocates variable memory in CPU Registers for faster accessibility.
Due to the limited number of CPU Registers, only a few variables can be declared with the
register storage class.
Variable of register storage class has the following properties...
Property Description
Keyword Register
Life time Till the control remains within the block in which variable is
defined
Example Program
#include<stdio.h>
int main()
{
register int a,b;
scanf("%d%d",&a,&b);
printf("%d %d",a,b);
return 0;
}
Q) What is recursion and explain about recursion with example program.
Recursive Functions
The process in which a function calls itself directly or indirectly is called recursion and the
corresponding function is called as recursive function.
Using recursion technique, certain mathematical problems can be solved quite easily.
In recursive function, both calling function and called function are represented at one place.
Syntax:
return_type function_name(parameters)
{
if (base_case_condition)
return value;
else
function_name(new_parameters);
}
Advantages:
Reduce unnecessary calling of function.
Through Recursion one can solve problems in easy way while its iterative solution is very
big and complex.
Program-1: /* Factorial Program using Recursion */
#include<stdio.h>
int factorial(int);
void main()
{
int n, fact;
printf("Enter any number:");
scanf("%d",&n);
fact =factorial(n);
printf("factorial of %d is: %d",n, fact);
}
int factorial(int n)
{
if(n==0)
return 1;
else if(n==1)
return 1;
else
return n*factorial(n-1);
}
#include<stdio.h>
int Fibonacci(int);
void main()
{
int n, i = 0, c;
scanf("%d",&n);
printf("Fibonacci series\n");
for ( c = 1 ; c <= n ; c++ )
{
printf("%d\n", fibonacci(i));
i++;
}
}
int fibonacci(int n)
{
if ( n == 0 )
return 0;
else if ( n == 1 )
return 1;
else
return ( fibonacci(n-1) + fibonacci(n-2) );
}