PROGRAMMING IN C
UNIT III
FUNCTIONS AND STORAGE
CLASSES
DEVENTHIRAN M |CSE
PROGRAMMING IN C | CS3251
Unit III
Topics
3.1 Functions and it’s types
3.2 Recursion
3.3 Storage classes
DEVENTHIRAN M |CSE
PROGRAMMING IN C | CS3251
CS3251 PROGRAMMING IN C LT PC 3 0 0 3
UNIT III
FUNCTIONS AND STORAGE CLASSES
Modular programming - Function prototype, function definition, function call, Built-in
functions (string functions, math functions) – Recursion, Binary Search using recursive
functions – Storage classes.
Course Outcomes
CO3: Apply and implement modular applications in C using functions K3
Course Objectives
To develop C programs using functions and storage class
Important Topics
1. Functions
2. Recursion
3. Storage classes
1. FUNCTIONS
A function in C is a self-contained block of code designed to perform a specific task.
Once defined, it can be called (invoked) from anywhere in your program. Functions enhance
modularity, code reuse, and readability
1. Components of a Function:
1. Return Type – The type of value returned (e.g., int, float, or void if no value).
2. Function Name – Identifier used to call the function.
3. Parameter List – Comma-separated input variables (optional).
4. Body – Code enclosed in { } that executes when called.
DEVENTHIRAN M |CSE
PROGRAMMING IN C | CS3251
5. Return Statement – Used if return type isn’t void
Syntax:
return_type function_name(param_type1 param1, param_type2 param2, ...) {
// Function body
return value; // if not void
}
2. Function Types:
Library (built-in) Functions: Provided by the C standard library (e.g., printf(),
scanf(), sqrt()), defined in headers like <stdio.h> and <math.h>
User-Defined Functions: Created by you to perform custom tasks, helping reduce
code duplication and improve structure.
3. User defined function:
Function prototype: A function prototype is simply the declaration of a function that
specifies function's name, parameters and return type. It doesn't contain function
body.
A function prototype gives information to the compiler that the function may later be
used in the program.
Syntax:
returnType functionName(type1 argument1, type2 argument2, ...);
4. Function calling:
Control of the program is transferred to the user-defined function by calling it.
Syntax :
functionName(argument1, argument2, …);
5. Function definition:
Function definition contains the block of code to perform a specific task. In our
example, adding two numbers and returning it.
Syntax:
returnType functionName(type1 argument1, type2 argument2, ...)
{
//body of the function
}
DEVENTHIRAN M |CSE
PROGRAMMING IN C | CS3251
Program:
#include <stdio.h>
int addNumbers(int a, int b); // function prototype
int main()
{
int n1,n2,sum;
printf("Enters two numbers: ");
scanf("%d %d",&n1,&n2);
sum = addNumbers(n1, n2); // function call
printf("sum = %d",sum)
return 0;
}
int addNumbers(int a, int b) // function definition
{
int result;
result = a+b;
return result; // return statement
}
Output:
Enter Two numbers: 2 6
Sum= 8
6. 2. RECURSION
1. What is Recurion?
Recursion is a programming technique where a function calls itself to solve smaller
instances of the same problem, typically until a base case is reached.
Syntax of a Recursive Function:
return_type function_name(parameters) {
if (base_case_condition) {
// Base case: stop recursion
DEVENTHIRAN M |CSE
PROGRAMMING IN C | CS3251
return base_case_value;
} else {
// Recursive case: reduce problem size
return function_name(modified_parameter);
}
}
Example Program: Sum of Natural Numbers:
#include <stdio.h>
int sum(int n) {
if (n == 0)
return 0; // Base case
else
return n + sum(n - 1); // Recursive call
DEVENTHIRAN M |CSE
PROGRAMMING IN C | CS3251
}
int main(void) {
int number = 5;
int result = sum(number);
printf("Sum of 0..%d = %d\n", number, result);
return 0;
}
Output:
Sum of 0..5 = 15
3. STORAGE CLASSES
1.What is Storage Classes?
In C, storage classes define the lifetime, scope, and visibility of variables. They specify
where a variable is stored, how long its value is retained, and how it can be accessed
which help us to trace the existence of a particular variable during the runtime of a
program.
C defines four main storage classes:
• auto
• register
• static
• extern
2. Auto :
• Default class for local variables (block scope).
• Lifetime: from entry until exit of the block.
• No initialization by default (holds garbage if not initialized).
• auto keyword is optional; rarely used explicitly.
Example:
void func() {
auto int x = 5; // same as: int x = 5;
printf("%d\n", x);
}
DEVENTHIRAN M |CSE
PROGRAMMING IN C | CS3251
3. register:
• Suggests storing the variable in a CPU register for fast access.
• Scope and lifetime are same as auto.
• Cannot use & operator (no memory address).
Example:
void loop() {
register int counter;
for (counter = 0; counter < 10; counter++)
printf("%d ", counter);
}
4. Static :
• When used inside a function:
1. Variable retains its value between function calls.
2. Lifetime spans the entire program execution.
• When used at global level:
1. Restricts linkage to current file (internal linkage).
2. Variable defaults to zero if not initialized.
Example:
// Global static
static int count = 0;
// Static inside function
void demo() {
static int calls = 0;
calls++;
printf("demo has been called %d times\n", calls);
}
5. Extern :
• Declares a variable defined in another file or later in the file.
• Provides external linkage (visible across multiple compilation units).
• Lifetime is for the entire program.
Example:
// file1.c
int shared = 42;
DEVENTHIRAN M |CSE
PROGRAMMING IN C | CS3251
// file2.c
extern int shared;
void print_shared() {
printf("%d\n", shared);
}
IMPORTANT PROGRAMS
1.Factorial of a Number Using Recursion:
#include<stdio.h>
long int multiplyNumbers(int n);
int main() {
int n;
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;
}
Output
Enter a positive integer: 6
Factorial of 6 = 720
DEVENTHIRAN M |CSE
PROGRAMMING IN C | CS3251
2.C Program to Display Fibonacci Sequence
#include <stdio.h>
int main() {
int i, n;
// initialize first and second terms
int t1 = 0, t2 = 1;
// initialize the next term (3rd term)
int nextTerm = t1 + t2;
// get no. of terms from user
printf("Enter the number of terms: ");
scanf("%d", &n);
// print the first two terms t1 and t2
printf("Fibonacci Series: %d, %d, ", t1, t2);
// print 3rd to nth terms
for (i = 3; i <= n; ++i) {
printf("%d, ", nextTerm);
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
}
return 0;
}
DEVENTHIRAN M |CSE
PROGRAMMING IN C | CS3251
Output
Enter the number of terms: 10
Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34
DEVENTHIRAN M |CSE
PROGRAMMING IN C | CS3251