0% found this document useful (0 votes)
3 views

Computer_Assignment_Solutions

The document provides an overview of functions in programming, including their definition, purpose, and the difference between function declaration and prototypes. It covers concepts such as pointers, recursion, arrays, and their initialization, as well as examples demonstrating how to use functions, pointers, and arrays in C. Additionally, it highlights the differences between recursion and iteration, and includes code snippets for various operations.

Uploaded by

kararpita814
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Computer_Assignment_Solutions

The document provides an overview of functions in programming, including their definition, purpose, and the difference between function declaration and prototypes. It covers concepts such as pointers, recursion, arrays, and their initialization, as well as examples demonstrating how to use functions, pointers, and arrays in C. Additionally, it highlights the differences between recursion and iteration, and includes code snippets for various operations.

Uploaded by

kararpita814
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Computer Assignment Solutions

1. What is function?

A function is a block of code that performs a specific task. It can take inputs (parameters), process

them, and return an output.

2. Why we use function?

Functions are used to improve modularity and reusability in programming. They allow breaking down

large code into smaller, manageable parts.

3. What is function declaration and prototype?

Function declaration informs the compiler about the function name, return type, and parameters

without defining the body.

Prototype refers to the same declaration, ensuring consistency in function calls and definitions.

4. Calling function value by reference with an example.

Passing by reference means passing the memory address of variables to a function, allowing the

function to modify the original variables.

Example:

```c

void swap(int *x, int *y) {

int temp = *x;

*x = *y;

*y = temp;

int main() {

int a = 5, b = 10;

swap(&a, &b);
printf("a = %d, b = %d", a, b);

return 0;

```

5. What is pointer?

A pointer is a variable that stores the memory address of another variable.

6. Print the address of integer variable.

```c

int a = 5;

printf("Address of a: %p", &a);

```

7. Write a function to swap the values of two variables.

```c

void swap(int *x, int *y) {

int temp = *x;

*x = *y;

*y = temp;

```

8. What do you mean by recursion?

Recursion is a programming technique where a function calls itself to solve a smaller instance of the

problem until a base condition is met.

9. What are the differences between recursion and iteration?

- Recursion is a function calling itself, while iteration involves loops like for or while.
- Recursion uses more memory (stack) compared to iteration.

- Iteration is generally faster and easier to debug.

10. What is Array?

An array is a collection of elements (of the same data type) stored at contiguous memory locations.

11. Array initialization and bound checking.

- Array initialization assigns values during declaration, e.g., `int arr[3] = {1, 2, 3};`.

- Bound checking ensures the array index is within the valid range. C/C++ does not perform

automatic bound checking.

12. Passing array element to a function.

Individual elements can be passed by value or reference.

```c

void printElement(int elem) {

printf("%d", elem);

int main() {

int arr[] = {10, 20};

printElement(arr[0]);

```

13. Pointer, Array with example.

- A pointer can be used to traverse or manipulate arrays.

Example:

```c

int arr[] = {10, 20, 30};

int *p = arr;
for (int i = 0; i < 3; i++) {

printf("%d ", *(p + i));

```

14. 2D Array to print (3x3) ordered matrix.

```c

int arr[3][3] = {

{1, 2, 3},

{4, 5, 6},

{7, 8, 9}

};

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

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

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

printf("\n");

```

You might also like