Skip to main content

Computer Science | Class 10 | Chapter 7: Functions in C | Question Bank Solutions SEBA.

 


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:

  1. Function Declaration (Prototype): Tells the compiler about the function name, return type, and parameters.
  2. Function Definition: The actual body of the function containing the code.
  3. 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:

  1. Function Declaration (Prototype)
  2. Function Definition
  3. 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:

  1. Library Functions – Predefined functions provided by C (e.g., printf(), scanf()).
  2. 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:

  1. Increases code reusability.
  2. Makes program easier to understand and debug.
  3. Reduces code redundancy.
  4. Supports modular programming.
  5. Saves development time and effort.

Categories of functions in C:

  1. Library functions – Predefined functions provided in C libraries, e.g., printf(), scanf(), sqrt().
  2. 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:

  1. Library Functions

    • Predefined in header files.
    • Ready to use, no need to define again.
    • Example: printf(), scanf().
  2. 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:

  1. Function Declaration (Prototype)
  2. Function Definition
  3. 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 declaration
  • sum(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:

  1. int x = fun();Error: function fun() requires one argument but none is given.
    ✅ Correct: int x = fun(number);
  2. 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

Popular posts from this blog

Class 2 | Worksheets | All Subjects | For CBSE & SEBA Students

Worksheets for Class 2 students , suitable for revision or practice. These include English, Math, EVS, and Logical Thinking , designed to build thinking and writing skills. ✏️ Worksheet 1: English – Sentence Formation Instructions : Rearrange the jumbled words to make correct sentences. Start with a capital letter and end with a full stop. playing / is / the / boy / football → ________________________________________ cat / the / on / is / the / mat → ________________________________________ apples / she / red / likes → ________________________________________ garden / flowers / the / in / are → ________________________________________ 🔢 Worksheet 2: Math – Word Problems Instructions : Read the questions carefully and solve them. Rina has 24 chocolates. She gives 9 to her brother. How many chocolates are left? → ___________________________ A basket has 36 apples. 18 apples are red and the rest are green. How many green apples are there? → _______________...

TENSE FORMS | English Grammar | Class 5 to 10; Question Paper Solutions of HSLC Exam; Most Common Questions.

    TENSE FORMS (CLASS 5th TO 10th) ✅ H.S.L.C. – 1998 Questions: When he entered my room, I —— (write) a letter. The match —— (start) before we reached the field. It is time you —— (go) home. Answers: was writing had started went ✅ H.S.L.C. – 1999 Questions: We shall wait here until he —— (come) back. When he came in, I —— (write) a letter. Answers: comes was writing ✅ H.S.L.C. – 2000 Questions: He —— (walk) across the road when a scooter hit him. I wish I —— (accept) the job. Hurry up, the taxi —— (wait). Answers: was walking had accepted is waiting ✅ H.S.L.C. – 2001 Questions: They —— (live) in Delhi for ten years. She died after she —— (suffer) for five years. Ten years —— (pass) since I met you. When he came, it —— (be) all over. He —— (stay) here until you return. Answers: have lived had suffered have passed was will stay ✅ H.S.L.C. – 2002 Questions: When he started for office, it still —— (rain). I —— (not m...

ENGLISH Literature | Class 9 | Poetry; Lesson 1: The Road Not Taken | Question Bank Solutions.

  Use this Question Bank to write comfortably. A. MCQ and Very Short Questions: 1. Who is the poet of this poem? (a) John Keats (b) William James (c) William Wordsworth ✅ (d) Robert Frost 2. What does the poem speak about? (a) about the plight of roads (b) about the people (c) about two roads ✅ (d) about the choices made by people 3. What does road signify in the poem? ✅ (a) the path or choice that was left or was not chosen to tread (b) The pathway (c) pathway on the roadside (d) a road with two turns 4. Which thing decides a person’s future according to this poem? ✅ (a) the path one chooses to walk (b) the path one leaves behind (c) the regrets (d) the success 5. What is the message of this poem? (a) be wise while choosing and taking decisions (b) two roads are confusing (c) road is nothing but a pathway ✅ (d) all of the above 6. Why is the poet asking to be wise while choosing a pathway? ✅ (a) because there is no Going Back option (b) Because it...