0% found this document useful (0 votes)
11 views6 pages

Unit-5 Imp

The document explains parameter passing techniques in C, which include Call by Value and Call by Reference, detailing how data is passed between functions. It also covers storage classes in C, including Automatic, External, Static, and Register classes, describing their properties and examples. Additionally, the document discusses recursion, providing definitions and examples of recursive functions for calculating factorials and generating Fibonacci series.

Uploaded by

praveen chodi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views6 pages

Unit-5 Imp

The document explains parameter passing techniques in C, which include Call by Value and Call by Reference, detailing how data is passed between functions. It also covers storage classes in C, including Automatic, External, Static, and Register classes, describing their properties and examples. Additionally, the document discusses recursion, providing definitions and examples of recursive functions for calculating factorials and generating Fibonacci series.

Uploaded by

praveen chodi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Q) Explain about parameter passing techniques in C.

Parameter Passing Techniques


 Parameter passing techniques pass data from a calling function to a called function.
 These techniques enable data exchange between functions.
 They allow functions to communicate and perform tasks based on the passed data.
 There are two primary techniques: Call by Value and Call by Reference.

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;
}

Q) Explain about storage classes in C?


Storage Classes
 Storage classes in C define the storage location, scope, lifetime, and default value of
variables.
 The allocation of memory to variables in C is done in either RAM or CPU Registers through
storage classes.
 Storage classes determine the properties and behavior of variables in a C program.
 There are four types of storage classes in C:
1. Automatic storage class
2. External storage class
3. Static storage class
4. Register storage class

1. Automatic storage class


The default storage class of all local variables (variables declared inside block or function) is auto
storage class. Variable of auto storage class has the following properties...
Property Description

Keyword auto

Storage Main Memory

Default Garbage Value


Value
Scope Local to the block in which the variable is defined

Life time Till the control remains within the block

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

Storage Main Memory

Default Value Zero

Scope Global to the program (i.e., Throughout the program)

Life time Till end of the program


Example Program
#include<stdio.h>
extern int x;
void display( )
{
printf("Value of x: %d\n",x);
}
int main()
{
display();
return 0;
}

In another file (e.g., ext_var.c):


int x=10;

3. Static storage class


The static storage class is used to create variables that hold value beyond its scope until the end of
the program. The static variable allows initializing only once and can be modified any number of
times. Variable of static storage class has the following properties...
Property Description

Keyword static

Storage Main Memory

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

Storage CPU Register

Default Value Garbage Value

Scope Local to the block in which the variable is defined

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);
}

Program-2: /*Fibonacci Series using Recursion*/

#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) );
}

You might also like