Day_3 C-Programming
Day_3 C-Programming
Formal Parameters (Arguments): The parameters that hold the value of actual parameters in
function definition are known as formal parameters.
Types of functions: There are following types of functions.
(i). Function with arguments and with return value
(ii). Function with arguments and without return value
(iii).Function without argument and with return value
(iv).Function without argument and with return value
(i) Function with arguments and with return value
#include<stdio.h>
#include<conio.h>
int sum(int, int);
void main()
{
int a,b,s;
printf(“Eneter two Numbers”);
scanf(“%d%d”,&a,&b);
s=sum(a,b);
printf(“Addition=%d”,s);
getch();
}
int sum(int x, int y)
{
int z;
z=x+y;
return z;
}
(ii) Function with arguments and without return value
#include<stdio.h>
#include<conio.h>
void sum(int, int);
void main()
{
int a,b;
printf(“Eneter two Numbers”);
scanf(“%d%d”,&a,&b);
sum(a,b);
getch();
}
void sum(int x, int y)
{
int z;
z=x+y;
printf(“Addition=%d”,z);
}
#include<stdio.h>
#include<conio.h>
void swap(int *, int *);
void main()
{
int a=10,b=20;
swap(&a,&b);
printf(“The value of Actual Arguments: a=%d, b=%d”,a,b);
getch();
}
void swap(int *x, int *y)
{
int t;
t=*x;
*x=*y;
*y=t;
printf(“The value of Formal Arguments: x=%d, y=%d”,*x,*y);
}
Recursion
Definition
Recursion is a process where a function calls itself directly or indirectly to solve a larger problem by
breaking it into smaller sub-problems.
Types of Recursive Functions
1. Direct Recursion: A function directly calls itself.
Example:
#include <stdio.h>
void printNumbers(int n) {
if (n > 0) {
printf("%d\n", n);
printNumbers(n - 1); // Function calls itself
}
}
int main() {
printNumbers(5);
return 0;
}
2. Indirect Recursion: A function calls another function, which in turn calls the first function.
Example:
#include <stdio.h>
void functionA(int n) {
if (n > 0) {
printf("%d\n", n);
functionB(n - 1);
}
}
void functionB(int n) {
if (n > 0) {
printf("%d\n", n);
functionA(n - 1);
}
}
int main() {
functionA(5);
return 0;
}
int main() {
int n = 3; // Number of disks
towerOfHanoi(n, 'A', 'C', 'B'); // A, B, and C are the rod names
return 0;
}
Output for n = 3:
Move disk 1 from A to C
Move disk 2 from A to B
Move disk 1 from C to B
Move disk 3 from A to C
Move disk 1 from B to A
Move disk 2 from B to C
Move disk 1 from A to C
2. Scope of Variables
Local Variables
Variables declared inside a function are local to that function.
They can only be accessed within the function where they are defined.
Example:
#include <stdio.h>
void test() {
int localVar = 10;
printf("Local variable: %d\n", localVar);
}
int main() {
test();
// printf("%d", localVar); // Error: localVar is not accessible here
return 0;
}
Global Variables
Variables declared outside all functions are global.
They can be accessed by any function in the program.
Example:
#include <stdio.h>
void test() {
printf("Global variable: %d\n", globalVar);
}
int main() {
test();
printf("Global variable in main: %d\n", globalVar);
return 0;
}
Nesting of Scope
Inner blocks can access variables of outer blocks.
Inner variables with the same name as outer variables will shadow the outer variables.
Example:
#include <stdio.h>
int main() {
int x = 10;
{
int x = 20; // Shadows the outer x
printf("Inner block x: %d\n", x);
}
3. Storage Classes
Storage classes in C define the scope, lifetime, and visibility of variables.
In C, variables have three important properties:
1. Scope: Where the variable is accessible in the program.
2. Lifetime: How long the variable retains its value in memory.
3. Visibility: Which part of the program can see (use) the variable.
int main() {
test();
return 0;
}
2. Register
o Scope: Local to the block in which it is defined.
o Lifetime: Limited to the execution of the block where it is defined.
o Visibility: Not visible outside the block.
o Special Feature: Stored in CPU registers (if available) for faster access. Address-of
operator (&) cannot be used.
Example:
#include <stdio.h>
int main() {
register int x = 10;
printf("Register variable: %d\n", x);
return 0;
}
3. Static
o Retains its value between function calls.
o Scope: Local to the block in which it is defined.
o Lifetime: Retains its value between function calls (entire program execution).
o Visibility: Not visible outside the block (local static variable).
Example:
#include <stdio.h>
void test() {
static int count = 0;
count++;
printf("Static variable count: %d\n", count);
}
int main() {
test();
test();
test();
return 0;
}
Output:
Static variable count: 1
Static variable count: 2
Static variable count: 3
4. Extern
o Used to declare a global variable in another file.
o Scope: Global across all files where it is declared.
o Lifetime: Entire program.
o Visibility: Visible in all files that declare it using the extern keyword.
Example (Two files):
File1.c:
#include <stdio.h>
void display() {
printf("Extern variable: %d\n", globalVar);
}
File2.c:
#include <stdio.h>
void display();
int main() {
display();
return 0;
}
Practice Problems
1. Write a program to calculate the factorial of a number using recursion.
2. Implement the Tower of Hanoi problem for n disks.
3. Demonstrate the use of local and global variables in a program.
4. Write a program to demonstrate the difference between auto and static storage classes.
5. Implement a program to calculate the Fibonacci series using recursion.
6. Demonstrate the use of extern variables across two files.
7. Write a program to calculate the sum of digits of a number using recursion.
8. Demonstrate the use of register variables in a program.
9. Implement nested scopes and show variable shadowing.
10. Write a program to reverse a string using recursion.
11. Write a program to find out G.C.D. of two numbers using recursion.