0% found this document useful (0 votes)
13 views12 pages

UNIT III

Uploaded by

Kore Ramesh
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)
13 views12 pages

UNIT III

Uploaded by

Kore Ramesh
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/ 12

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.

Functions enable code reuse, modularity, and easier debugging.

3. Write about function definition.

A function definition includes the function's name, return type, parameters, and
body.

4. Define parameter passing.

Parameter passing provides inputs to a function when called.

5. What are formal parameters.

Formal parameters are variables defined in a function to receive passed values.

6. What are the actual arguments.

Actual arguments are values passed to the function when called.

7. Define character array.

A character array is a sequence of characters stored in contiguous memory.

8. Define array.

An array is a collection of elements of the same data type stored in contiguous


memory.

1|Page
UNIT III
9. Define recursion.

Recursion is when a function calls itself.

10. List common string handling functions.

Functions like strlen(), strcpy(), strcat(), strcmp() are used for string handling.

11. List different types of functions in C.

C functions include library and user-defined functions.

12. Advantages of recursion.

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.

Need for Functions:

Functions promote code reusability, modular programming, and easier debugging.

Elements of Functions:

1. Function Declaration: Specifies the function name, return type, and


parameters.

o Ex:

int add(int, int);

2. Function Definition: Contains the actual code of the function.

o Ex:

int add(int a, int b) {

return a + b;

2|Page
UNIT III

3. Function Call: Executes the function in the main program.

o Ex:

result = add(5, 10);

Example Program:

#include <stdio.h>

int add(int a, int b) {

return a + b;

int main( ) {

int result = add(5, 10);

printf("Sum: %d\n", result);

return 0;

OUTPUT:

Sum: 15

14. Explain the different types of functions.

Types of Functions in C:

1. Library Functions:

o Predefined functions provided by the C library.

o Ex:

printf( )

scanf( )

strlen( )

3|Page
UNIT III

2. User-Defined Functions:

o Functions written by the programmer to perform specific tasks.

o Example:

void display() {

printf("Hello, World!");

3. Based on Arguments and Return Values:

o Function with No Arguments and No Return Value:

void greet( ) {

printf("Hello!");

o Function with Arguments and No Return Value:

void print(int n) {

printf("Number: %d", n);

o Function with No Arguments and a Return Value:

int getNumber( ) {

return 5;

o Function with Arguments and a Return Value:

int add(int a, int b) {

return a + b;

4|Page
UNIT III

15. Explain how to pass parameters to functions.

Parameter Passing Methods in C:

1. Pass by Value

 A copy of the actual parameter is passed to the function.

 Changes made to the parameter inside the function do not affect the original
value.

Example Program:

#include <stdio.h>

void increment(int a) {

a++;

printf("Inside Function (Pass by Value): a = %d\n", a);

int main( ) {

int num = 10;

printf("Before Function Call: num = %d\n", num);

increment(num);

printf("After Function Call: num = %d\n", num);

return 0;

Output:

Before Function Call: num = 10

Inside Function (Pass by Value): a = 11

After Function Call: num = 10

5|Page
UNIT III

2. Pass by Reference (Using Pointers)

 The address of the actual parameter is passed to the function.

 Changes made to the parameter inside the function affect the original value.

Example Program:

#include <stdio.h>

void increment(int *a) {

(*a)++; // Modifies the original value using its address

printf("Inside Function (Pass by Reference): *a = %d\n", *a);

int main( ) {

int num = 10;

printf("Before Function Call: num = %d\n", num);

increment(&num); // Pass the address of num

printf("After Function Call: num = %d\n", num);

return 0;

Output:

Before Function Call: num = 10

Inside Function (Pass by Reference): *a = 11

After Function Call: num = 11

6|Page
UNIT III

16. What is recursion? What are its advantages?

Definition:
Recursion is a programming technique where a function calls itself to solve a
problem.

Advantages of Recursion:

1. Simplifies complex problems by breaking them into smaller, similar problems.

2. Reduces the need for loops in certain algorithms (e.g., factorial, Fibonacci).

3. Useful in solving problems like tree traversal, backtracking, and divide-and-


conquer.

Example:

int factorial(int n) {

if (n == 0)

return 1;

return n * factorial(n - 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>

// Function to compute the nth Fibonacci number

int fibonacci(int n) {

if (n <= 1)

return n;

return fibonacci(n - 1) + fibonacci(n - 2);

// Function to compute the sum of the first N Fibonacci numbers

int fibonacciSum(int n) {

7|Page
UNIT III
if (n == 0)

return 0;

return fibonacci(n) + fibonacciSum(n - 1);

int main( ) {

int n;

printf("Enter a positive integer: ");

scanf("%d", &n);

printf("Sum of first %d Fibonacci numbers: %d\n", n, fibonacciSum(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.

Difference from Ordinary Variable:

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:

1. Efficient Storage: Group multiple elements under one name.

2. Easy Access: Use indexing to retrieve or modify elements.

3. Iterative Processing: Suitable for looping structures.

4. Static Memory Allocation: Avoids frequent memory allocation.

8|Page
UNIT III

19. Explain different types of arrays.

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( ) {

int arr[5] = {1, 2, 3, 4, 5};

printf("One-Dimensional Array Elements:\n");

for (int i = 0; i < 5; i++) {

printf("%d ", arr[i]);

return 0;

Output:

One-Dimensional Array Elements:

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( ) {

int mat[2][3] = {{1, 2, 3}, {4, 5, 6}};

printf("Two-Dimensional Array Elements:\n");

for (int i = 0; i < 2; i++) {

for (int j = 0; j < 3; j++) {

printf("%d ", mat[i][j]);

9|Page
UNIT III
}

printf("\n");

return 0;

Output:

Two-Dimensional Array Elements:

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

printf("Multi-Dimensional Array Elements:\n");

for (int i = 0; i < 2; i++) {

for (int j = 0; j < 2; j++) {

for (int k = 0; k < 2; k++) {

printf("arr[%d][%d][%d] = %d\n", i, j, k, arr[i][j][k]);

} }

return 0;

Output:

Multi-Dimensional Array Elements:

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.

Commonly Used String Handling Functions in C:

1. strlen( ): Returns the length of a string.

int len = strlen("Hello");

2. strcpy( ): Copies one string to another.

char dest[20];

strcpy(dest, "Hello");

3. strcat( ): Concatenates two strings.

strcat(dest, " World");

4. strcmp( ): Compares two strings.

if (strcmp("abc", "abc") == 0) {

printf("Equal");

5. strrev( ) (Non-standard): Reverses a string (may require custom


implementation).

char str[10] = "Hello";

strrev(str); // Output: "olleH"

6. strchr( ): Finds the first occurrence of a character in a string.

char *ptr = strchr("Hello", 'e');

11 | P a g e
UNIT III
7. strstr( ): Finds the first occurrence of a substring in a string.

char *ptr = strstr("Hello World", "World");

12 | P a g e

You might also like