Semester
Subject
Year
Tribhuwan University
2077
Bachelor Level / First Year / First Semester / Science
(C Programming)
Full Marks: 60
Pass Marks: 24
Time: 3 Hours
Candidates are required to give their answers in their own words as for as practicable.
The figures in the margin indicate full marks.
Long Answers Questions
A function is a self-contained block of code that performs a specific task and can be reused multiple times in a program.
Tells the compiler about the function's name, return type, and parameters before it is used.
return_type function_name(parameter_list);
Example: int add(int a, int b);
Contains the actual body of the function with the code to be executed.
int add(int a, int b) {
return a + b;
}
Invokes the function to execute its code by passing actual arguments.
int result = add(5, 3);
Conclusion: Functions make programs organized, reusable, and easier to debug by dividing complex problems into smaller sub-problems.
A recursive function is a function that calls itself repeatedly until a base condition is met.
#include <stdio.h>
int factorial(int n) {
if (n == 0 || n == 1)
return 1; // Base condition
else
return n * factorial(n - 1); // Recursive call
}
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
printf("Factorial of %d = %d", n, factorial(n));
return 0;
}
factorial(5) = 5 × factorial(4)factorial(4) = 4 × factorial(3)factorial(3) = 3 × factorial(2)factorial(2) = 2 × factorial(1)factorial(1) = 1 (Base condition reached)Final Result: 5 × 4 × 3 × 2 × 1 = 120
Conclusion: Recursion simplifies problems like factorial by breaking them into smaller identical sub-problems until the base case is reached.
Short Answers Questions