Sunday, 17 December 2017

Function

Ques 1) Why function prototype declaration is needed ?

Ans--> Function prototype declaration is necessary in order to provide information to the compiler about the function, about its return type and argument list so that during function calling compiler looks forward in the program for the function definition and also helps the compiler to check whether the value being passed and returned are as per the prototype declaration.

Ques 2) What is function ?

Ans--> A function is a complete block of statement that performs logical task of some kind.

Ques 3) What is built in function ?

Ans--> Built in or inbuilt function are that type of function which are already defined or created in program or in programming framework.

Ques 4) What is standard library function ?

Ans--> Standard library function are the built in function in C programming to handle tasks such as mathematical computation, I/O processing, string handling etc.

Ques 5) Why should we use function ?

Ans--> i) CODE REUSABILITY : Writing function prevent rewriting the same code over and over.

ii) CODE OPTIMIZATION : Makes program easier to design and understand.

Ques 6) What is function declaration ?

Ans--> Function declaration tells the compiler about the function name, parameter list and return type and how to call the function.

Ques 7) What is Argument ?

Ans--> Arguments in context with function are the actual value that is passed to the function when it is called.

Ques 8) What is difference between Argument and parameter ?

Ans-->Argument in context to function are the actual values that are passed to the function when it is called.
                                   whereas, parameter refers to the variable that are used in function declaration/definition to represent those argument that were send to the function during function call. 

Ques 9)  What is the difference between actual and formal argument ?

Ans-->The calling function passes the information to called function in "actual argument".
               whereas, the called function access those information using corresponding "formal arguments".

Ques 10) What are the purposes served by return statement ?

Ans--> i) On executing return statement, it immediately transfer the control back to the calling function.

ii) It returns the value present in the parenthesis after return to the calling function.

Ques 11) What are the things that calling convention indicate ?

Ans--> i) The order in which argument are passed to the function.

ii) which function performs clean up of variable when control returns from the function.

Ques 12) What are the two ways in which argument is passed to the function ?

Ans--> Arguments are passed to a function in 2 ways :-

i) Call by value : This method copies the actual value of the argument into the formal parameter of function.

ii) Call by reference : This method copies the address of an argument into formal parameter of the function.

Ques 13) What is difference between call by value and call by reference ?

Ans--> i) Call by value method copies the actual value of an argument into formal parameter of a function.
                                   Call by reference method copies the address of an argument into formal parameter of a function.

ii) In call by value, changes made to the formal argument doesn't reflect to actual argument.
          In call by reference, the changes made to formal argument is also reflected to actual argument.

iii) In call by value, actual and formal arguments will be created indifferent memory location.
       In call by reference, actual and formal arguments will be created in same memory location.

Ques 14) What happen if one doesn't specify the function prototype ?

Ans--> If we don't specify the function prototype then--

i) compiler don't have any reference of function.

ii) don't have any prior information of that function.

iii) Compiler get confused and interpret it as unknown reference and throws error.

Ques 15) When does the control return from a function ?

Ans--> i) when control reaches the end of the function.

ii) when return statement is encountered.

Ques 16) How many values are returned from a function ?

Ans--> Only one value can be returned directly. However, indirectly any number of values can be returned from function.

Ques 17) What is the best place for function prototype ?

Ans--> At the top of program outside the main function so that it can be called from any function at any time.

Ques 18) How can we pass an array to a function by value ?

Ans--> An array can be passed to a function by value by declaring in called function, the array name with square brackets when calling the function, simply passing the address of array to called function.

Ques 19) How can you pass variable number of argument to a function ?

Ans--> To create a function that accepts the variable number of arguments, C has header file by name <stdarg.h> that contains library functions to accept variable number of arguments. It defines the variable of type va_list and contain 3 macros that can be used to get the arguments in the function when the number of arguments is not known.

void va_start(va_list ap, last arg): It initializes the ap variable to be used with va_arg and va_end.

type va_arg( va_list ap, type): It returns next argument in the parameter list of a function of type type.

void va_end( va_list ap) : This macros allows function with variable number  of argument which used va_start macro to return. If va_end is not called before returning from the function then the result is invalid.

Ques 20) How to return more than one value from a function ?

Ans--> i) By using pointer.
           ii) By using Dynamic Memory Allocation.
           iii) By using Dynamic Array.



                                                                      
                                                


  

                      


No comments:

Post a Comment

Featured Post

TCS Latest Test Patern ( For On-Campus Drives )

Tata Consultancy Services is software services consulting company whose  headquarter is situated in Mumbai, India. It is the Asia’s larges...