Google News
logo
User-defined functions In C Language
C allows programmer to define their own functions according to their requirement. These types of functions are known as user-defined functions. Suppose a programmer wants to find factorial of a number and check whether it is prime number or not in same program. Then, he/she can create two separate user-defined functions in that program: one for finding factorial and other for checking whether it is prime number or not.

The general form of a function declaration is 

returntype functionname (datatype arg1, datatype  arg2 ……datatype  arg n) //function declarator
{
--------------------
< statement block>  //function body
--------------------
return (variable or expression)
}

Where returntype is the data type of the value return by the function and arguments expected,

arg1, arg2…. argn are the arguments which are variables which will receive values form the calling program,

 Functionname is the name of function by which the function is called by the calling program. 

There is a local declaration of variables. These variables are referred as local variables, and those are used only inside the function. The statement block consists of a set of statements and built-in functions which are executed when the function is called. The result is returned to the calling program through a return statement that normally appears at the end of a function block. This function block starts and ends with braces { } 


Function declarator is the first line of function definition. When a function is called control of the program is transferred to Function declarator.
Function declarator is followed by body of function inside braces.

main () Function :
1. main() is the starting function for any C program. Execution commences from the first statement in the main() function.  
2. It returns int value to the environment that called the program. Usually zero is returned for normal termination of the main(). Non zero is returned to convey abnormal termination.  
3. It uses no parameter. But it may use two specific parameters.
4. Recursive call is allowed for main() function also.
5. Only the function body varies from programmer to programmer main() function head follows the common syntax by either having no parameter or only two standard parameters. 
6. The program execution ends when the closing brace of the in main is reached.

FUNCTION PROTOTYPE :

  When a C program is compiled, the compiler does not check for data type mismatch of actual arguments in the function call and the formal arguments in the function declaration. To enable the compiler to check the same, a function prototype declaration is used in the main program. 

Function prototype is always declared at the beginning of the main() program.


ACTUAL AND FORMAL ARGUMENTS :

The arguments listed in the function calling statements are referred to as actual arguments. These actual values are passed to a function to compute a value or to perform a task. 

  The arguments used in the function declaration are referred as formal arguments. They are simply formal variables that accept or receive the values supplied by the calling function.  

Note: The number of actual and formal arguments and their data types should match.

Passing arguments to functions :
In programming, argument (parameter) refers to data this is passed to function (function definition) while calling function.

For example, two variables, num1 and num2 are passed to function during function call and these arguments are accepted by arguments a & b in function definition.

 
                  #include
                  int add(int a, int b);
                  int main()
                  {
                   â€¦â€¦â€¦â€¦â€¦â€¦.
                   sum=add(num1,num2);
                   â€¦â€¦â€¦â€¦â€¦â€¦.
                  }
                 int add(int a, int b)
                 {
                   â€¦â€¦â€¦â€¦â€¦â€¦
                   â€¦â€¦â€¦â€¦â€¦â€¦.
                 }

                        Here,
    a=num1
                            b=num2

Arguments that are passed in function call and arguments that are accepted in function definition should have same data type.

 For example: If argument num1 was of int type and num2 was of float type then, argument variable “a” should be of type int and “b” should be of type float, i.e., type of argument during function call and function definition should be same.

Function return type :
 Functions in C may or may not return values. If a function does not return a value the return type in the function definition and declaration is specified as void. Otherwise, the return type is specified as a valid data type. 

Example :
main()
{
   unsigned sq = squareint (32); /*function  call *)
   printf(“ The square of 32 is ./.n\n”, sq), /*  control returns here */
}

 The function squareint is called and the return value is assigned to the variable sq. the control is returned to the printf statement after the statements within the function definition of squareint are executed.

FUNCTION CALLS :

  A function can be called by simply using the function name followed by a list of actual parameters (or arguments).

Rules to call a function :
1. A function has a statement block which is called by the main( ) or any other function.
2. When the data type in a function declaration is omitted the function will return a value of the type integer.
3. The data type of the formal arguments may be declared in the next line which follows the function declaration statement.
A function can be called with or without an argument.

  Program :
#include<stdio.h>
main()
{
 int y ;
 y = mul (10, 5);          / * function call * /
 printf (“Multiplication is:%d \n”, y) ; 
}

int mul(int x, int y)
{
int p ;   / * local variables x=10, y=5 * /
p= x * y ;
return(p);
}
Output :

Multiplication is: 50

When the compiler encounters a function call the control is transferred to the function mul (), this function is then executed line by line as described and the value of p is returned, when the return statement is encountered. This value is assigned to y.

Parameters can also be used to send values to the calling programs. Parameter list contains declaration of variables separated by commas and surrounded by parenthesis.

A function need not always receive values form the calling program. In such cases, functions have no formal parameters. To indicate that the parameter list is empty we use the keyword void between parentheses as,

Syntax:
void printline(void)
{
  ………………
  ………………
}

This function neither receives any input values nor returns back any value.

Many compilers accept an empty set of parenthesis without specifying anything as void printline().

There are two ways in which we can pass arguments to the function :

• Call by value
• Call by reference.

CALL BY VALUE :

In this type value of actual arguments are passed to the formal arguments and the operation is done on the formal arguments. Any change made in the formal arguments does not affect the actual arguments because formal arguments are photocopy of actual arguments. Hence when the function is called by the call by value method, it does not affect the actual contents of the actual arguments. Changes made in the formal arguments are local to the block of called function. Once control returns back to the calling function the changes made vanish.

In call by value, original value is not modified.

Example program to send values by call by value: In this program, the values of the variables “m” and “n” are passed to the function “swap”. These values are copied to formal parameters “a” and “b” in swap function and used.

#include<stdio.h>
void swap(int a, int b); 
int main()
{
    int m,n;
    // calling swap function by value
    printf(“Enter m & n values :”);
    scanf(“%d%d”, &m, &n);
    printf(“ values before swap  m = %d \n and n = %d”, m, n);
    swap(m, n);                         
}
void swap(int a, int b)
{ 
    int tmp;
    tmp = a;
    a = b;
    b = tmp;
    printf(“ \n values after swap m = %d\n and n = %d”, a, b);
}
Output :

Enter m & n vales : 5 10
Values before swap m=5 and n=10
Values after swap m=10 and n=5

CALL BY REFERENCE :

In this type instead of passing values, addresses are passed. Function operates on addresses rather than values. Here the formal arguments are pointers to the actual arguments. In this type, formal arguments point to the actual argument. Hence changes made in the arguments are permanent.

In call by reference, original value is modified because we pass reference (address).

  Program : The following program is an example of call by reference.
#include<stdio.h>
void swap(int *a, int *b); 
int main()
{
  int m,n;
 printf(“Enter m and n values:”);
 scanf(“%d%d”, &m, &n);
printf(“values before swap m = %d \n and n = %d”, m, n);
swap(&m, &n);         
}
void swap(int *a, int *b)
{
 int tmp;
tmp = *a;
*a = *b;
*b = tmp;
 printf(“\n values after swap a = %d \n and b = %d”, *a, *b);
}
Output :

Enter m and n values : 20 40
Values before swap m=20 and n=40
Values after swap a=40 and n=20

Difference between call by value and call by reference in C :
S.No Call by value Call by reference
1 A copy of value is passed to the function An address of value is passed to the function
2 Changes made inside the function is not reflected on other functions Changes made inside the function is reflected outside the function also
3 Actual and formal arguments will be created in different memory location Actual and formal arguments will be created in same memory location