Ad Code

Responsive Advertisement

function in c language.

Q:-1 what is UDF? Explain the Structure of UDF. Discuss the advantages of UDF.

-> ‘C’ allows programmer to define their own function according to their requirement. These
types of functions are known as user-defined functions.
-> The main difference between library function and UDF are that library function are not
require to be written by user or client whereas a user-define function (UDF) has to be
developed by the user at the time of writing program.

Structure of UDF 
function1 (); //Function Declaration or Function Prototyping
function2 (); //Function Declaration or Function Prototyping

main()
{
 function1 (); //Function Calling
 function2 ();//Function Calling

}
function1 () //Function Definition
{
 ………………………
 ………………………
}
function2 ()//Function Definition
{
 ………………………
 ………………………
}
Function Definition
-
->  Every C program begins from main () and program starts executing the codes inside
main() function. Outside the main () function user can define the user define
function. In above structure user can define two function function1 () and function2
() which are known as function definition.
-
 - > Function definition includes function name, function type, parameters, local variable
declaration, function statement, return statement elements.

General format of function Definition
function_type function_name (parameter)
{
 local_variable declaration;
 statement1;
 statement2;
 return statement;
}

Function Declaration or Function Prototyping 

Every function in C programming should be declared before they are used. This type of
declaration are also called function prototype. Function prototype gives compiler
information about function name, type of arguments to be passed and return type.
Note: Older versions of the C language didn't have prototypes; the function declarations
only specified the return type and did not list the argument types.

Function Calling
Control of the program cannot be transferred to user-defined function unless it is called
invoked. In the above example, function call is made using statement function1 () and
function2 (). This makes the control of program jump from that statement to function
definition and executes the codes inside that function.

Example
 {
 int a;
 a=add(25,23); // function call
 printf("%d\n", a);
 }


Advantages of UDF
-
 - > User defined functions helps to decompose the large program into small segments
which makes programmer easy to understand, maintain and debug.
-
 - > If repeated code occurs in a program. Function can be used to include those codes
and execute when needed by calling that function.
-
 - > Programmer working on large project can divide the workload by making different
functions.

Q:-2 Explain category of UDF or types of UDF with example. 
 User-defined functions can be categorized define as follow: 
-
->  Function with no arguments and no return value
-
->  Function with no arguments and return value
-
->  Function with arguments but no return value
-
->  Function with arguments and return value.


1. Function with no arguments and no return value

      In C program function has no arguments; it does not receive any data from the
calling function. When it does not return a value the calling function does not receive data
from the called function.

Example 
#include <conio.h>
#include <stdio.h>
display();
main()
{
 clrscr();
 display();
getch();
}
display()
{
 printf("\n\n Vimal Vaiwala");
}


2. Function with no arguments and return value 


These could be occasions where we may need to design functions that may not take
any arguments but returns a value to the calling function.
Example
#include <conio.h>
#include <stdio.h>
int add(); //Function Prototyping
main()
{
      int ans;
      clrscr();
      ans=add(); //no Arguments
    printf("%d",ans);
 getch();
}
int add()
{
 int i=5,j=6,k;
 k=i+j;
 return k; //return Value
}

3. Function with arguments but no return value. 


The nature of data communication between the calling function and the arguments to the
called function and the called function does not return any values.
Example
#include <conio.h>
#include <stdio.h>
int add();
main()
{
     int i,j;
     clrscr();
      printf("\nEnter the value i:=>>");
      scanf("%d",&i);
      printf("\nEnter the value j:=>>");
      scanf("%d",&j);
 add(i,j);
 getch();
}
int add(int a,int b) //passing Arguments
{
 int k;
 k=a+b;
 printf("\nAddition:=>>%d",k);
}
4. Function with arguments and return value.
The function of the type Arguments with return values will send arguments from the calling
function to the called function and expects the result to be returned back from the called
function back to the-calling function.
Example
#include <conio.h>
#include <stdio.h>
int add();
main()
{
     int i,j;
     clrscr();
     printf("\nEnter the value i:=>>");
     scanf("%d",&i);
     printf("\nEnter the value j:=>>");
     scanf("%d",&j);
     add(i,j);
     printf("\nAddition:=>>%d",i+j);
 getch();
}
int add(int a,int b) //passing Arguments
{
 return a+b;
}

Q:-4 Recursive function is a function that calls itself with advantages and disadvantages.


When a function calls another function and that second function calls the third function
then this kind of a function is called nesting of functions. But a recursive function is the
function that calls itself repeatedly.

Example
main( )
{
     printf(“\n\n vimal vaiwala”);
     main();
}

Recursive function allows you to divide your complex problem into identical singles simple
cases which can handle easily. This is also a well-known computer programming technique:
divide and conquer.
Recursive function must have at least one exit condition that can be satisfied otherwise
program will go in infinite condition.

Example
#include<stdio.h>
#include<conio.h>
int factorial(int number)
{
 if(number<=1)
 return 1;
 return number* factorial(number-1);
}
void main()
{
     int x=5;
    clrscr();
     printf("factorial of %d is      %d",x,factorial(x));
 getch();
}

Advantages
-
 - > 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.


Disadvantages
-
->  It is hard to think the logic of a recursive function. It is also difficult to debug the
code containing recursion.


Q:-5 Explain passing array in function with an example.

In C programming, a single array element or an entire array can be passed to a function. Also, both
one-dimensional and multi-dimensional array can be passed to function as argument.


Example
#include <stdio.h>
void printarr(int a[])
{
     int i;
    for(i=0;i<5;i++)
    {
         printf("%d\n",a[i]);
    }
}
main()
{
       int a[5];
       int i;
      clrscr();
      for(i=0;i<5;i++)
      {
            a[i]=i;
      }
    printarr(a);
 getch();
}

Post a Comment

0 Comments