USER-DEFINED FUNCTIONS


USER-DEFINED FUNCTIONS

INTRODUCTION:

A function is a self-contained block of statements that perform a task of some kind. A function performs same type of task every time. Every C program can be thought of as a collection of these functions. Generally we use three primary functions, namely, main, printf, and  scanf. We shall consider in detail how a function is designed, how two or  more functions are put together and how they communicate with one another. C functions can be classified into two categories, namely,

1. Library functions and
 2. user-defined functions.

 Note:  main is an example of user-defined function.

 printf and scanf belongs to the category of library functions.

The main difference between these two categories is that library functions are not required to be written by us whereas a user-defined function has to be  developed by the user at the time of writing a program.

NEED FOR LIBRARY& USER-DEFINED FUNCTIONS

Every program must have a main function to indicate where the program has to begin its execution. If only main function is used, then the program may become too big and complex, and it will be difficult to debug,test and maintain it. So we use independent functions in main function. These  functions are much easier to understand, debug, and test.

The main function calls familier functions from C library. The function of specific type can be prepared by the user called user-defined function. The user-defined functions are kept after the main function ends. Whenever these user-defined functions are required, the main function calls the required function. Then the main function can be called as calling function and the  user-defined function is called called function.

A user-defined function is a seft-contained block of code that performs a particular task. Once a function has been designed and packed, it can be treated as a ‘block box’ that takes some data from the main program and returns a value. The inner details of operation are invisible to the rest of the program. All that the program knows about a function is: What goes in and what comes out. Every C program can be designed using a collection of these block boxes.


THE FORMAT OF USER-DEFINED FUNCTION:

All user-defined functions have the form given below:
function-name(argument list)
argument declaration;
{
local variable declarations;
executable statement1;
executable statement1;
-------------
-------------
return(expression);
}

All parts are not essential. Some may be absent. For example, the argument list and its associated argument declarartion parts are optional. The declaration of local variable is required only when any local variable are used in the function. A function can have any number of executable statements.

The return statement is the mechanism for returning a value to the calling function. This is an optional statement. Its absence indicates that no  value is being returned to the calling function.

CATEGORY OF USER-DEFINED FUNCTIONS

A user-defined function, depending on whether arguments are present or not and whether a value is returned or not, may belongs to one of the following categories.

Category 1: Functions with no arguments and no return values
Category 2: Functions with arguments and no return values
Category 3: Functions with arguments and return values.

Variable Declaration:
 Varible declaration can be two types: They are:
  Local And Global

Where a program's fingers can't reach.



From the computer's point of view, a C program is nothing more than a collection of functions and declarations. Functions can be thought of as sealed capsules of program code which float on a background of white space, and are connected together by means of function calls. White space is the name given to the white of an imaginary piece of paper upon which a program is written, in other words the spaces and new line characters which are invisible to the eye. The global white space is only the gaps between functions, not the gaps inside functions.

Global Variables:
Global variables are declared in the white space between functions. If every function is a ship floating in this sea of white space, then global variables (data storage areas which also float in this sea) can enter any ship and also enter anything inside any ship (See the diagram). Global variables are available everywhere;. they are created when a program is started and are not destroyed until a program is stopped.

Local Variables:
Local variables are more interesting. They can not enter just any region of the program because they are trapped inside blocks. To use the ship analogy: if it is imagined that on board every ship (which means inside every function) there is a large swimming pool with many toy ships floating inside, then local variables will work anywhere in the swimming pool (inside any of the toys ships, but can not get out of the large ship into the wide beyond. The swimming pool is just like a smaller sea, but one which is restricted to being inside a particular function.

1.      NO  ARGUMENTS  AND  NO  RETURN VALUES

When a function has no arguments, it does not receive any data from the calling function. Similarly, when it does not return a value, the calling function does not receive any data from the called function. In effect, there is

no data transfer between the calling function and the called function.

main()
{
printf(“I love India.\n”);
neha();
shilpa();
}
neha()
{
printf(“Do you love India?\n”);
} shilpa()
{
printf(“I too love India.\n”);
}

The output of the above program when executed would be as under:

   o/p:    I love India.
             Do you love India?
              I too love India.
1.      ARGUMENTS BUT NO  RETURNVALUES:



    When a function has an argument, it receives data from the terminal and  it is transferred to the called function. If there is no calculations in the calling  function, then it does not return any value to the main function.

        The mechanism used to convey information to the user-defined function is the arugument or parameter. The            arugment for parameter may be a variable or  character constant. These arguments must be in parentheses.


Example:
main()
{
char ch;
clrscr();
printf("Enter a character\n");
scanf("%c", &ch);
clrscr();
printline(ch);
printf("\nWelcome to Neha Computers\n");
printline(ch);
printf("\n");
getch();
}
Printline(char x)
{
int i;
for(i=1; i <= 25; i++)
printf("%c", x);
}


The output will be under the control of the user, if ch is * then the the output  will be as under:

In this program ch can be called as argument or paramenter. It receives argument from the terminal. This argument can be called actual argument. This argument is transferred to the user-defined function. In the

User defined function we find another argument called formal argument which must be declared in the parenthesis. If you use actual argument in the user defined function, then the compiler would still treat them as different variable since they are in different function.

In this user-defined function nothing is to be returned. Therefore, it is not necessary to use the return statement. Immediately after the closing brace the control is returned to the calling function.

2.      ARGUMENTS WITH  RETURN VALUES



When a function has arguments, it receives data from the terminal and they are transferred to the called function. The calculations are done in the calling function, then it returns the to the main function for further processing or displaying.


Example:
/* Sending and receiving values between calling and called functions */
main()
{
int a, b, c, sum;
clrscr();
printf("Enter any three numbers\n");
scanf("%d %d %d", &a, &b, &c);
sum = calsum(a, b, c);
printf("Sum = %d\n", sum);
getch();
}
calsum(x, y, z)
int x, y, z;
{
int d;
d = x + y + z;
return(d);
}


RETURN VALUES AND THEIR TYPES:

A function may or may not send back any value to the calling function. If it does, it is done through the return statement. While it is possible to pass to the called function any number of values, the called function can only return one value per call, at the most.

The return statement can take one of the following forms:

Return ;
or
return (expression);

Example:

main()
{
float a, b, mul();
a = 10.0;
b = 3.0;
clrscr();
printf("%f\n", mul(a, b));
getch();
}
float mul(x, y)
float x, y;
{
return(x*y);
}

In this program it returns a floating value.

FUNTIONS RETURNING NOTHING

We have seen earlier that in many occations, functions do not return and value. They perform only some printing. This is made possible by making

use of the keyword void.

Example:

main()
{
void gospel();
printf(“I want to be your friend\n”);
}
void gospel()
{
Printf(“What a nice person you are!\n”);
}


NESTING OF  FUNCTIONS



C permits nesting functions freely. main can call function1,
which calls
function2,
which calls function3, ..............
There is in principle no limit as to how deeplly functions can be nested.

Comments