Questions on Functions in C Language

Questions on Functions in C Language

1. What is a function in C language?

A function is a group of statements which are collectively stated under one entity or one name, i.e. function name. A function contains many statements that need to be operated on. It can perform any operation like addition, subtraction or print certain statements or perform any logical operation etc.
C program does not execute the functions directly. It is required to invoke or call that functions. When a function is called in a program then program control goes to the function body. Then, it executes the statements which are involved in a function body. Therefore, it is possible to call function whenever we want to process that functions statements.

2. What are the different features provided by Functions in C language?

Functions Provides us Following Features,
Reusability of Code: Means once a code has developed then we can use that code any time.
Remove Redundancy: Means a user doesn’t need to write code again and again.
Decrease Complexity: Means a Large program will be Stored in the two or more functions. So that this will makes easy for a user to understand that code.

3. What is the need of Functions in C language?

As we all know C is procedure oriented programming language and procedure or functions is like the building block of a C program. The entire C program is built with the help of many functions so that it becomes easy for everyone to understand it. The complexity or the difficulty of the program is even decreased since the program is divided into many modules or functions. The detection of errors is even simpler as we can track the error easily.

4. What are the advantages of Functions in C language?

The advantages of functions are as follows,
•    It is easy to use.
•    Debugging is more suitable for programs.
•    It reduces the size of a program.
•    It is easy to understand the actual logic of a program.
•    Highly suited in case of large programs.
•    By using functions in a program, it is possible to construct modular and structured programs.

5. When should I declare a function?

Functions that are used only in the current source file should be declared as static, and the function's declaration should appear in the current source file along with the definition of the function. Functions used outside of the current source file should have their declarations put in a header file, which can be included in whatever source file is going to use that function.

6. Why should I prototype a function?

A function prototype tells the compiler what kind of arguments a function is looking to receive and what kind of return value a function is going to give back. This approach helps the compiler ensure that calls to a function are made correctly and that no erroneous type conversions are taking place. For instance, consider the following prototype:
int some_func (int, char*, long);
Looking at this prototype, the compiler can check all references (including the definition of some_func ()) to ensure that three parameters are used (an integer, a character pointer, and then a long integer) and that a return value of type integer is received. If the compiler finds differences between the prototype and calls to the function or the definition of the function, an error or a warning can be generated to avoid errors in your source code.

7. How many parameters should a function have?

There is no set number or "guideline" limit to the number of parameters your functions can have. However, it is considered bad programming style for your functions to contain an inordinately high (eight or more) number of parameters. The number of parameters a function has also directly affects the speed at which it is called—the more parameters, the slower the function call. Therefore, if possible, you should minimize the number of parameters you use in a function. If you are using more than four parameters, you might want to rethink your function design and calling conventions.
One technique that can be helpful if you find yourself with a large number of function parameters is to put your function parameters in a structure. Generally, you should keep your functions small and focused, with as few parameters as possible to help with execution speed. If you find yourself writing lengthy functions with many parameters, maybe you should rethink your function design or consider using the structure-passing technique presented here. Additionally, keeping your functions small and focused will help when you are trying to isolate and fix bugs in your programs.

8. What are the different types of functions in C language?

Functions are of two types, they are
•    Built in function or Library Functions
•    User defined functions

9. What are built in Functions in C language?

Built in functions are the functions that are provided by C library. Many activities in C are carried out using library functions. These functions perform file access, mathematical computations, graphics, memory management etc. A library function is accessed simply by writing the function name, followed by an optional list of arguments and header file of used function should be included with the program.
Definition of built in functions are defined in a special header file. A header file can contain definition of more than one library function but the same function cannot be defined in two header files. These functions are stored in library files. Ex:

•    scanf()
•    printf()
•    strcpy
•    strlwr
•    strcmp
•    strlen
•    strcat

10. What is string handling functions?

The different string handling functions are,
string.h: String functions
strcat (): concatenates a copy of str2 to str1.
strcmp (): compares two strings.
strcpy (): copy contents of str2 to str1.
memset (): Initialize Memory Block
strerror (): Convert Error Number to String
strlen (): String Length

11. What are text input/output functions?

The different text I/O functions are,
stdio.h: I/O functions:
getchar () : returns the next character typed on the keyboard.
putchar () :outputs a single character to the screen.
printf () :to do input.
pcanf () :for output.
ferror(): Test for File Error
perror(): Print Error Message
vfprintf (): Formatted File Write Using Variable Argument List
vprintf (): Formatted Write Using Variable Argument List
vsprintf (): Formatted String Write Using Variable Argument List.

12. What are time related functions?

Time functions in C are used to interact with system time routine and formatted time outputs are displayed. The different time related functions are,
time.h: Time and Date functions
time () returns current calendar time of system
difftime () returns difference in secs between two times
clock () returns number of system clock cycles since program execution
setdate ():This function used to modify the system date
getdate():This function is used to get the CPU time.

13. What are miscellaneous functions?

The different miscellaneous functions are,
stdlib.h: Miscellaneous functions
malloc () provides dynamic memory allocation, covered in future sections
srand () used to set the starting point for rand()
exit (): Exit from Program
atof (): Convert String to Floating-Point
atoi (): Convert String to Integer

14. What are int, char validation functions?

There are many inbuilt functions in C language which are used to validate the data type of given variable and to convert upper to lower case and lower to upper case. “ctype.h” header file support all the below functions in C language.
ctype.h: Character functions
isdigit (): returns non-0 if arg is digit 0 to 9
isalpha (): returns non-0 if arg is a letter of the alphabet
isalnum (): returns non-0 if arg is a letter or digit
islower (): returns non-0 if arg is lowercase letter
isupper (): returns non-0 if arg is uppercase letter
tolower():checks whether character is alphabetic & converts to lower case
toupper():checks whether character is alphabetic & converts to upper case

15. What are arithmetic functions?

C functions which are used to perform mathematical operations in a program are called Arithmetic functions. “math.h” and “stdlib.h” header files support all the arithmetic functions in C language. All the arithmetic functions used in C language are given below.
math.h: Mathematics functions
acos () returns arc cosine of arg
asin () returns arc sine of arg
atan () returns arc tangent of arg
cos () returns cosine of arg
exp () returns natural logarithm e
fabs () returns absolute value of num
sqrt () returns square root of num


Post a Comment