C Programming Question With Answers
- Posted by Engineering Helpline Pvt. Ltd
- Categories c programming, C Programming Question With Answers, First Semester
- Date April 1, 2018
- Comments 0 comment
71. What happens to the statements written after return statement?
The statements after the return statement will not execute. Since the return statement sends the control back to the calling function, the control never reaches the statements written after return
statement.
72.What does return type symbolizes in function definition and declaration? How is it related to return statement?
Return type in function declaration and definition symbolizes whether the function returns a value to the calling function or not. If the return type is void, it represents function doesn’t return a value while other data types such as int, float, char in return type represent that the function returns a value.
The return statement can be avoided if the return type is void. But if the return type in not void then in return statement, the expression to be returned must be of same type to that of the return type of the function.
73.What difference can be observed in arguments portion of function declaration, function call and function definition?
The difference in arguments that can be seen is
•In function declaration: only data type of variables is written
•In function call: only the name of variables is written
•In function definition: data type along with name of variables is written
74. What does function declaration suggest? How are function called?
Function declaration provides the following information.
•Name of the function, generally, defines what its function is.
•Return type symbolizes whether the function returns a value after processing.
•List of argument or parameter represents what type of and how many values are required by the function to operate.
39
Functions are called with their name followed by the list of parameters or arguments. Function call must be used or assigned if the returned value should be taken into considerations. But if the function does not return any value or it returns a value which is not of much use then the function call need not be assigned to or used.
For example, simply calling power function which returns a value does not make any sense. pow(x,y);
But if it is assigned to a variable or used in some calculations then only it gives some meaning. z = pow(x,y);
z = a * pow(x,y)/b;
printf(“x to the power y is %f”, pow(x,y));
75.“The number and type of arguments must be same in function declaration, function call and function definition”, clarify the statement.
The number and type of arguments must be same in function declaration, function call and function definition; this statement can be clarified using following demonstration.
Let us take an example to calculate the largest number of three numbers, then Function declaration: int large(int, int, int);
Function call: l = large(a, b, c);
Function Definition: int large( int x, int y, int z) { }
Since, the type and number of arguments match in function declaration, call and definition, there would be no error. But if the number of arguments is changed, let us say in function call; l = large(a,b), then the compiler would give error. Similarly if the type of either variable a, b, c is not integer then again, it would result some kind of error. Hence, the number and type of arguments must be same in function declaration, function call and function definition.
76.Is it mandatory to have function declaration? “Any entity must be declared first before it is used”, explain the statement.
Function declaration can be omitted, if the function definition is written before its call. So function declaration is not mandatory.
Every entity in C programming must be declared before it is used. Declaration gives information not only to the user but also to the compiler about the type and name of the entity we are using. With such information, the compiler will interpret statements correctly. Also the compiler can decide how much storage space to allocate. Hence, every entity such as variables, functions must be declared first prior to its use. For example, without declaration, statement sum = num1 + num2 can represent
40
sum of integers or sum of float numbers or even combining of characters or strings. This can cause problem to compiler.
77. Describe different types of functions based on return type and arguments.
Based on return type and argument, the function can be categorized into four types. The description is based on where the input, processing and output will take place.
•Function with no arguments and no return type: This kind of function can have no information exchange with other functions unless global variables are used. Generally, all the operations such as input, processing and output are done within the function.
▪General form:
void function_name(void);
▪Example:
clrscr(); is used to clear the screen with no arguments and no return type.
•Function with no arguments but with return type: In this kind of function, the inputs along with the processing, if required, will be done within the function but the result will be returned to the called function.
▪General form:
return_type function_name(void); where return type is not void
▪Example:
ch = getchar();
•Function with arguments but with no return type: Such functions take input values in the form of arguments, but processing and output part of the problem will be within the function.
▪General form:
void function_name(argument list);
▪Example:
putchar(ch);
•Function with arguments and return type: This function, usually, contains only the processing part. The input and output of the problem will be in the calling function. The inputs will be passed as arguments while the result will be returned through return statement of function.
▪General form:
Return_type function_name(argument list); where return type is not void
41
▪Example:
sq = sqrt(num);
78.What are library functions? How it differs from user defined functions?
Library functions are inbuilt functions which consist of line of codes to carry out particular operation. Library functions are declared in header files, so inclusion of corresponding header file is a must whenever the library functions are used.
The difference between library function and user defined function are listed below.
|
SN
|
Library Function
|
User Defined Function
|
|
1.
|
Inbuilt functions for specific purpose
|
Defined by user based on problem specified
|
|
2.
|
Requires header file to be included
|
Header file not required
|
|
3.
|
Definition is already written
|
Definition should be written by the user
|
|
4.
|
It cannot be modified
|
It can be modified as required
|
|
5.
|
clrscr(), getchar(), scanf(), pow() etc are
|
Functions not defined in library such as sum(),
|
|
examples of library functions
|
prime() etc are few examples
|
|
79. What do you mean by passing reference? When are they used?
Passing reference in a function means to pass the address of the variable rather than its value. When a function needs to return more than one value, which it cannot do through the return
statement, then in such situations passing reference are used.
80. Differentiate between pass by value and pass by reference.
The differences between pass by value and pass by reference can be summarized as:
|
SN
|
Pass by value
|
Pass by reference
|
|
1.
|
Value of variable is passed
|
Address of variable is passed
|
|
2.
|
Changes of formal arguments in called
|
Any changes made in formal arguments in the
|
|
function does not the value of actual
|
called function changes the value of actual
|
|
|
arguments in calling function
|
arguments in calling function
|
|
|
3.
|
Return statement should be used to
|
Return statement is not required in this case
|
|
return value
|
but can be used
|
|
|
4.
|
Only one value can be returned to the
|
More than one value can be returned to the
|
|
calling function
|
calling function
|
|
|
5.
|
Pointer is not required
|
Pointer is required since it is used to hold the
|
|
address of the variable
|
||
|
42
|
|
6.
|
Used when no values are to be returned
|
Generally used when function needs to return
|
|
or single value is needed to return to the
|
more than one value.
|
|
|
calling function
|
||
7.Example to demonstrate pass by value and pass by reference: void check(int, int*);
void main();
{
int x = 2, y = 5; check(x, &y);
printf(“The final value of x is %d and y is %d”, x, y); getch();
}
void check(int a, int *b)
{
a = a+ 3; *b = *b + 2;
}
Here in this program, value of x is passed where address of y is passed. Variable a holds the value of x while pointer variable b holds the address of y. So, change in variable b represents change in variable y of main function but change in variable a does not change value of x in main function. Hence, the final value of x is 2 but the value of y is 7.
You may also like
Applied mechanis And Its Solutions
6 January, 2020
c programming notes
17 April, 2019
Structure: Array VS Structure: Pointer and Structure:
4 February, 2019
A structure is a collection of variable (of different data types) referenced under one name, providing of convenient means of keeping related information together. A structure definition forms a template that may be used to create structure variable. The general …
