User Defined Functions
Functions provided by library are not enough for user so to complete their needs user has to define some functions themselves; these are called user defined functions. main () is a user defined function.
Means except built in functions user can also define and write small programs as functions to do a task relevant to their programs, there functions should be codified by the user, so that such functions can perform the task as desired by user. Three things are necessary while using a function.
i. Declaring a function or prototype:
The general structure of a function declaration is as follows:
return_type function_name (arguments);
Before defining a function, it is required to declare the function i.e. to specify the function prototype. A function declaration is followed by a semicolon ‘;’. Unlike the function definition only data type are to be mentioned for arguments in the function declaration.
The Function Prototyping Contains
1) Return Type of a function: The Return Function determines whether a Function will return any value to the Function. If a Function is declared with the void Keyword or if a Function Contains a void then that’s means a Function Never Returns a value. Means a Function will Executes his statements one by one. And if a Function Contain any other data type means if a Function Contains int or Float then the Function must return a value to the user.
2) Name of Function : The Name of Function must be valid and the name of function must be Start from any Alphabet and the Name of Function doesn’t Contains any Spaces and the Doesn’t Contains any Special Character For Example Space , * sign etc.
3) Argument List: A Function may have zero or More Arguments. So that if we want to call a Function. Then we must have to Supply Some Arguments or we must have to pass some values those are also called as the Argument List. So that The Argument List is the total Number of Arguments or the Parameters those a Function Will takes. So that We must have Supply Some Arguments to the Functions,. The Arguments those are used by the Function Calling are known as the Actual Arguments and the Arguments those are used in the Function declaration are Known as the Formal Arguments, When we call any Function then the Actual Arguments will Match the Formal Arguments and if a proper Match is Found, then this will Executes the Statements of the Function otherwise this will gives you an error Message.
ii. Calling a function:
The function call is made as follows:
return_type = function_name (arguments);
There are Two Ways for Calling a Function
Call by Value: when we call a Function and if a function can accept the Arguments from the Called Function, Then we must have to Supply some Arguments to the Function. So that the Arguments those are passed to that function just contains the values from the variables but not an Actual Address of the variable.
So that generally when we call a Function then we will just pass the variables or the Arguments and we doesn’t Pass the Address of Variables , So that the function will never effects on the Values or on the variables. So Call by value is just the Concept in which you must have to Remember that the values those are Passed to the Functions will never effect the Actual Values those are Stored into the variables.
Call By Reference: When a function is called by the reference then the values those are passed in the calling functions are affected when they are passed by Reference Means they change their value when they passed by the References. In the Call by Reference we pass the Address of the variables whose Arguments are also Send. So that when we use the Reference then, we pass the Address the Variables.
When we pass the Address of variables to the Arguments then a Function may effect on the Variables. Means When a Function will Change the Values then the values of Variables gets Automatically Changed. And When a Function performs Some Operation on the Passed values, then this will also effect on the Actual Values.
iii. Defining 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*/
int add (int a, int b); int main()
printf (“Enters two number to addn”);
scanf (“%d %d”,&num1,&num2);
sum=add (num1, num2);
printf (“sum=%d”, sum);
int add(int a,int b)
/* Start of function definition. */
add = a+b;
/* End of function definition. */
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”.
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.
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.
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.
Functions with no arguments but returns 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.
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 */
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.