PASSING PACKAGE — MODULE 4
Programming in C (1BEIT205) — VTU 2025 Scheme
City Engineering College, Bengaluru | Dept. of AIML/ISE
(Original passing package had no written solutions for this module — answers below are newly prepared)
Q18. Explain Function with the syntax of function definition and declaration, with a
simple example.
A function is a self-contained block of code that performs a specific task. It is executed only when
called, and helps in breaking a large program into smaller, reusable parts.
Function Declaration (Prototype) – Tells the compiler about the function's name, return type, and
parameters before it is used.
return_type function_name(parameter_list);
int add(int, int);
Function Definition – Contains the actual body/code of the function.
int add(int a, int b)
{
int sum;
sum = a + b;
return sum;
}
Complete Example:
#include <stdio.h>
int add(int a, int b); // declaration
int main() {
int result;
result = add(5, 3); // function call
printf("Sum = %d", result);
return 0;
}
int add(int a, int b) { // definition
return a + b;
}
Final Answer: A function declaration specifies the function's signature for the compiler, while the
function definition contains the actual logic; both together allow code reuse and modularity.
Q19. Explain recursion and discuss a program to find the factorial of a given number
using recursion.
Recursion is a technique where a function calls itself, directly or indirectly, to solve a problem by
breaking it into smaller sub-problems of the same type. Every recursive function must have a base
condition to stop the recursive calls, otherwise it leads to infinite recursion (stack overflow).
Program: Factorial using Recursion
#include <stdio.h>
int factorial(int n) {
if (n == 0 || n == 1) // base condition
return 1;
else
return n * factorial(n - 1); // recursive call
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("Factorial = %d", factorial(num));
return 0;
}
Sample Input: 4 → Output: Factorial = 24
Working: factorial(4) = 4 × factorial(3) = 4 × 3 × factorial(2) = ... = 4×3×2×1 = 24. Each call waits for the
next, until the base case (n=1) is reached, then results multiply back up.
Final Answer: Recursion solves a problem by calling the function within itself until a base condition is
met; factorial(n) = n × factorial(n-1), with factorial(1) = 1 as the base case.
Q20. Define dynamic memory allocation. List and explain the functions used to handle it
in C.
Dynamic memory allocation is the process of allocating memory to variables during program
execution (at run time), rather than at compile time. It is done using pointers and functions from the
stdlib.h header, allowing memory size to grow or shrink as needed.
Function Purpose
malloc() Allocates a block of memory of given size (uninitialized).
calloc() Allocates multiple blocks of memory, all initialized to zero.
realloc() Changes (increases/decreases) the size of previously allocated memory.
free() Releases/deallocates previously allocated memory.
Example:
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr;
ptr = (int*) malloc(5 * sizeof(int)); // allocate memory for 5 ints
if (ptr == NULL) {
printf("Memory not allocated");
} else {
ptr[0] = 10;
printf("Value = %d", ptr[0]);
free(ptr); // release memory
}
return 0;
}
Final Answer: Dynamic memory allocation reserves memory at run time using malloc(), calloc(),
realloc(), and free() from stdlib.h, giving flexible control over memory usage.
Q21. Explain TWO techniques of parameter passing to functions with suitable program
segments (Argc, Argv).
Parameters can be passed to functions in two main ways:
• Call by Value – A copy of the actual argument's value is passed to the function. Changes made
inside the function do not affect the original variable.
• Call by Reference – The address of the actual argument is passed (using a pointer). Changes
made inside the function directly affect the original variable.
Call by Value Example:
void change(int x) {
x = x + 10; // only local copy changes
}
int main() {
int a = 5;
change(a);
printf("%d", a); // Output: 5 (unchanged)
}
Call by Reference Example:
void change(int *x) {
*x = *x + 10; // modifies original value
}
int main() {
int a = 5;
change(&a);
printf("%d", a); // Output: 15 (changed)
}
Command-line arguments (argc, argv) are a special way of passing parameters to the main() function
itself, from outside the program at the time of execution.
• argc (argument count) – Stores the number of command-line arguments passed, including the
program name.
• argv (argument vector) – An array of strings storing the actual arguments; argv[0] is the program
name.
#include <stdio.h>
int main(int argc, char *argv[]) {
printf("Number of arguments = %d\n", argc);
for (int i = 0; i < argc; i++)
printf("Argument %d: %s\n", i, argv[i]);
return 0;
}
Final Answer: The two parameter-passing techniques are Call by Value (passes a copy) and Call by
Reference (passes the address); argc and argv allow command-line arguments to be passed directly
into main().
Q22. List the advantages of functions. With a suitable program, show how a pointer is
initialized to a function for call/reference.
Advantages of functions:
• Breaks a large program into smaller, manageable modules.
• Improves code reusability — the same function can be called multiple times.
• Makes the program easier to read, debug, and maintain.
• Reduces code length by avoiding repetition.
• Allows easier testing, since each function can be tested independently.
A function pointer is a pointer that stores the address of a function instead of a variable. It is used to
call a function indirectly.
Syntax: return_type (*pointer_name)(parameter_types);
#include <stdio.h>
int add(int a, int b) {
return a + b;
}
int main() {
int (*funcPtr)(int, int); // function pointer declaration
funcPtr = add; // initialize with function's address
int result = funcPtr(3, 4); // call function via pointer
printf("Sum = %d", result);
return 0;
}
Output: Sum = 7
Final Answer: Functions improve modularity, reusability, and readability. A function pointer is declared
as return_type (*ptr)(params), initialized with a function's name (its address), and used to call that
function indirectly.
Q23. Explain Function Arguments, Return statement, and Function prototypes with
suitable examples.
Function Arguments – Values passed into a function when it is called. They allow data to be sent from
the calling function to the called function.
add(5, 3); // 5 and 3 are arguments
Return Statement – Used to end a function's execution and send a value back to the calling function.
Its type must match the function's declared return type (void returns nothing).
int square(int n) {
return n * n; // sends value back to caller
}
Function Prototype – A declaration that tells the compiler the function's name, return type, and
parameter types, before the function is actually used or defined. It ensures correct type checking and is
usually placed before main().
#include <stdio.h>
int square(int n); // function prototype
int main() {
printf("%d", square(5)); // 25
return 0;
}
int square(int n) { // function definition
return n * n;
}
Final Answer: Arguments pass data into a function, the return statement sends a result back, and a
function prototype declares the function's signature in advance so the compiler can check calls for
correctness.
Q24. Develop a C program and a function to check whether the given number is Prime or
not.
#include <stdio.h>
int isPrime(int n) {
int i;
if (n <= 1)
return 0; // not prime
for (i = 2; i <= n / 2; i++) {
if (n % i == 0)
return 0; // divisible, not prime
}
return 1; // prime
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (isPrime(num))
printf("%d is Prime", num);
else
printf("%d is Not Prime", num);
return 0;
}
Sample Input: 7 → Output: 7 is Prime
Sample Input: 10 → Output: 10 is Not Prime
Final Answer: The isPrime() function checks divisibility from 2 up to n/2; if any number divides n
exactly, it is not prime, otherwise it is prime (numbers ≤ 1 are not prime).
Q25. Differentiate between different dynamic memory allocation functions in C.
Function Memory Initialized? No. of Blocks Use Case
malloc() No (garbage values) Single block Allocate memory of a given size
calloc() Yes (initialized to 0) Multiple blocks Allocate and zero-initialize memory
realloc() Keeps old data Resizes existing block Increase/decrease previously allocated memory
free() — — Deallocates memory back to the system
Final Answer: malloc() allocates uninitialized memory, calloc() allocates and zero-initializes multiple
blocks, realloc() resizes already allocated memory, and free() releases the memory once it is no longer
needed.