1. OCS752 − INTRODUCTION TO C PROGRAMMING VII SemesterEEE
Dept. of CSE Dhanalakshmi College of Engineering 1
DHANALAKSHMI COLLEGE OF ENGINEERING
Tambaram, Chennai
Department of Computer Science and Engineering
OCS752 INTRODUCTION TO C PROGRAMMING
Year / Sem : IV / VII
2 Marks Q & A
2. OCS752 − INTRODUCTION TO C PROGRAMMING VII SemesterEEE
Dept. of CSE Dhanalakshmi College of Engineering 2
UNIT IV
FUNCTIONS
Introduction to Functions – Types: User-defined and built-in functions – Function prototype – Function
definition – Function call – Parameter passing: Pass by value – Pass by reference – Built-in functions
(string functions) – Recursive functions – Exercise programs: Calculate the total amount of power
consumed by ‘n’ devices (passing an array to a function) – Menu-driven program to count the numbers
which are divisible by 3, 5 and by both (passing an array to a function) – Replace the punctuations
from a given sentence by the space character (passing an array to a function)
PART – A
1. What is a function? (N/D – 14, N/D – 16, N/D – 19)
A function is a group of statements that together perform a task. The general form of a function
definition in C language:
return_type function_name( parameter list )
{
body of the function
}
2. Specify the advantages of function. (A/M – 16, A/M – 19)
Advantages of function
1) The program will be easier to understand, maintain and debug.
2) Reusable codes that can be used in other programs
3) A large program can be divided into smaller modules. Hence, a large project can be divided
among many programmers.
3. What does a function header and function body consist of?
A function definition consists of
1) Function header
2) Function body
The Function header consists of
1) Return Type
2) Function Name
3) Parameters
The Function body consists of
Declarations and statements necessary for performing the required task.
4. What is a recursive function?
A Function calls itself again and again, then that function is called Recursive function. This
technique is known as recursion.
3. OCS752 − INTRODUCTION TO C PROGRAMMING VII SemesterEEE
Dept. of CSE Dhanalakshmi College of Engineering 3
Example:
void recursion()
{ recursion(); /* function calls itself */
}
int main()
{
recursion();
}
5. Differentiate pass by value from pass by reference. (A/M – 14, N/D – 17)
S. No. Pass by value Pass by reference
1 A copy of actual arguments is passed to
formal arguments of the called function.
The location (address) of actual arguments is
passed to formal arguments of the called
function.
2 Actual arguments will remain safe and
they cannot be modified accidentally.
Alteration to actual arguments is possible
within the called function.
6. List the advantages of recursion. (A/M – 18)
Advantages of recursion
1) Reduce unnecessary calling of function.
2) Through recursion one can solve problems in easy way, while its iterative solution is very big
and complex.
7. What are actual parameters and formal parameters? (A/M – 15)
Functions can take two kinds of parameters.
1) Actual parameter
2) Formal parameter
Actual parameters are parameters as they appear in function calls. Formal parameters are parameters
as they appear in function declarations.
8. Write a program to print the first 50 prime numbers recursively. (N/D – 15)
Program to print the first 50 prime numbers recursively
#include<stdio.h>
void main ( )
{
intn,i=3, j, c;
scanf("%d",&n);
for(i=3;i<=n;i++)
{
4. OCS752 − INTRODUCTION TO C PROGRAMMING VII SemesterEEE
Dept. of CSE Dhanalakshmi College of Engineering 4
c=0;
{
for (j=1;j<=i;j++)
if(i%j==0)
c++;
}
if(c==2)
{
printf("%d",i);
}}}
Output:
50
357111317192329313739414347
9. What are the components of a function? (A/M – 17)
Components of a function
1) Function declaration
2) Function definition
3) Function call
10. What are the steps in writing a function in a program? (N/D – 19)
Steps in writing a function in a program
1) Function Declaration (Prototype declaration): Every user-defined function has to be declared
before the main ().
2) Function Callings: The user-defined functions can be called inside any function like main(),
user-defined function, etc.
3) Function Definition: Once a function is declared and defined, it can be called any number of
times, from any function.
11. Is it better to use a macro or a function? (N/D – 19)
Macros are more efficient than function because their corresponding code is inserted directly at the
point where the macro is called. Macros are generally small and cannot handle large, complex
coding constructs. Functions are used for large, complex coding constructs.
12. Is main() a library function in C?
The function main() is not a predefined or built in function. It is a user defined function with a
predefined function prototype (also called function signature). It is neither user defined nor a built in
library function.
5. OCS752 − INTRODUCTION TO C PROGRAMMING VII SemesterEEE
Dept. of CSE Dhanalakshmi College of Engineering 5
13. Distinguish between exit ( ) and return in C.
S. No. exit() return
1) exit() is a system call (not a language
statement) that terminates the current
process.
return is an instruction of the language
that returns from a function call.
2) #include <stdio.h>
void f()
{
printf("Executing fn");
exit(0);
}
int main()
{
f();
printf("Back from fn");
}
#include <stdio.h>
void f()
{
printf("Executing fn");
return;
}
int main()
{
f();
printf("Back from fn");
}
14. How to generate random numbers in C?
rand() function is used in C to generate random numbers. If we generate a sequence of random
number with rand() function, it will create the same sequence again and again every time program
runs.
15. Classify the functions based on arguments and return values.
Depending on the arguments and return values, functions are classified into four types.
1) Function without arguments and return values.
2) Function with arguments but without return values.
3) Function without arguments but with return values.
4) Function with arguments and return values.
16. List the types of functions in C programming.
Depending on whether a function is defined by the user or already included in C compilers,
there are two types of functions in C programming
1) Standard library functions
2) User defined functions
17. List the advantages of functions. (A/M – 14)
Advantages of functions
6. OCS752 − INTRODUCTION TO C PROGRAMMING VII SemesterEEE
Dept. of CSE Dhanalakshmi College of Engineering 6
1) Functions are self-contained block or sub program of one or more statements that performs a
specific task.
2) It increases the modularity, reusability of a program.
18. List the advantages of user-defined function.
Advantages of user defined functions
1) The program will be easier to understand, maintain and debug.
2) Reusable codes that can be used in other programs
3) A large program can be divided into smaller modules. A large project can be divided among
many programmers.
19. What are standard library functions?
The standard library functions are built-in functions in C programming to handle tasks such as
mathematical computations, I/O processing, string handling. These functions are defined in the
header file.
20. What is function prototyping? Why it is necessary? (A/M – 11)
Many built in functions can be used in C programs. The prototype of these functions is given in the
respective header files. With the help of a function prototype, the compiler can automatically
perform type checking on the definition of the function, which saves the time to delay the program.