Chapter 7 Book Question

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Chapter 7: Functions in C

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


Ans: 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.
A simple example of global variable can be showed by a very simple example given
as follows:-
// C program to show declaration of global variable
#include <stdio.h>

int x = 5; // global variable


int main() {

int y = 10; // local variable


return 0;
}
The global variables get defined outside any function- usually at the very
top of a program. They are easy to access than local variables and can be
accessed from anywhere in 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 : The syntax of the said so function declaration can be given by:-
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: 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).

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


function? Justify.
Answer discussed already in class.
2nd part of the question: 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.
But however in C++, its entirely possible as long as the two functions are having
the same name but different set of parameters. This is called function overloading.
Multiple functions are able to have the same name if you like, however MUST have
different parameters.
void func(int a, int b) { }
void func(float a, int b) { }
void func(int a, int b, int c) { }
Here we have overloaded function "func", and will all work within the same
program. When calling the function, the program will run the function which
corresponding to the arguements you pass through. For example, if you called
func(5.5, 6), it would run the second function.

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


program.
Ans: First part given in additional questions
2nd part

9. Define recursive function. Can we use a recursive function to solve all


kinds of problems?
Ans: already written.
10. Consider the below code and list all the syntax Consider the below code
and list all the syntax errors.
. 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);.

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:

You might also like