Functions in C: SEBA Class 10 Computer questions and answers

Functions in C seba class 10
Share with others

Get answers, questions, notes, textbook solutions, extras, pdf, mcqs, for Computer Chapter 7 Functions in C of class 10 (HSLC/Madhyamik) for students studying under the Board of Secondary Education, Assam (SEBA). These notes/answers, however, should only be used for references and modifications/changes can be made wherever possible.

Summary

Functions are an important programming concept that help divide a large program into smaller modular blocks of code. Each function performs a specific task and enhances code reusability. In C programming, a function declaration informs the compiler about the function name, parameters, and return type. The function definition contains the actual code statements that are executed when the function is called. We invoke a function by making a function call and passing arguments if required. The control shifts from the calling function to the called function during execution. After completing its task, the called function returns control back to the calling function along with a return value if applicable.

Functions can be library functions already defined in header files or user-defined written by the programmer. Parameters or arguments enable passing data to functions. Functions can return a value of a specific data type or void if no return value. We can write function declarations separately or combine with definitions if the definition appears first. Global variables are accessible to all functions while local variables are private to the function. Recursive functions call themselves repeatedly until a base condition is met. Factorial calculation can be implemented recursively. Dividing code into functions makes programs more readable, maintainable and aids debugging. Functions allow reusing code easily. Different functions can have the same name if parameter lists differ. Judicious use of functions is key to modular programming.

Exercise/textual questions and answers

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

Answer: A global variable in C is a variable that is declared outside of any function, usually at the top of the code. It can be accessed by any function in the program. We need global variables when we want to share data between multiple functions rather than passing the data back and forth as parameters.

2. 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: int calculateAge(int currentYear, int birthYear);

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

Answer: int calculateAge(int currentYear, int birthYear)
{
int age = currentYear – birthYear;
return age;
}

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

Answer: There are two types of functions in C:

Library functions: These are the functions that are defined in the C header files. We use these functions while writing our programs.

User-defined functions: These are the functions that are created by the programmer.

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

Answer: The caller function is the function that calls or makes a call to another function. The callee function is the function that is being called by the caller function.

A small program example:

#include <stdio.h>
// Callee Function
int add(int a, int b) {
return a + b;
}
// Caller Function
int main() {
int num1 = 5, num2 = 10;
// Calling the ‘add’ function and storing the result
int result = add(num1, num2);
printf(“The sum of %d and %d is %d.\n”, num1, num2, result);
return 0;
}

6. 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 defined by the programmer, as opposed to a library function which is pre-defined in C libraries. printf() is a library function, not a user-defined 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.

Answer: Yes, we can have two functions with the same name but different parameters in C. A simple program:

int add(int a, int b){
//function 1
}
double add(double a, double b){
//function 2
}

8. What are different components of a function? Show with a complete C program.

Answer: The components of a function are function declaration, function definition, and function calling.

A complete sample program:

int add(int a, int b); //declaration
int main(){
int sum = add(5,3); //calling
}
int add(int a, int b){ //definition
return a+b;
}

9. 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. Recursive functions can be used to solve problems that can be broken down into smaller sub-problems, like calculating factorials, Fibonacci series etc. But they cannot be used for all kinds of problems.

10. 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;
}

Answer: Here are the syntax errors in the given code:

  • Missing return type in function declaration int fun(int x)
  • Missing semicolon after function declaration
  • Missing parentheses in function call int x = fun(number);
  • Missing braces {} in function definition
  • Missing return statement in function definition
  • Statement if(x%2 == 0) is outside function definition

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;
}

Answer: When the user enters 5,

  • 5 is stored in ‘number’
  • number (which is 5) is passed to fun()
  • Since 5 is odd, x%2 == 0 evaluates to false
  • So fun() returns 0
  • This 0 is stored in x
  • x (which is 0) is printed

Therefore, the final output will be 0 when the input is 5.

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.  

Answer: #include <stdio.h>
int square(int num){
  return num*num;
}
int main() {
  int n, result;
  printf(“Enter a number: “);
  scanf(“%d”, &n);
  result = square(n);
  printf(“Square is %d”, result);
  return 0;
}

13. 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>
int search(int arr[], int size, int elem){
  for(int i=0; i<size; i++){
    if(arr[i] == elem){
      return i;
    }
  }
  return -1;
}
int main(){
  int arr[] = {1, 3, 5, 7};
  int n = 4;
  int index = search(arr, n, 5);
if(index == -1){
    printf(“Element not found”);
  }
  else {
    printf(“Element found at index %d”, index);
  }
  return 0;
}

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

Answer: #include <stdio.h>
int sum(int n) {
  if(n == 0) {
    return 0;
  }
  else {
    return n + sum(n-1);
  }
}
int main() {
  int n, result;
printf(“Enter N: “);
scanf(“%d”, &n);
  result = sum(n);
printf(“Sum is %d”, result);
  return 0;
}

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

788009

For example, 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.

Answer: #include <stdio.h>
int arr[] = {7, 8, 8, 0, 0, 9};
int add(int i, int j, int k){
  return arr[i] + arr[j] + arr[k];
}
int main() {
  int result = add(0, 2, 5);
  printf(“Sum is %d”, result);
  return 0;
}

Extra/additional questions and answers

1. What is a function in C programming?

Answer: A function in C programming is a group of statements that together perform a task. We can divide our code of a program into separate functions.

2. What are the components of a function in C – declaration, definition, and call? Explain each with an example.

Answer: The components of a function in C are:

  • Function declaration: This declares the function name, return type, and parameters before the function is defined. It is required to tell the compiler in advance about the function. The syntax is:

return_type function_name(parameter1_type, parameter2_type);

For example: int sum(int num1, int num2);

  • Function call: The function call executes the function. It is used whenever we need to run the code inside the function. The syntax is:

function_name(argument1, argument2);

For example: sum(5, 3);

  • Function definition: The function definition contains the actual code statements that will execute when the function is called. It includes the function header, body, and a return statement.

return_type function_name(parameters){ // function body return value; }

3. What are the two types of functions in C – library functions and user-defined functions? Give examples of each.

Answer: There are two types of functions in C:

  • Library functions – defined in C header files like printf(), scanf()
  • User-defined functions – created by the programmer like calculateInterest(), findLargest()

4. How do you declare and define a function in C? What is the syntax?

Answer: To declare a function in C, we specify the return type, function name, and parameters. This informs the compiler about the function before it is defined and called.

For example: int add(int num1, int num2);

To define a function, we provide the function header (same as declaration), function body containing code statements, and a return statement.

For example: int add(int num1, int num2){
int sum = num1 + num2;
return sum;
}

5. What is the difference between functions with parameters and functions without parameters? Give examples.

Answer: Functions with parameters accept data when called. Functions without parameters do not accept any data.

6. How do you call a function and pass parameters to it in C?

Answer: To call a function: function_name(arguments);

Arguments passed must match parameter declaration.

7. What happens when a function is called in a C program? Explain with an example.

Answer: When a function is called, control goes from the caller to the function definition. Statements in the function execute and then control returns to caller.

8. What is a return value in a C function? How do you return a value from a function?

Answer: Return value is the data returned from a function to the caller. Use a return statement:

return value;

9. How are global and local variables used with functions in C? Explain with an example.

Answer: Global variables can be accessed by all functions. Local variables can be accessed only inside their function.

10. What is recursion? How can you write a recursive function in C? Give an example.

Answer: Recursion is when a function calls itself either directly or indirectly. This creates a repetitive execution flow.

For example, a factorial function can be defined recursively as:

int factorial(int n){ if(n == 0) //base case return 1;
else
return n * factorial(n-1); //function calls itself }

The function calls itself repeatedly until the base case is reached. Then the stack unwinds and returns the final result.

Recursion provides an alternative to iteration for solving problems with repetitive steps. But it can cause stack overflow if there is no base case.

11. How can functions help improve the readability and reusability of a C program? Explain with an example program.

Answer: Functions improve readability of a program by splitting the code into smaller logical parts each doing a specific task. For example, separate functions can be made for input, processing, and output.

Functions also improve reusability as the same function can be called multiple times. This avoids rewriting the same logic. For example, a ‘calculateSum()’ function can be called whenever we need to find the sum of numbers.

12. What are the similarities and differences between functions and procedures in C?

Answer: Similarities:

  • Both functions and procedures are reusable blocks of code.
  • Both can accept parameters and arguments.
  • Both use the same syntax for declaration and calling.

Differences:

  • Functions return a value, procedures do not.
  • Functions can return data back to the caller. Procedures cannot.

13. What is the purpose of a function prototype/declaration before the main() function?

Answer: The purpose of a function prototype or declaration before main() is to inform the compiler in advance about the function name, return type, and parameters before the function is defined and called. This allows the compiler to check for consistency.

14. How do you pass an array as a parameter to a function in C? Give an example.

Answer: To pass an array to a function, specify the array name without brackets in the function parameter. Inside the function, access elements using index.

For example:

void displayArray(int arr[], int size){ for(int i=0; i<size; i++){ printf(“%d “, arr[i]); } }

15. What are some common errors made while working with functions in C?

Answer: Some common errors are:

  • Passing wrong number of arguments
  • Not returning any value from function expected to return
  • Returning wrong data type
  • Infinite recursion causing stack overflow
  • Accessing local variables outside function scope
  • Redeclaring global variables as local variables

Get notes of other boards, classes, and subjects

NBSESEBA/AHSEC
NCERTTBSE
WBBSE/WBCHSEICSE/ISC
BSEM/COHSEMQuestion papers
Custom Notes ServiceYouTube

Share with others

Leave a Comment

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