Lecture 4 - Functions and Procedures Flashcards
What is a function in C?
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; }
What is the difference between a function and a procedure in C?
A function returns a value, while a procedure does not return anything (its return type is void
).
void printMessage() { printf("Hello, World!\n"); }
What is a function prototype in C, and why is it needed?
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);
What is pass-by-value in C?
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 }
What is pass-by-reference in C, and how is it done?
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 }
What is the purpose of the const
keyword in C?
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 do you define a constant pointer in C?
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;
~~~
What is the execution stack, and how does it relate to function calls?
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 does C handle recursion?
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.
What is the difference between a definition and a declaration in C?
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; }
What is a multi-file program in C, and how are functions handled across files?
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 do you pass arrays to functions in C?
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);
What is a function pointer in C?
A function pointer stores the address of a function and can be used to call the function.
int (*funcPtr)(int, int) = &add;
How do you pass a function as a parameter in C?
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)); }
What is a void pointer, and when would you use it?
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;