Functions

A function is a set of statements which performs a specific task. 
Functions Advantages:-
 
  • Reducing the length of the program.
  • Eliminating the redundency of the code.
  • Increasing the efficiency of the program.
Functions are 2 types 
1) standard functions or Library functions or Predefined functions
 
2) User defined functions
 
1) Standard Functions:-
These functions are ready made functions available to the user.
 
Ex:- printf(), scanf(), pow(), strlen(), strcpy(), toupper(),……
 
2) User defined functions:-
These functions are created by the user.
 
Ex:- address(),name(), add(a,b), area(r) , fact(n), …………
 
For declaring a user defined function we have to use 4 ways.
  • Function with arguments with return values.
  • Function with arguments without return values.
  • Function without arguments with return values
  • Funtion without arguments without return values. 
    For all these types of functions we have to use
    • function prototyping or function calling
    • function called
    • function implementation.
Syntax of function calling (or) function prototying:-
syntax-<return type> <function-name>(<argument list>);
Return types are:-
 int,char,float,double, void.
Void means function should not return any value. By default return type for a function is int.
Function name is user defined.
Argument list :-
 Contains the datatypes for the variable what type of variable we are passing. 
Function prototying or function calling always ends with semicolon. Function prototying or function calling is always before the main() or within the main() first statement.
Syntax of Called function:-
<function-name>(<argument list>);
Function name should be samename. Argument list we pass the variables as parameters.
This is always within the main().
Syntax of implementation function:-
<Return_type> <function_name>(<argument list>)
{
<declarations>;
<execution statements>;
-------------
return value;
}
return statement:
 
It returns a value of the expression to the calling function. Return statement should not be used for void return type. Except void return type all the functions returns the values. These values return to the main().
syntax:-return(expression);
ex:-return(0);
return(a);
return(a+b);
return a;
return;
Note:- If you write implementation function before the main() function calling is not required. In main() called function parameters are called actual parameters. Implementation function parameters are called formal parameters. Formal parameters and actual parameters may or maynot be the same. But same type of parameters.
Function with arguments and with return values:-
In this we pass the arguments and it returns the values also.


No comments:

Post a Comment