A. MCQ & Very Short Questions: Mark: 1
Q1. It can be defined as a programming model which is based upon the concept of calling procedure. What is it called?
Answer: It is called Procedure Oriented Programming (POP).
[HSLC ’24]
B. Short Type Questions: Marks: 2/3
Q1. What do you mean by library function? Is sum() a library function? Justify.
Answer: A library function is a pre-defined function available in the C library which can be directly used in a program, such as printf(), scanf(), sqrt(), etc. They are stored in header files like <stdio.h>, <math.h>.
sum() is not a library function because it is not part of the standard C library; it has to be created by the programmer. Hence, sum() is a user-defined function, not a library function.
[HSLC ’23]
Q2. What is group function? Explain with an example.
Answer: A group function is a function that performs an operation on a group of values and gives a single result.
👉 Example: In databases, SUM() adds a group of values, and in C, strlen() works on a group of characters to return the length of a string.
[HSLC ’23]
Q3. Can we have two functions with the same name but with different number of parameters in a single program? Write a C program to justify your answer.
Answer: No, in C programming language we cannot have two functions with the same name but different number of parameters. This concept is called function overloading, which is supported in C++ but not in C.
👉 Example (in C):
#include <stdio.h>
int add(int a, int b) {
return a + b;
}
int add(int a, int b, int c) { // Error in C
return a + b + c;
}
int main() {
printf("%d", add(2, 3));
return 0;
}
This program will give an error in C because function overloading is not allowed.
[HSLC ’23]
Q4. What is user-defined function? Give an example.
Answer: A user-defined function is a function created by the programmer to perform a specific task. It helps in code reusability and better program structure.
👉 Example:
#include <stdio.h>
int sum(int a, int b) {
return a + b;
}
int main() {
printf("Sum = %d", sum(5, 10));
return 0;
}
Here, sum() is a user-defined function.
[HSLC ’24]
Q5. What are the different components of a function?
Answer: The different components of a function are:
- Function Declaration (Prototype): Tells the compiler about the function name, return type, and parameters.
- Function Definition: The actual body of the function containing the code.
- Function Call: The statement that invokes the function in the program.
[HSLC ’25]
Additional Important Questions
A. Short type questions : Marks : 2/3
Q1. What is function in computer programming?
Answer: A function in computer programming is a block of code that performs a specific task. It helps in dividing a large program into smaller parts, making the program easier to understand, test, and maintain. Functions allow code reusability and reduce redundancy.
Q2. What is function name?
Answer: The function name is the identifier given to a function. It is used to call and access the function in the program. For example, in int sum(int a, int b), the function name is sum.
Q3. What is return type?
Answer: The return type of a function specifies the type of value that the function will return to the calling program after execution. For example, if a function returns an integer, its return type is int. If it does not return anything, the return type is void.
Q4. What is function parameter?
Answer: Function parameters are variables declared inside the parentheses of a function definition. They act as inputs to the function. For example, in int sum(int a, int b), the variables a and b are parameters.
Q5. Name the three parts of a function in C.
Answer: The three parts of a function in C are:
- Function Declaration (Prototype)
- Function Definition
- Function Call
Q6. What is function declaration? Give an example.
Answer: A function declaration tells the compiler about the function’s name, return type, and parameters before it is used. It does not contain the body of the function.
👉 Example: int sum(int, int);
Q7. What is function call? Give an example.
Answer: A function call is the statement used to invoke a function in a program. It transfers control to the function.
👉 Example:
sum(5, 10); // Function call
Q8. What is function declaration?
Answer: A function declaration is a statement that provides information to the compiler about the function such as its return type, name, and number of parameters. It ensures that the function is used correctly in the program.
Q9. How many types of functions in C programming? What are they?
Answer: There are two types of functions in C programming:
- Library Functions – Predefined functions provided by C (e.g.,
printf(),scanf()). - User-Defined Functions – Functions created by the programmer to perform specific tasks.
Q10. What is library functions?
Answer: Library functions are pre-defined functions provided by the C standard library. They are already written, compiled, and stored in header files. Examples include printf(), scanf(), sqrt(), and strlen().
Q11. What is user-defined functions?
Answer: User-defined functions are functions that are written by the programmer for performing specific tasks. They help in breaking down a large program into smaller parts. Example:
int sum(int a, int b) {
return a + b;
}
Q12. What is recursive function?
Answer: A recursive function is a function that calls itself during execution. It is mainly used to solve problems that can be divided into smaller sub-problems.
👉 Example:
int fact(int n) {
if(n == 0) return 1;
else return n * fact(n-1);
}
This function calculates factorial using recursion.
B. Essay type questions : Marks 4/5
Q1. What is a function? Mention the advantages of using functions in programming. How functions can be categorized in C?
Answer:
A function in C is a self-contained block of code that performs a specific task. It helps in breaking a large program into smaller modules, making it more organized and easier to manage.
Advantages of functions:
- Increases code reusability.
- Makes program easier to understand and debug.
- Reduces code redundancy.
- Supports modular programming.
- Saves development time and effort.
Categories of functions in C:
- Library functions – Predefined functions provided in C libraries, e.g.,
printf(),scanf(),sqrt(). - User-defined functions – Functions created by the programmer, e.g.,
int sum(int a, int b).
Q2. What is a global variable in C? Why do we need such a variable?
Answer:
A global variable is a variable that is declared outside all functions. It can be accessed and modified by any function in the program.
Need of global variables:
- They are used when multiple functions need to share and update the same data.
- They reduce the need of passing parameters repeatedly.
- They are useful for storing program-wide constants.
👉 Example:
#include <stdio.h>
int x = 10; // global variable
void display() {
printf("x = %d\n", x);
}
int main() {
display();
x = 20;
display();
return 0;
}
Q3. Write the syntax of a function declaration. The name of the function is calculateAge(). The function accepts the current year and birth year of a person. The function returns the age of the person.
Answer:
The syntax of function declaration is:
return_type function_name(parameter_list);
👉 Declaration of calculateAge():
int calculateAge(int currentYear, int birthYear);
Q4. Write the code segment for the function definition of the above function calculateAge().
Answer:
👉 Function definition:
int calculateAge(int currentYear, int birthYear) {
return currentYear - birthYear;
}
This function subtracts the birth year from the current year and returns the age of the person.
Q5. What are different types of functions in C? Differentiate among them.
Answer:
In C, functions are of two types:
-
Library Functions
- Predefined in header files.
- Ready to use, no need to define again.
- Example:
printf(),scanf().
-
User-Defined Functions
- Created by the programmer.
- Used for specific tasks as per program needs.
- Example:
int sum(int a, int b).
Difference:
| Library Functions | User-Defined Functions |
|---|---|
| •Already available in libraries | •Created by the programmer |
| •No need to write definition | •Definition must be written |
•Examples: printf(), sqrt() |
•Examples: sum(), factorial() |
Q6. Differentiate between caller and callee functions. Write a small C program and identify the caller and callee function in it.
Answer:
- Caller Function: The function which calls another function.
- Callee Function: The function which is called by another function.
👉 Example:
#include <stdio.h>
int sum(int a, int b) { // callee function
return a + b;
}
int main() {
int result = sum(5, 10); // caller function
printf("Sum = %d", result);
return 0;
}
Here, main() is the caller, and sum() is the callee function.
Q7. When do we call a function user-defined? Is printf() a user-defined function? Justify.
Answer:
A function is called user-defined when it is written by the programmer for a specific purpose in the program.
printf() is not a user-defined function; it is a library function defined in <stdio.h>. Programmers can use it directly, but cannot modify its definition.
Q8. Can we have two functions with the same name but with different numbers of parameters in a single C program? Write a simple C program to justify your answer.
Answer:
No, C does not support function overloading (two functions with the same name but different parameters). This feature is available in C++, not in C.
👉 Example (error in C):
#include <stdio.h>
int add(int a, int b) {
return a + b;
}
int add(int a, int b, int c) { // Not allowed in C
return a + b + c;
}
int main() {
printf("%d", add(5, 10));
return 0;
}
This program will give an error, showing function overloading is not possible in C.
Q9. What are different components of a function? Show with a complete C program.
Answer:
The main components of a function are:
- Function Declaration (Prototype)
- Function Definition
- Function Call
👉 Example:
#include <stdio.h>
// Function declaration
int sum(int, int);
int main() {
int result;
// Function call
result = sum(10, 20);
printf("Sum = %d", result);
return 0;
}
// Function definition
int sum(int a, int b) {
return a + b;
}
Here:
int sum(int, int);→ Function declarationsum(10, 20);→ Function call- Function body → Function definition
Q.10. Define recursive function. Can we use a recursive function to solve all kinds of problems?
Answer :
A recursive function is a function that calls itself directly or indirectly in order to solve a problem. In recursion, a big problem is broken down into smaller sub-problems until a base condition is reached.
Example:
int fact(int n)
{
if(n==0 || n==1)
return 1;
else
return n * fact(n-1);
}
Here, fact() is a recursive function to calculate factorial.
👉 However, recursion cannot be used to solve all problems. Some problems are better solved using iteration (loops) because recursion may lead to infinite calls, stack overflow, or inefficiency.
Thus, recursion is useful for problems like factorial, Fibonacci series, tree traversal, etc., but not always suitable for all cases.
Q.11. Consider the below code and list all the syntax errors.
#include <stdio.h>
int fun(int x)
{
if(x % 2 == 0)
return 1;
else
return 0;
}
int main()
{
int number;
printf("\n Enter the number: ");
scanf("%d", &number);
int x = fun(); // ERROR
return 0;
}
Answer :
The syntax errors in the given code are:
int x = fun();→ Error: functionfun()requires one argument but none is given.
✅ Correct:int x = fun(number);- Although not a syntax error, there is no output statement to display the result.
✅ Should add:printf("%d", x);
So, the corrected program should be:
#include <stdio.h>
int fun(int x)
{
if(x % 2 == 0)
return 1;
else
return 0;
}
int main()
{
int number;
printf("\n Enter the number: ");
scanf("%d", &number);
int x = fun(number);
printf("%d", x);
return 0;
}
Q.12. Consider the code segment below and find out the output if the user enters 5 from the keyboard when asked for.
#include <stdio.h>
int fun(int x)
{
if(x % 2 == 0)
return 1;
else
return 0;
}
int main()
{
int number;
printf("\n Enter the number: ");
scanf("%d", &number);
int x = fun(number);
printf("%d", x);
return 0;
}
Answer :
Step-by-step execution:
- The user enters
5. - Function call:
fun(5) - Inside function:
5 % 2 == 0→ condition is false. - So, it executes
else return 0; - Therefore, the value of
x = 0. printf("%d", x);prints 0.
👉 Final Output:
0
Q.13. Write a C program and define a function square() that accepts a number as the parameter and returns the square of that number as output.
Answer :
#include <stdio.h>
// Function definition
int square(int n)
{
return n * n;
}
int main()
{
int num, result;
printf("Enter a number: ");
scanf("%d", &num);
result = square(num);
printf("Square of %d is %d\n", num, result);
return 0;
}
👉 If input = 5, Output = 25.
Q.14. Write a C program and define a function search() that searches an element in an array and returns the index of the element.
Answer :
#include <stdio.h>
// Function definition
int search(int arr[], int size, int key)
{
for(int i = 0; i < size; i++)
{
if(arr[i] == key)
return i; // return index
}
return -1; // if not found
}
int main()
{
int arr[5] = {10, 20, 30, 40, 50};
int key, pos;
printf("Enter the element to search: ");
scanf("%d", &key);
pos = search(arr, 5, key);
if(pos != -1)
printf("Element found at index %d\n", pos);
else
printf("Element not found\n");
return 0;
}
👉 If input = 30, Output = Element found at index 2.
Q.15. Write a C program and define a recursive function to find the summation of first N natural numbers.
Answer :
#include <stdio.h>
// Recursive function definition
int sum(int n)
{
if(n == 0)
return 0;
else
return n + sum(n - 1);
}
int main()
{
int n, result;
printf("Enter value of n: ");
scanf("%d", &n);
result = sum(n);
printf("Summation of first %d natural numbers = %d\n", n, result);
return 0;
}
👉 If input = 5, Output = 15.
Q.16. Write a C program and define a function add() that accepts three integers. These integers indicate indices of an integer array. The function returns the summation of the elements stored in those indices.
Answer :
#include <stdio.h>
// Function definition
int add(int arr[], int i, int j, int k)
{
return arr[i] + arr[j] + arr[k];
}
int main()
{
int arr[6] = {7, 8, 8, 0, 0, 9};
int result;
// Example: call with indices 0, 2, 5
result = add(arr, 0, 2, 5);
printf("Summation = %d\n", result);
return 0;
}
👉 For indices (0,2,5), Output = 24 (7+8+9).

Comments
Post a Comment