Functions

1. What is a function?
Ans:
A function is a set of instructions, which performs a specific task when it is called.

eg: printf() --> Performs the task of printing something on the screen
scanf() --> Performs the task of reading something from the keyboard
exit() --> exits the program
rand() --> returns a random number
strcmp() --> compares two strings and returns a value
bubbleSort() ( userdefined ) -> sorts an integer array
binarySearch() ( userdefined ) --> searches for an elements using binary search algorithm
push() ( userdefined)--> pushes an element into a stack  etc

2. What is the benefit of writing a piece of code as a separate function?
Ans:a)Avoids code repetition.
b)modular programming can be achieved.
c)easy to debug.

3. When does the control return from a function
Ans:
There are two cases:
a) When the control reaches to the end of the function i.e., closing brace of the function.
b) When a return statement gets executed.

4. How many values can be returned from a function?
Ans:
Only one value directly. However, indirectly any number of values can be returned from a function.

5. What is a function prototype, why is it required?
Ans:
A function’s prototype contains:

Its Name
Its Parameters list
Its Return type

6. What is the advantage of dividing a program into separate files?
Ans:
Better organization.  Suppose that different files contain different set of functions. Suppose that a set of functions from a file is required in some other program. It is sufficient to just include that file in a required file.

7. What is the default return type of a function?
Ans:
Integer

8. From where can a function be called?
Ans:
Local Vs STD Vs ISD

Local : Recursion
STD    : Same file
ISD         : External file

9. What is the best place for function prototype?
Ans:
At the top of a program outside all the functions so that it can be called from any function in any file

10. What happens when we return a value from a function whose return type is void?
Ans:
Error

11. What happens when we don’t return a value from a non-void return type function?
Ans:
Compiler dependent:
Turbo C -> the number of arguments it receives
Turbo C++ --> same
GCC (Linux) --> same
VC++  --> Error

12. What happens when we don’t catch the returned value from a function?
Ans:
Turbo C --> No problem
Turbo C++ --> same
GCC --> same
VC++ --> same

13. What are actual arguments/formal arguments/parameters?
Ans:
Actual arguments:

The values which are passed into a function at its calling statement :
The actual argument may be a:
Variable Eg: fun(a)
Constant Eg: fun(10 )
Expression Eg: fun (a+b, c, d*e)
Address Eg: fun (&a)

Formal arguments/ parameters

The variables in the parameters list section of a function’s body

14.What is the advantage of unformatted console I/O functions?
Ans:
They are more efficient when compared to equivalent formatted counterparts because:

They do not have the overhead of formatting the input or output.

Formatted I/O functions unformatted I/O functions

printf(“%d\n%.2f\nHello – %-20s”, ….), puts()
scanf(“%[ ^\n]”, ….) gets()
scanf(“%20s”, …) getchar()

15. What are the different storage classes in C ?
Ans:There are four types of storage classes in C. They are extern, register, auto and static

16. What does static variable mean?
Ans:Static is an access qualifier. If a variable is declared as static inside a function, the scope is limited to the function,but it will exists for the life time of the program. Values will be persisted between successive
calls to a function

17. What is scope of a variable?
Ans:The area or the part in which a variable is accessible.

18. What is the purpose of register storage class?
Ans:Execution speed may increase

19. What is recursion ?
Ans:Recursion : calling a function from itself is called as recursion.

20. Difference between pass by reference and pass by value?
Ans:Pass by value just passes the value from caller to calling function so the called function cannot modify the values in caller function. But Pass by reference will pass the address to the caller function instead of value if called function requires to modify any value it can directly modify.