Friday, December 19, 2008

TIPS FOR USER-DEFINED FUNCTION in c++

DEFINITION - A UDF IS A NAMED GROUP OF PROGRAM STATEMENTS. THIS UNIT CAN BE INVOKED FROM OTHER PARTS OF THE PROGRAM AND MAY OR MAY NOT RETURN A VALUE.
THEY ARE BROADLY CATEGORISED IN TWO CATEGORIES -
1. Standard Library functions ( Bulid-in functions) - Are provided by the language itself.
2. User Defined function - Are made by the user .
Every UDF has three statements-
1. Function declaration or function prototype.
eg - void trunc( float, int &, int &);
2. Function definition block or function body.
eg - void trunc( float a, int &b, int &c)
{-------- }
3. Function call.
eg- trunc(a,b,c)
NOTE_ ORDER AND NUMBER OF PARAMETERS SHOULD MATCH THE FUNCTION HEADER.
Each of these staements consists of following parts-
1. return type of the function - is a valid data type . In case no value is returned by the function the return type will be void .
2. Function name - Any name adhering to the valid identifier naming rules should be used.
NOTE - DO NOT USE BUILD_IN FUNCTIONS NAMES FOR USER-DEFINED FUNCTIONS.
3. Parameter list - consist of varaibles with their data-type seperated by comma. If no parameters exists then this list is void.
eg- 1. void prn(void) or void prn() - No return-type & no parameters.
2. void prn(int, float, char) - - No return -type & three paameters.
3. float prn ( ) - Return type float and no parameters.
4. float prn( float, int) - return type float and two parameters.
NOTE- Depending on the problem the function will be defined using these four possible ways.
Possible function styles -
1. Function body appears before function call(usually in main()) , then function prototype or declaration is omitted. (Refer- page no. 338 in 'C.Sc with c++ 'by Sumita Arora class XI)
2. Function body appears after the function call(usually in main()) , then function prototype or declaration becomes mandatory. (Refer- page no. 338 in 'C.Sc with c++ 'by Sumita Arora class XI).
Parameters of function can be-
1. Passed by value - Hence original parameters are not effected by change of formal parameters.
2. Passed by reference - Hence original parameters are changed by change of formal parameters. & is prefixed in the parameter name.
3. Default parameters - Function call may miss the defaulted parameter, but if given default value will be overwritten.
NOTE- DEFAULTING OF PARAMETERS IS DONE FROM RIGHT TO LEFT.
4. Constant parameters- const qulifier appears in the parameter list. No change on this parameter is acceptable.

Function may return -
1. A value - In this case function call will be written with cout statement or will appear on the right hand side of assignment statement.
2. A reference - In this case function call will appear at the left hand side of the assignment statement.
NOTE- THIS IS USED TO RESET THE VALUE OF THE REFERNCE PASSED THRU FUNCTION.

2 comments:

Anonymous said...

hello ma'am.. this is shriram.. thx for the basic notes u have provided.. its useful.. ma'am.. if u can pls.. explain some tricky qestions through example..good blog..an eductional and knowledgable one..thank you..!!

Unknown said...

hey mam.... herz aatri...... of 11th e.... imformation u provided is really much helping for me.... thanx so mauch....n beautifully designd blog.:-)