google.com, pub-4617457846989927, DIRECT, f08c47fec0942fa0 Learn to enjoy every minute of your life.Only I can change my life.: Functions based Qs and answer in C programming

Monday, July 28, 2014

Functions based Qs and answer in C programming

1. What is a function?
Answer:-
A function is a small segment of the program(sub program) designed to perform a specific task and return a result to the main or calling program.


2. What are the different types of functions supported in “C” language?
Answer:-
C supports two types of functions. They are
1. Built-in / Library functions
2. User defined functions



3. What are library functions?
Answer:-
Some of the operations are programmed and stored in C library so that they can be called in the program. These functions are called as library functions.
Eg : printf(), sqrt(), pow()


4. What is a user defined function?
Answer:-
A user-defined function is a complete and independent program unit, which can be used(or invoked) by the main program or by other sub-programs.



5. Mention the need for a function
Answer:-
If there are a number of statements that are repeatedly needed in the same program at different locations,then a function may be used.




6. What do you mean by a calling function?
Answer:-
Once the function is created it can be called from the main program or from any other function. This main program or the function which calls another function is called calling function.




7. What is a called function?
Answer:-
The user-defined function which is called by the main program or from any other function is known as called function.




8. What does the return-type-specifier of a function identify?
Answer:-
It identifies the type of value, which will be sent back after the function has performed its task (eg: int, float).




9. Why is the return type void used?
Answer:-
The return type void is used if the function does not return any value to the calling function.




10. What is an argument?
Answer:-
Information or values are passed to the function through special identifiers are called arguments.




11. What are actual parameters?
Answer:-
The arguments (values) which are passed to the function when a function call is made are called actual parameters.



12. What are formal parameters?
Answer:-
The arguments which are used in the argument list of the function header to receive the values from the calling program are called formal parameters or dummy parameters.




13. Give the syntax of a function call?
Answer:-
A function call has the following syntax:
variable = function_name(arg1,arg2…);
where arg1, arg2… are the actual parameters.




14. How is a function invoked?
Answer:-
A function is invoked(or called) through an output statement or through an assignment statement by using the function name followed by the list of arguments.Example: p = prod(x, y);



15. What is the use of the return statement?
Answer:-
The return statement is used in the body of the function which contains the value that has to be sent back to the main program.




16. Give the syntax of return statement?
Answer:-
The return statement can take one of the following forms return; Or return(expression);The first form does not return any value, where the second function returns the value of the expression.




17. What are local variables?
Answer:-
Variables declared inside a block or function are said to belong only to that block and these are called as local variables. Values of these variables are valid only in that block.




18. What are global variables?
Answer:-
Variables declared before the main function block are called global variables. Values of these variables are available in every block of that program.




19. What is function prototype?
Answer:-
A function must be declared before it is actually called. The declaration of a function is known as function prototype.




20. Give the syntax of a function prototype.
Answer:-
return-type function-name (type1, type2);
Ex: float power(float, int);




21. Define the term recursion.
Answer:-
A function which calls itself directly or indirectly again and again until a condition is satisfied is called as recursive function. This process is called recursion.




22. How do we pass an entire array to a function?
Answer:-
To pass an entire array to a called function only the array name without a subscript is used as argument(actual parameter).
Ex: void main( )
{
int a[100], n;
. . . . . . . . . .
sort(a, n); (where sort is the function name)
}




23 What is meant by scope of a variable?
Answer:-
1. The name of the variable.
2. The amount of space required to store that variable.
3. Address of the memory location.
4. The possible operations, which can be performed with the variable.




24. What are storage classes?
Answer:-
A storage class refers to the scope and life time of a variable i.e., the portion of program where the variable is valid and how long a variable can retain its value is defined
in the storage class.




25. What are the different types of storage classes?
Answer:-
1. Automatic storage class
2. External storage class
3. Static storage class
4. Register storage class



26 does Register variable have address?
Answer:-
No. (CPU registers do not have addresses).




27. Give the differences between library functions and user-defined functions.

Answer:-
Library functions :-
i. Some of the operations are programmed and stored in C library so that they can be called in the program. These functions are called as library functions.
ii. Library functions are in-built functions.

User-defined functions :-
i. Large programs can be broken down into smaller sub programs or modules. These sub programs are called user-defined functions.
ii. User-defined functions are created by the user.




28. What is a function call? What is the syntax of a function call?
Answer:-
To execute a function we will have to call the function. A function call establishes a link between the called function and the calling program.
A function call has the following syntax:
variable = Function_name (arg1, arg2…);
where arg1, arg2… are the actual parameters.




29. Why is the return statement is required in a function body?
Answer:-
When a function is called control is transferred from the calling function (main program) to the called function (sub-program). The statements of the called function are executed and finally the called function is terminated and sends the required result back to the calling function. The return statement is used to send the result from the function to the main program, thus terminating the called function.
The syntax is: return;
Or
return(expression);
The first form does not return any value, where the second function returns the value of the expression.




30. What is a function prototype? When is a function prototype necessary?
Answer:-
The declaration of a function before it is used is called as function prototype. A function prototype contains the data type of arguments sent as input to the function and data type of the result sent back from the function
Syntax: data_type function_name(data_type1,data_type2,….);
Example: float power(float, int);




31. Explain user defined function with an example
Answer:-
User defined function is a subprogram that is called by a main() function or any other function.
A user defined function may appear before or after the main() function. The general syntax of function definition is as follows:
Return_type_ specifier Function_name (type arg1, type arg2….)
{
local variable declarations; argument list
statement1;
statement2;
… Body of the function
statement n;
return (expression);
}
Return data type- indicates the data type of variable sent back from called
function to calling function
Function name- indicates the name of the function .It is an identifier
Argument list- list of input variables and their data-types received from calling
function
Local variable declaration-list of data that required only for the function block in which they are declared.
Body of the function-It includes declaration part (local variables) and a set
of executable statements which will perform the required task.
Return statement- indicates termination of function and transfer of control from called
function to calling function
Example:
int sum(int a,int b)
{
int c;
c=a+b;
return(c);
}




32. Mention different types of functions.
Answer:-
The different types of functions are
1. functions with no input and no return value
2. functions with input and no return value
3. Functions with input and return value
4. Recursive functions




33. What are storage classes? Mention different types of storage classes. Explain
Answer:-
A storage class refers to the scope of data in a program. Scope of data means the portion of the program in which the variable is valid(mian() as well as sub
programs) and lifetime of the variable.i.e., the length of time to which the variable
retains its value.

A variable must belong to any one of the following storage classes.
1. Automatic storage class
2. External storage class
3. Register storage class
4. Static storage class

Automatic storage class :-
Automatic storage class is similar to local variable declaration
Key word auto precedes the declaration
Ex:
auto int x,y,z;
• Automatic variables are local variables
• The variables are stored in memory(RAM)
• The key word auto is implicit(it means even without the word auto the
variable is automatic)
• Initial value stored is junk

External storage class :-
External storage class is similar to global variables.
Keyword extern precedes the declaration
Ex:
Extern int m,n;
• Scope of the variable is global
• The variables are stored in memory
• Needs explicit(must use the keyword)declaration
• Initial value stored is junk

Register storage class :-
Register storage class is used when data is needed in cpu registers
Key word register must precede the declaration
Ex:
register int x,y;
• Scope of the variable is local
• The variables are stored in registers
• Needs explicit declaration
• Initial value is not known

Static storage class:-
Static storage class is used in function blocks. The static variable can be initialized exactly once when the function is called for the first time. For subsequent calls to the same function, the static variable stores the most recent value.
Key word static precedes the variable declaration
Ex: static int x,y,z;
• Scope of the variable is local
• The variables are stored in memory
• Needs explicit declaration
• Default Initial value is stored is 0.


No comments:

Post a Comment

रामायण

रामायण दशरथ की तीन पत्नियाँ – कौशल्या, सुमित्रा , कैकेयी दशरथ के चार पुत्र – राम,लक्ष्मण,भरत,शत्रुघ्न दशरथ: राम के पिता और कौशल के राजा कौशल...