Lecture 4 - Functions and Procedures Flashcards

1
Q

What is a function in C?

A

A function is a reusable block of code that performs a specific task, takes parameters, and may return a value.

int add(int a, int b) {
    return a + b;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the difference between a function and a procedure in C?

A

A function returns a value, while a procedure does not return anything (its return type is void).

void printMessage() {
    printf("Hello, World!\n");
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a function prototype in C, and why is it needed?

A

A function prototype declares the function signature before its definition, specifying the return type, function name, and parameters. It is necessary for functions that are defined after they are called.

int add(int, int);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is pass-by-value in C?

A

In pass-by-value, a copy of the argument is passed to the function. Changes made to the parameter inside the function do not affect the original variable.

void foo(int x) {
    x = 10;  // does not affect the original value
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is pass-by-reference in C, and how is it done?

A

Pass-by-reference means passing the address of a variable to a function so the function can modify the original value. This is done using pointers.

void foo(int *x) {
    *x = 10;  // modifies the original variable
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the purpose of the const keyword in C?

A

const is used to declare a variable or a pointer as constant, meaning its value cannot be changed after initialization.

const int x = 10;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you define a constant pointer in C?

A

Pointer to a constant value: The pointer can change, but the value it points to cannot.

const int *ptr;

Constant pointer: The pointer cannot change, but the value it points to can.
~~~
int *const ptr;
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the execution stack, and how does it relate to function calls?

A

The execution stack stores information about active functions, including local variables and parameters. Each function call creates a new stack frame, which is destroyed when the function exits.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How does C handle recursion?

A

C supports recursion, where a function calls itself. Each recursive call creates a new stack frame. Without a proper base case, recursion can lead to stack overflow.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the difference between a definition and a declaration in C?

A

Declaration: Introduces a function or variable, specifying its type.

int add(int, int);  // declaration

Definition: Provides the actual implementation of the function or variable.

int add(int a, int b) {  // definition
    return a + b;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is a multi-file program in C, and how are functions handled across files?

A

A multi-file program allows different functions to be defined in separate .c files. Function declarations are placed in .h header files, which are included wherever the function is used.

// In file1.h
int add(int, int);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you pass arrays to functions in C?

A

Arrays are passed by reference (as pointers), meaning changes to the array inside the function will affect the original array.

void printArray(int arr[], int size);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is a function pointer in C?

A

A function pointer stores the address of a function and can be used to call the function.

int (*funcPtr)(int, int) = &add;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How do you pass a function as a parameter in C?

A

You can pass a function as a parameter by passing its pointer.

void execute(int (*func)(int, int), int a, int b) {
    printf("%d\n", func(a, b));
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is a void pointer, and when would you use it?

A

A void pointer is a generic pointer that can point to any data type. It is often used in functions that deal with different types of data.

void *ptr;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the difference between local and global variables in C?

A

Local variables: Declared inside functions and accessible only within that function.
Global variables: Declared outside all functions and accessible from any function in the same program.

17
Q

How does C implement default function arguments?

A

C does not support default function arguments natively. However, you can simulate them using function overloading or by providing arguments manually within the function.

18
Q

How can you avoid multiple inclusion of a header file in C?

A

Use include guards to prevent multiple inclusions.

#ifndef HEADER_FILE_NAME_H
#define HEADER_FILE_NAME_H
// content of header file
#endif
19
Q

What is the difference between inline and normal functions in C?

A

An inline function suggests to the compiler that the function’s code should be inserted directly into the calling code to reduce the overhead of a function call.

20
Q

What is a recursive function?

A

A recursive function is a function that calls itself, typically used for problems that can be broken down into similar sub-problems (e.g., factorial, Fibonacci sequence).

int factorial(int n) {
    if (n == 0) return 1;
    return n * factorial(n - 1);
}