UNIT III
UNIT III
1. Define function.
A function is a block of code that performs a task and can be called from other parts
of the program.
2. Advantages of function.
A function definition includes the function's name, return type, parameters, and
body.
8. Define array.
1|Page
UNIT III
9. Define recursion.
Functions like strlen(), strcpy(), strcat(), strcmp() are used for string handling.
Recursion simplifies problems that divide into similar sub-problems and is useful
for algorithms like tree traversal.
LONG
13. What is the need for functions? Explain the elements of functions.
Elements of Functions:
o Ex:
o Ex:
return a + b;
2|Page
UNIT III
o Ex:
Example Program:
#include <stdio.h>
return a + b;
int main( ) {
return 0;
OUTPUT:
Sum: 15
Types of Functions in C:
1. Library Functions:
o Ex:
printf( )
scanf( )
strlen( )
3|Page
UNIT III
2. User-Defined Functions:
o Example:
void display() {
printf("Hello, World!");
void greet( ) {
printf("Hello!");
void print(int n) {
int getNumber( ) {
return 5;
return a + b;
4|Page
UNIT III
1. Pass by Value
Changes made to the parameter inside the function do not affect the original
value.
Example Program:
#include <stdio.h>
void increment(int a) {
a++;
int main( ) {
increment(num);
return 0;
Output:
5|Page
UNIT III
Changes made to the parameter inside the function affect the original value.
Example Program:
#include <stdio.h>
int main( ) {
return 0;
Output:
6|Page
UNIT III
Definition:
Recursion is a programming technique where a function calls itself to solve a
problem.
Advantages of Recursion:
2. Reduces the need for loops in certain algorithms (e.g., factorial, Fibonacci).
Example:
int factorial(int n) {
if (n == 0)
return 1;
17. Write a complete C program that reads a positive integer N & computes the
sum of N Fibonacci numbers using recursion.
#include <stdio.h>
int fibonacci(int n) {
if (n <= 1)
return n;
int fibonacciSum(int n) {
7|Page
UNIT III
if (n == 0)
return 0;
int main( ) {
int n;
scanf("%d", &n);
return 0;
18. What is an array? How is it different from an ordinary variable? What are its
advantages?
Definition:
An array is a collection of elements of the same data type stored at contiguous
memory locations.
1. A variable stores a single value, whereas an array can store multiple values of
the same type.
2. Arrays are accessed using indices, while variables are accessed by name.
Advantages of Arrays:
8|Page
UNIT III
Types of Arrays in C:
1. One-Dimensional Array:
A one-dimensional array stores elements in a single row or column.
Example Program:
#include <stdio.h>
int main( ) {
return 0;
Output:
12345
2. Two-Dimensional Array:
A two-dimensional array stores elements in a tabular format (rows and columns).
Example Program:
#include <stdio.h>
int main( ) {
9|Page
UNIT III
}
printf("\n");
return 0;
Output:
123
456
3. Multi-Dimensional Array:
A multi-dimensional array extends beyond two dimensions (e.g., a 3D array).
Example Program:
#include <stdio.h>
int main( ) {
int arr[2][2][2] = {{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}};
} }
return 0;
Output:
arr[0][0][0] = 1
10 | P a g e
UNIT III
arr[0][0][1] = 2
arr[0][1][0] = 3
arr[0][1][1] = 4
arr[1][0][0] = 5
arr[1][0][1] = 6
arr[1][1][0] = 7
arr[1][1][1] = 8
20. What are the commonly used string handling functions in C? Explain.
char dest[20];
strcpy(dest, "Hello");
if (strcmp("abc", "abc") == 0) {
printf("Equal");
11 | P a g e
UNIT III
7. strstr( ): Finds the first occurrence of a substring in a string.
12 | P a g e