User defined functions


Function:
  • A function is a set of instructions that are used to perform specified tasks that repeatedly occur in the main program.
  • It helps to avoid duplication of work and also break down a large program into a number of small functions.
  • It is used to divide the complex task into manageable task.
  • Functions are classified into two types as shown below,

              User-defined functions
              Pre-defined functions
1.User defined function:
  • The users have full scope to implement their own ideas in the function.
  • It defined by the users according to their requirements are called user-defined functions.
  • Elements of user-defined function:

               * Function declaration
               * Function definition
               * Function call

Syntax of user-defined function:

<Header_files>
Main_function()
{
variable_declaration;
//function declaration part
return_type or data_type function_name(parameters list);
// function calling part
function_name();//no parameter passed
or
function_name(parameter);//parameter passed
or
return value=function_name(parameter);
//printing statements
}
//function definition part
Data_type function_name(parameters list)
{
Local variable declaration;
......
Body of the function;
return (expression);
}

Parameters:
It provides data communication between the calling function and called function.
There are two types of parameters,
(i). Actual parameters:
Used in data transferred from the main function to the user defined function.
(ii). Formal parameters:
Used in data transferred from a user-defined function into the main function.
For example:


Where
          a, b are the actual parameters.
          x,y are the formal parameters.
Local and global variables:
Local variable:
The local variables are defined within the body of the function.
This variable used within the function only, other functions can not access these variables.
Example:

fun1(int a, int b)
{
          int c,d;
}

Global variable:
The global variables are defined outside the main () function, multiple functions can use these variables.
Example:

int x,y=10;
main()
{
          int a,b;
}

Example program for local and global variables.

#include<stdio.h>
#include<conio.h>
int i; //Global variable
void main()
{
          int j; //Local variable
void fun1();//Function declaration
i=0;
clrscr();
printf("Value of i in main function: %d\n",i);
fun1();//Function calling
printf("Value of i in after call: %d\n",i);
getch();
}
//Function definition
void fun1()
{
int k; //Local variable for fun1()
i=50;
}

Output:
The return statement:
A return statement is an optional part of the function.
It is used to populate the results without any printing function.
Syntax:

return; or return(expression);

Example:

if(x>0)
          return(1);
else
          return(0);

Function calling:
  • A function can be called by specifying the function name in the source program with parameters if presence within parentheses.
  • If a function is not declaring the variable to return, we must be specified the data type at return value.

For example:

  • It is divided into two types,

1.Function call without a parameter:

#include<stdio.h>
#include<conio.h>
void main()
{
msg_func();//Function definition without parameter and
                          called the function
printf("\n Message from main program or function");
getch();
}
//Function definition
msg_func()
{
clrscr();
          printf("\n Message from msg_func() user defined function");
}

Output:
2.Function call with parameter:

#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
a=msg_func(10,20);//Function definition with parameter and 
                                   called the function
printf("\nMessage from main program or function");
printf("\nThe value of a is %d",a);
getch();
}
//Function definition
int msg_func(int x,int y)
{
int z;
z=x+y;
printf("\nMessage from msg_func() user defined function");
return(z);
}

Output:



Share:

No comments:

Post a Comment

Recent Posts

Service Support

Need our help to Learn or Post New Concepts Contact me