C Language User defined Functions

Questions on C Language User defined Functions

1. How to define a function?
All the statements or the operations to be performed by a function are given in the function definition which is normally given at the end of the program outside the main. Function is defined as follows
return_type function_name (arguments)
/*Program to demonstrate the working of user defined function*/
#include <stdio.h>
int add (int a, int b);  int main()
int num1,num2,sum;
printf (“Enters two number to addn”);
scanf (“%d %d”,&num1,&num2);
sum=add (num1, num2);
printf (“sum=%d”, sum);
return 0;
int add(int a,int b)
/* Start of function definition. */
int add;
add = a+b;
return add;
/* End of function definition. */

2. What are the different types of functions depending on return type and arguments?

There are four types of functions depending on the return type and arguments:
•    Functions that take nothing as argument and return nothing.
•    Functions that take arguments but return nothing.
•    Functions that do not take arguments but return something.
•    Functions that take arguments and return something.
A function that returns nothing must have the return type “void”. If nothing is specified then the return type is considered as “int”.

3. What are the Functions with no arguments and no return value?
A C function without any arguments means you cannot pass data (values like int char etc) to the called function. Similarly, function with no return type does not pass back data to the calling function. It is one of the simplest types of function in C. This type of function which does not return any value cannot be used in an expression it can be used only as independent statement.

4. What are the Functions with arguments and no return value?
A C function with arguments can perform much better than previous function type. This type of function can accept data from calling function. In other words, you send data to the called function from calling function but you cannot send result data back to the calling function. Rather, it displays the result on the terminal. But we can control the output of function by providing various values as arguments.

5. What are the Functions with arguments and return value?

This type of function can send arguments (data) from the calling function to the called function and wait for the result to be returned back from the called function back to the calling function. And this type of function is mostly used in programming world because it can do two way communications; it can accept data as arguments as well as can send back data as return value. The data returned by the function can be used later in our program for further calculations.

6. What are the Functions with no arguments but return value?

We may need a function which does not take any argument but only returns values to the calling function then this type of function is useful. The best example of this type of function is “getchar ()” library function which is declared in the header file “stdio.h”. We can declare a similar library function of own.

7. What is a recursive function?

A function that calls itself is known as recursive function and the process of calling function itself is known as recursion in C programming. But while using recursion, programmers need to be careful to define an exit condition from the function; otherwise it will go in infinite loop.
Recursive functions are very useful to solve many mathematical problems like to calculate factorial of a number, generating Fibonacci series etc.
void recursion ()
recursion (); /* function calls itself */
int main ()
recursion ();

8. What are the advantages and disadvantages of recursion?

Recursion is more elegant and requires few variables which make program clean. Recursion can be used to replace complex nesting code by dividing the problem into same problem of its sub-type.
In other hand, it is hard to think the logic of a recursive function. It is also difficult to debug the code containing recursion.

9. What do you mean by inline function?
The idea behind inline functions is to insert the code of a called function at the point where the function is called. If done carefully, this can improve the application’s performance in exchange for increased compile time and possibly (but not always) an increase in the size of the generated binary executables.



This site uses Akismet to reduce spam. Learn how your comment data is processed.