Class 10 Computer Science Chapter 7 Function In C

Class 10 Computer Science Chapter 7 Function In C Question Answer, SEBA Class 10 Computer Science Solutions, NCERT Class 10 Computer Science Chapter 7 Function In C to each chapter is provided in the list so that you can easily browse throughout different chapters SCERT Class 10 Computer Science Chapter 7 Function In C and select needs one.

Class 10 Computer Science Chapter 7 Function In C

Join Telegram channel

Also, you can read the SCERT book online in these sections Solutions by Expert Teachers as per SCERT (CBSE) Book guidelines. NCERT Solution of Class 10 Computer Science Chapter 7 Function In C is part of AHSEC All Subject Solutions. Here we have given Class 10 Computer Science Chapter 7 Function In C Notes for All Subjects, You can practice these here in Class 10 Computer Science.

FUNCTIONS IN C

Chapter – 7

COMPUTER SCIENCE

TEXTUAL QUESTIONS AND ANSWERS

1. What is a global variable in C? Why do we need such a variable?

Ans. A global variable is a variable that is declared outside any function and is accessible to all routines in our program. It has a global scope means it holds its value throughout the lifetime of the program. 

Hence, it can be accessed throughout the program by any function defined within the program.

2. Write the syntax of a function declaration. 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.

Ans. Function declaration: 

int calculateAge(int current_year, int birth_year);

3. Write the code segment for the function definition of the above function calculateAge ().

Ans. Code segment:

int calculateAge(int current_year, int birth_year)

{

int age;

age = current_year — birth_year,

return age;

}

4. What are the different types of functions in C? Differentiate between them.

Ans. There are two types of function in C programming: Standard library functions. User-defined functions.

Library functions are built-in functions which are defined inside C library whereas, user-defined functions are declared and defined by the programmer based on their needs.

5. Differentiate between caller and callee functions. Write a small C program and identify the caller and callee function in it.

Ans. A caller is a function that calls another function; a callee is a function that was called. 

Example:

#include <stdio.h>

// Callee function

int add(int a, int b) {

    return a + b;

}

int main() {

    // Caller function

    int x = 5, y = 3;

    int result = add(x, y); // Call the add function

    printf("The result of adding %d and %d is %d\n", x, y, result);

    return 0;

}

In this program:

  • ‘main’ is the caller function because it initiates the function call by invoking add(x, y).
  • ‘add’ is the callee function because it receives the arguments ‘x’ and ‘y’, performs the addition, and returns the result to the caller function (main).

6. When do we call a function user-defined? Is printf() a user-defined function? Justify.

Ans. User-defined functions are functions that you use to organize your code in the body of a policy. Once you define a function, you can call it in the same way as the built-in action and parser functions. Variables that are passed to a function are passed by reference, rather than by value.

No, printf() is not a user–defined function. It is a built–in – function which is also called a library function.

7. 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.

Ans. In C program, we cannot have two functions with the same name.

Code:

#include <stdio.h>

// Function with one parameter

void printMessage(char* message) {

    printf("%s\n", message);

}

// Attempting to declare another function with a different parameter count

void printMessage() {

    printf("No message provided.\n");

}

int main() {

    char message[] = "Hello, World!";

    printMessage(message);

    return 0;

}
  • If you try to compile this program, you will receive an error similar to the following:

Output:

error: conflicting types for ‘printMessage’

8. Write the different components of a function? Show with a complete C program.

Ans. There are three parts of a function in C.

  • Function Signature: This includes the function’s name, return type, and parameters (if any).
  • Function Body: This contains the actual code or statements that are executed when the function is called.
  • Function Call: This is where the function is invoked or called from another part of the program.

Example:

#include <stdio.h>

// Function Signature

int add(int a, int b); // Function to add two integers

// Function Body

int add(int a, int b) {

    int sum = a + b;

    return sum;

}

int main() {

    // Function Call

    int x = 5, y = 3;

    int result = add(x, y); // Call the add function

    printf("The result of adding %d and %d is %d\n", x, y, result);

    return 0;

}

9. Define recursive function. Can we use a recursive function to solve all kinds of problems?

Ans. Any function that happens to call itself again and again (directly or indirectly), unless the program satisfies some specific condition/subtask is called a recursive function.

Recursive functions are a powerful and versatile tool in programming, but they may not be the best or most efficient choice for solving all types of problems. Whether a recursive function can be used effectively depends on the nature of the problem and the specific requirements of the task.

10. Consider the below code and list all the syntax 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 ();

return 0;

}

Solution:

  • The format of the ‘printf’ and ‘scanf’ function calls should use double quotes (“) for specifying format strings. However, the code uses incorrect curly quotes (“ and ”).
  • The fun function is called without any arguments, but it is defined to accept one integer argument int x. The call should be like int x = fun(number);.

Rewrite:

#include <stdio.h>

int fun(int x) {

    if (x % 2 == 0)

        return 1;

    else

        return 0;

}

int main() {

    int number;

    printf("\nEnter the number: ");

    scanf("%d", &number);

    int x = fun(number); // Pass the 'number' as an argument

    return 0;

}

11. 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;

}

Ans. If the user enters 5, the program will output 0 to indicate that 5 is an odd number.

12. 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.

Solution: Code:

#include <stdio.h>

// Function to calculate the square of a number

int square(int num) {

    return num * num;

}

int main() {

    int number, result;

    // Input from the user

    printf("Enter a number: ");

    scanf("%d", &number);

    // Calculate the square using the 'square' function

    result = square(number);

    // Display the result

    printf("The square of %d is %d\n", number, result);

    return 0;

}

Output: 

Enter a number: 5

The square of 5 is 25

13. Write a C program and define function search () that searches an element in an array and returns the index of the element.

Solution: Code:

#include <stdio.h>

// Function to search for an element in an array

int search(int array[], int size, int target) {

    for (int i = 0; i < size; i++) {

        if (array[i] == target) {

            return i; // Element found, return its index

        }

    }

    return -1; // Element not found, return -1

}

int main() {

    int array[] = {10, 20, 30, 40, 50, 60, 70};

    int size = sizeof(array) / sizeof(array[0]);

    int target, result;

    printf("Enter the element to search for: ");

    scanf("%d", &target);

    result = search(array, size, target);

    if (result != -1) {

        printf("Element %d found at index %d\n", target, result);

    } else {

        printf("Element %d not found in the array\n", target);

    }

    return 0;

}

Output:

Enter the element to search for: 30

Element 30 found at index 2

14. Write a C program and define a recursive function to find the summation of first N natural numbers.

Solution: Code:

#include <stdio.h>

// Recursive function to find the summation of first N natural numbers

int sumOfNaturals(int n) {

    if (n == 1) {

        return 1; // Base case: 1 is the sum of the first natural number

    } else {

        return n + sumOfNaturals(n - 1); // Recursive case: n + sum of (n-1) natural numbers

    }

}

int main() {

    int N, result;

    printf("Enter a positive integer N: ");

    scanf("%d", &N);

    if (N < 1) {

        printf("N must be a positive integer.\n");

    } else {

        result = sumOfNaturals(N);

        printf("The sum of the first %d natural numbers is %d.\n", N, result);

    }

    return 0;

}

Output:

Enter a positive integer N: 5

The sum of the first 5 natural numbers is 15.

15. Write a C program and define a function add () that accept three integers. These integers indicate indices of an integer array. The function returns the summation of the elements stored in those indices.

788009

For example, if we call the function add (0, 2, 5), the function will return 24. The output is formed by 7 + 8 + 9 because elements at indices 0, 2 and 5 are 7, 8 and 9 respectively.

Solution: Code:

#include <stdio.h>

// Function to calculate the sum of elements at specified indices

int add(int array[], int index1, int index2, int index3) {

    return array[index1] + array[index2] + array[index3];

}

int main() {

    int array[] = {7, 8, 8, 0, 0, 9};

    // Indices of the elements to sum

    int index1 = 0, index2 = 2, index3 = 5;

    int result = add(array, index1, index2, index3);

    printf("The sum of elements at indices %d, %d, and %d is %d\n", index1, index2, index3, result);

    return 0;

}

Output:

The sum of elements at indices 0, 2, and 5 is 24

1 thought on “Class 10 Computer Science Chapter 7 Function In C”

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top