C Language Interview Questions Part 6

C Language Functions Interview Questions

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 are 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

16. How would you use the functions randomize () and random ()?

randomize ():initiates random number generation with a random value.

random ():generates random number between 0 and n-1;

17. What is the difference between the functions memmove () and memcpy ()?

The arguments of memmove () can overlap in memory. The arguments of memcpy () cannot.

18. Difference between linker and linkage?

Linker converts an object code into an executable code by linking together the necessary built in functions. The form and place of declaration where the variable is declared in a program determine the linkage of variable.

19. What is the purpose of main () function?

The main () function in C is the most vital part of a program. The program execution occurs from the main () function.

The main () function may contain any number of statements and they are sequentially executed. main () function can in turn call other functions.

20. What is friend function?

The function declaration should be preceded by the keyword friend. The function definitions do not use either the keyword or the scope operator (::).

The functions that are declared with the keyword friend as friend function. Thus, a friend function is an ordinary function or a member of another class.

21. What are user defined functions in C language?

C provides programmer to define their own function according to their requirement known as user defined functions.

Means except built in functions user can also define and write small programs as functions to do a task relevant to their programs, there functions should be codified by the user, so that such functions can perform the task as desired by user.

Suppose, a programmer wants to find factorial of a number and check whether it is prime or not in same program. Then, he/she can create two separate user-defined functions in that program: one for finding factorial and other for checking whether it is prime or not.

22. What are the advantages of user defined functions?

Advantages of user defined functions are,

• User defined functions helps to decompose the large program into small segments which makes programmer easy to understand, maintain and debug.
• If repeated code occurs in a program. Function can be used to include those codes and execute when needed by calling that function.
• Programmer working on large project can divide the workload by making different functions.

23. What is the return type of a function?

The Return Function determines whether a Function will return any value to the Function.

If a Function is declared with the void Keyword or if a Function Contains a void then that’s means a Function Never Returns a value.

Means a Function will Executes his statements one by one. And if a Function Contain any other data type means if a Function Contains int or float then the Function must return a value to the user.

24. What is argument list?

A Function may have zero or More Arguments. So that if we want to call a Function. Then we must have to Supply Some Arguments or we must have to pass some values those are also called as the Argument List.

So that The Argument List is the total Number of Arguments or the Parameters those a Function Will takes. So that we must have to supply some arguments to the Function.

The Arguments those are used by the Function Calling are known as the Actual Arguments and the Arguments those are used in the Function declaration are Known as the Formal Arguments, When we call any Function then the Actual Arguments will Match the Formal Arguments and if a proper Match is Found, then this will Executes the Statements of the Function otherwise this will gives you an error Message.

25. What are the two ways of calling a function?

The function call is made as follows:

return_type = function_name (arguments);

There are Two Ways for Calling a Function,they are

•    Call by value
•    Call by reference

26. What is Call by value?

Call by Value: when we call a Function and if a function can accept the Arguments from the Called Function, Then we must have to Supply some Arguments to the Function. So that the Arguments those are passed to that function just contains the values from the variables but not an Actual Address of the variable.

So that generally when we call a Function then we will just pass the variables or the Arguments and we doesn’t Pass the Address of Variables , So that the function will never effects on the Values or on the variables. So Call by value is just the Concept in which you must have to Remember that the values those are Passed to the Functions will never effect the Actual Values those are Stored into the variables.

27. What is Call by reference?

Call By Reference: When a function is called by the reference then the values those are passed in the calling functions are affected when they are passed by Reference Means they change their value when they passed by the References.

In the Call by Reference we pass the Address of the variables whose Arguments are also Send. So that when we use the Reference then, we pass the Address the Variables.

When we pass the Address of variables to the Arguments then a Function may effect on the Variables. Means When a Function will Change the Values then the values of Variables gets Automatically Changed. And When a Function performs Some Operation on the Passed values, then this will also effect on the Actual Values.

28. What is the difference between Call by value and Call by reference?

When using Call by Value, you are sending the value of a variable as parameter to a function, whereas Call by Reference sends the address of the variable. Also, under Call by Value, the value in the parameter is not affected by whatever operation that takes place, while in the case of Call by Reference, values can be affected by the process within the function.

29. What are the different types of functions depending on return type and arguments?

There are four types of functions depending on the return type and arguments:

•    Functions that take nothing as argument and return nothing.
•    Functions that take arguments but return nothing.
•    Functions that do not take arguments but return something.
•    Functions that take arguments and return something.

A function that returns nothing must have the return type “void”. If nothing is specified then the return type is considered as “int”.

30. What are the Functions with no arguments and no return value?

A C function without any arguments means you cannot pass data (values like int char etc) to the called function.
Similarly, function with no return type does not pass back data to the calling function. It is one of the simplest types of function in C.

This type of function which does not return any value cannot be used in an expression it can be used only as independent statement.

31. What are the Functions with arguments and no return value?

A C function with arguments can perform much better than previous function type. This type of function can accept data from calling function. In other words, you send data to the called function from calling function but you cannot send result data back to the calling function. Rather, it displays the result on the terminal. But we can control the output of function by providing various values as arguments.

32. What are the Functions with arguments and return value?

This type of function can send arguments (data) from the calling function to the called function and wait for the result to be returned back from the called function back to the calling function.

And this type of function is mostly used in programming world because it can do two way communications; it can accept data as arguments as well as can send back data as return value.

The data returned by the function can be used later in our program for further calculations.

33. What are the Functions with no arguments but return value?

We may need a function which does not take any argument but only returns values to the calling function then this type of function is useful.

The best example of this type of function is “getchar ()” library function which is declared in the header file “stdio.h”. We can declare a similar library function of own.

34. What is a recursive function?
A function that calls itself is known as recursive function and the process of calling function itself is known as recursion in C programming.

But while using recursion, programmers need to be careful to define an exit condition from the function; otherwise it will go in infinite loop.

Recursive functions are very useful to solve many mathematical problems like to calculate factorial of a number, generating Fibonacci series etc.

35. What are the advantages and disadvantages of recursion?
Recursion is more elegant and requires few variables which make program clean. Recursion can be used to replace complex nesting code by dividing the problem into same problem of its sub-type.

In other hand, it is hard to think the logic of a recursive function. It is also difficult to debug the code containing recursion.

36. What do you mean by inline function?
The idea behind inline functions is to insert the code of a called function at the point where the function is called.
If done carefully, this can improve the application’s performance in exchange for increased compile time and possibly (but not always) an increase in the size of the generated binary executables.


Post a Comment