Elements of user defined Function and parameter funciton

 Elements of user-defined functions :

         In order to write a different user-defined function, the programmer must know with three elements.


Function declaration.

Function definition.

Function call / invoke.

Function Declaration :

   Like normal variables in a program, users must declare the function before they defined and invoked.

the syntax 

                return_data_type function_name( parameters_list);


  return_data_type  ------> is the return type of function

  function_name    -------> is the name of the function 

  parameters_list  -------> is the list of the parameter that function can convey.


when declaring this function on your program you have to make the following rules on your mind.

i ) The list of parameters must be separated by a comma.

ii)  names of the parameters are optional but data type is a must.

iii) If the function doesn't return any value the return type must be void.

iv)  If there is no parameter simply place void braces.

v)  Data type of the actual & formal parameter must match.

vi) Place the semicolon at the end of the function declaration.

 

Function Definition :

           A function definition is also called function implementation. It is the process of specifying and establishing the user-defined function.

function definition mainly contains two parts.

   1 . function header.

   2.  function body


return_data_type function_name (arguments_list)  // function header

{

     local declarations;        /*     function body

     executable statements;     

     return statements;              function body end      */

}


1 ) function header :

          function header mainly consists of three elements. They are :

Return data type.

Function name.

Argument / parameter list 

Return data type :

              Specify the type of data returned by the function. Suppose the function doesn't return anything then it must be specified with the void.


Function name :

        Specify the name of the function. Function names must be unique and also it should not be a keyword. All the rules of naming variables applicable to this also.


Argument/ parameter :

             Specify the number of parameters and type of each parameter passed into the function during the function call.


2) Function Body :

Local variables :

            Any variables that need to be used in the function body must be declared before they are used inside the body of the function.


Executable statements :

          All the executable statements/coding must be written within this body of the function.

Return statements :

                It is optional the return statement may or many not send some values to the calling function.

the syntax for return statement is ,

            return (variable);


Function Call / Invoke :

                         A function can be called by simply specifying the name of the function and with actual parameters if any and must and with a semicolon. Once a function is called the control of the main(calling)  program passes to the called function. And the working of the calling function is temporarily stopped. When the execution of the called function is completed then only the control return back to the calling function and execute the next statement.


the syntax for a function call is,

                              function_name();

              function_name(parameters);

              variable= function_name(parameter);


#include<stdio.h>

int add2num(int x,int y);

int main(){

    int a,b,sum;

printf(“enter the first values”);

scanf(“%d”,&a);

printf(“enter the second values”);

scanf(“%d”,&b);

sum=add2num(a,b);

pritnf(“sum of the vlues is:”,sum);

}

int add2num(int x,int y){

           int x;

           z=x+y;

           return z;

}


output:

enter first number: 10

enter second number:  20

sum of the values is: 30



 

 Parameter or Arguments:

                     usually, the parameter provides data communication between the calling function and called functions. 

parameter mainly has two types.





               

Actual parameters :

               These are the parameter transferred from the calling function (Usually main program) to the called function. Arguments are calling functions known as the actual parameters.


Formal parameters :

      These are the parameters that are used in the called function(Usually arguments in the user-defined functions). Arguments of called function known as a parameter. 


Function Prototypes :

      The functions are classified into the following types depending on whether the arguments are present or not and whether a value is returned or not are called function prototypes. it is mainly four types.


1 ) Function with no argument and no return value

2) Function with no argument and with return value.

3) Function with argument and return value

4) Function with argument and with return value .

 Function with no arguments and no return value :

            Here no arguments take place between the calling function and the called function. The called function doesn't receive any data from the called function and also doesn't return any values calling function. 


#include<stdio.h>

void add();

int main(){

  add();  

  }

void add(){

   int a,b,sum;

  printf(“enter the first values :”);

  scanf(“%d”,&a);

  printf(“enter the second values :”);

  scanf(“%d”,&b);

  sum=a+b;

  printf(“sum values are : “,sum);

}

 

here, In this program, we didn't declare any return value.


output:

enter the first number: 8

enter the second number: 2

sum values are: 10


         

FUNCTION WITH NO ARGUMENT AND WITH RETURN VALUE:

                                  in this format data transfer take place from the called function to calling function . It is a one way data communication i.e, the called function does not receives any data from the calling functions  but it return some value to the calling program.The called function . it reads values from the user through the keyboard and process that and return the result to calling function.

#include<stdio.h>

 

int addnum();

 

int main(){

 

   int sum;

 

sum=addnum();

 

printf(“%d”,sum);

 

}

 

int addnum(){

 

       int a,b,s;

 

       printf(“enter the values:”);

 

       scanf(“%d”,&a);

 

       printf(“enter the values:”);

 

       scanf(“%d”,&b);

 

       s=a+b;

 

    printf(“sum of the two num is :”);

 

       return s;               

    }





output:

enter the values :2

enter the values:4

sum of the two num is:6

FUNCTION WITH ARGUMENT AND NO RETURN VALUE :

          In this type data transfer take place from the calling function to called function. It is a once way data communication. The called function receives data from calling function but it does not return any value to the function .

#include<stdio.h>

void  addnum(int a,int b);

int main(){

     int c,y;

       printf(“enter the values:”);

       scanf(“%d”,&a);

       printf(“enter the values:”);

       scanf(“%d”,&b);

      addnum(c,y);

}

void addnum(int a, int b){

       int s;

       s=a+b;

       printf(“sum of the two num is   :”,s);                                                 

}


output:

enter the values:4

enter the values:4

sum of the two num is:8

FUNCTION WITH ARGUMENT WITH RETURN VALUE:

      in this format the data transfer take place between both from the calling function to the called function and called function to calling function. It is a two way data communication.This the called function revives data from the calling function.

#include<stdio.h>

Int  addnum(int a,int b);

Int main(){

     Int c,y,sum;

       Printf(“enter the values:”);

       Scanf(“%d”,&a);

       Printf(“enter the values:”);

       Scanf(“%d”,&b);

      Sum=Addnum(c,y);

}

Int  addnum(int a, int b){

       Int s;

       S=a+b;

       Return s;                                       

}

 



output:

enter the values: 5
enter the values: 5
sum of the value is: 10


No comments:

Post a Comment