Chapter 7 Book Question
Chapter 7 Book Question
Chapter 7 Book Question
3. Write the code segment for the function definition of the above function
calculateAge ().
Ans: The Function definition for the above function can be given by:-
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: already written in additional questions.
5. Differentiate between caller and callee functions. Write a small C program
and identify the caller and callee function in it.
Ans: A caller function is a function that calls another function and a callee function
is a function that was called. A simple c program to demonstrate it is given as:
#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 the above 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).
11. Consider the code segment below and find out the output if the user
enters 5 from the keyboard when asked for .
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.
Ans:
The output will be:
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.
Ans: The required code can be given by:
#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;
}
14. Write a C program and define a recursive function to find the summation
of first N natural numbers.
Ans: Already done .
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.
Ans: