Why to use function?
Functions are used for driving a large code into module, due to this we can easily debug andd maintain code. For example if we write a calculator programs at that time we can write every logic in a separate function(for example sum(), for subtraction sub()). Any function can be called many times.
Advantages of Function
- Code Re-usability
- Develop an application in module format
- Easily debug the program.
- Code optimization: No need to write lot code.
- Program testing becomes easy.
- Compiled executable is smaller.
- You don't have to make comments because function name make sense.
- Program can be developed in short period of time.
- There is no chance of code duplication.
Defining of function is nothing but give body of function that means write logic inside function body.
Syntax
return_type_function(parameter)
{
function body;
}
Return type : A function may return a value. The return type is the data type of the value the function returns. Return type parameters and return statement are optional.
Function name : Function name is the name of function. It is decided by programmer or you.
Parameters : This is a value which is pass in function at the time of calling of function. A parameter is like a placeholder. It is optional.
Function body : Function body is the collection of statements.
Function Declarations
A function declaration is the process that tells the computer about a function name. The actual body of the function can be defined separately.
Syntax :
return_type function_name();
Calling a function
When we call any function, control goes to function body and execute entire code. For call of any function, just write name of function and if any parameter is required then pass parameter.
Syntax:
function_name(); or
Varriable=function_name(argurment);
Nice
ReplyDeleteThank you.
ReplyDelete