SEBA Class 10 Computer Science Textbook Solution Chapter 7


Explore the solutions chapter-wise:

SL. No.

Contents

Chapter 1

Introduction to Computer Network

Chapter 2

HTML5 and CSS3

Chapter 3

Database Part - II MySQL

Chapter 4

Introduction to Loops

Chapter 5

Nested Loops in C

Chapter 6

Arrays in C

Chapter 7

Functions in C

Chapter 8

Pointers in C

Chapter 9

Structure in C

Chapter 10

An Introduction to Object Oriented Programming

Chapter 11

Case Studies


Chapter 7 – Functions in C


Short Answer Type Questions

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 all functions. It can be used by any function in the program.
It has global scope, which means it keeps its value for the entire execution of the program.
We need global variables when many functions need to use the same data.

2. Write the syntax of a function declaration named calculateAge().
(The function accepts current year and birth year and returns age.)
Ans:

int calculateAge(int current_year, int birth_year);

3. Write the function definition of calculateAge().
Ans:

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 functions in C:

a) Library (Standard) Functions
i. Already defined in C libraries
ii. Example: printf(), scanf()

b) User-defined Functions
i. Created by the programmer
ii. Used to perform specific tasks

Difference:

Library FunctionsUser-Defined Functions
Library functions are predefined functions provided by the C language.User-defined functions are created by the programmer.
They are stored in header files like stdio.h, math.h, etc.They are written inside the program by the user.
No need to write their code again.The programmer must write the function code.
They perform common and standard tasks.They perform specific tasks as per program requirement.
Examples: printf(), scanf(), sqrt()Examples: add(), square(), calculateAge()
They help in saving time and effort.They help in making programs organized and reusable.

5. Differentiate between caller and callee functions with an example.
Ans:

Caller FunctionCallee Function
The caller function is the function that calls another function.The callee function is the function that is called by another function.
It sends values (arguments) to another function.It receives values (parameters) from the caller function.
Program execution starts from the caller.Program execution shifts to the callee when it is called.
It receives the result returned by the called function.It performs a task and returns the result to the caller.
Example: main() functionExample: add() function

Example Program:

#include <stdio.h>

// Callee function
int add(int a, int b)
{
return a + b;
}

int main()
{
// Caller function
int result = add(5, 3);
printf("Result = %d", result);
return 0;
}

6. When do we call a user-defined function? Is printf() a user-defined function?
Ans:
A user-defined function is called when we want to divide a program into smaller parts for better understanding and reuse.
No, printf() is not a user-defined function. It is a library function, already provided by C.

7. Can we have two functions with the same name in C? Explain with an example.
Ans:
We cannot have two functions with the same name, even if the parameters are different because C does not allow function overloading.

Example (Error Program):

#include <stdio.h>

void printMessage(char *msg)
{
printf("%s", msg);
}

void printMessage()
{
printf("Hello");
}

int main()
{
printMessage("Hi");
return 0;
}

This will give a compile-time error.

8. Write the different components of a function with a complete C program.
Ans:
A function has three components:
a) Function Declaration
b) Function Definition
c) Function Call

Example Program:

#include <stdio.h>

// Function declaration
int add(int a, int b);

// Function definition
int add(int a, int b)
{
return a + b;
}

int main()
{
int result = add(5, 3); // Function call
printf("Sum = %d", result);
return 0;
}

9. Define a recursive function. Can it solve all problems?
Ans:
A recursive function is a function that calls itself until a certain condition is met.
Recursive functions are useful for some problems, but not suitable for all problems because they can use more memory and time.

10. Find the syntax errors in the given code and rewrite it.
Ans:
Errors:
a) Wrong quotation marks in printf() and scanf()
b) Function fun() is called without passing an argument

Corrected Code:

#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);
return 0;
}

11. Find the output if the user enters 5.
Ans:
Since 5 is an odd number, the function returns 0.

Output:

0

Long Answer Questions

12. Write a C program to find the square of a number using a function.
Program:

#include <stdio.h>

int square(int num)
{
return num * num;
}

int main()
{
int number;
printf("Enter a number: ");
scanf("%d", &number);

printf("Square = %d", square(number));
return 0;
}

Output:

Enter a number: 5
Square = 25

13. Write a C program to search an element in an array using a function.
Program:

#include <stdio.h>

int search(int arr[], int size, int key)
{
for (int i = 0; i < size; i++)
if (arr[i] == key)
return i;
return -1;
}

int main()
{
int arr[] = {10, 20, 30, 40, 50};
int key;
printf("Enter element: ");
scanf("%d", &key);

int result = search(arr, 5, key);

if (result != -1)
printf("Found at index %d", result);
else
printf("Not found");

return 0;
}

14. Write a recursive function to find the sum of first N natural numbers.
Program:

#include <stdio.h>

int sum(int n)
{
if (n == 1)
return 1;
else
return n + sum(n - 1);
}

int main()
{
int n;
printf("Enter N: ");
scanf("%d", &n);

printf("Sum = %d", sum(n));
return 0;
}

Output:

Enter N: 5
Sum = 15

15. Write a function that adds array elements using given indices.
Program:

#include <stdio.h>

int add(int arr[], int i1, int i2, int i3)
{
return arr[i1] + arr[i2] + arr[i3];
}

int main()
{
int arr[] = {7, 8, 8, 0, 0, 9};
int result = add(arr, 0, 2, 5);

printf("Sum = %d", result);
return 0;
}

Output:

Sum = 24