Interview Questions on C - Functions
1. What is a Function?
A set of statements which performs a particular task.
Syntax :-
return_type function_name(datatype argument1){
}
Ex:-
int sumofdigits(int n){
int sum =0;
while(n>0){
sum+= n%10;
n+=n/10;
}
return sum;
}
2. What is Difference b/w function declaration and function definition?
Function declaration :- It is the syntax when we use at the time of calling a function.
Syntax:- retun_type function_name(arguments);
Function Definition:- it describes the implementation of the function.
it contains the body.
Synatax:-
return type function_name(arguments){
/*body of the function*/
}
3. What are the actual parameters and formal parameters?
Actual parameters :- The arguments we use in calling function or function declaration..
Formal parameters:- The arguments we use in in function definition.
4. What is the Difference b/w Pass By Value and Pass By reference.
Pass By Value :- in Function if we pass arguments as values then it is called as pass by value.
Pass By Reference :- in Function if we pass arguments as pointers then we call that it as pass by reference.
5. Should actual arguments and formal arguments be same type.
Yes. It must be same type.
6. Can we pass arrays as arguments?
Yes.
7. Can we pass pointers as arguments ?
Yes
8. Can we pass void as arguments?
Yes , if function doesn’t take any argumets then you can pss void as argument.
9. What is the scope of the formal arguments?
Local to that function.
10. What is local variable and Globla varialbes?
Local Variables :- The Variables which are declared with in the function or in particular block are called local variables.
They are permitted to that particular function in which it is declared.
Global variables :- The Variables which are visible to entire program or all functions are called global variables.