Functions
Function is defined as a named group of statements. A function can be called from any point in a program. If a problem can be divided into small modules then each module can be represented as a function.
Two types of functions in c++. They are
Library functions: They are predefined functions. E.g. cin, cout.
User defined functions:
A user defined function has to be written by the programmer to carry out some well defined and specific task.
Function definition:
A function should be defined before it is used. A function has two parts
Syntax:
function-type function-name (list of parameters with declaration)
{
local variable declaration;
executable statement-1;
-- -- - - -- - -- - - -- - -
-- -- - - -- - -- - - -- - -
return(expression);
}
function-type – represents the data type of the value returned by the function.
Function- name – valid c++ variable name
List of arguments – formal arguments.
Local variable declaration – declaration of variables in the statement body.
return - to return the value of the function
Rules:
The expression in the return statement is optional .If there is no expression the return statement acts as a closing brace and return back the control. If there is expression it returns the value of the expression.
To declare function-type,The function type in the function header is optional. If there is no function type in the function header the return statement by default returns a integer value.
But with the help of the option function-type in the function header we can declare the type of the value returned by the function.
Eg:Function-name(list of arguments)
his function returns integer value.
Calling a function:
A defined function can be called from other functions by specifying its name followed by a list of arguments enclosed within parenthesis.
Syntax:
Function-name(list of arguments);
Function-name – name of a already defined function.
List of arguments – actual arguments.
| Name* : |
|||||
| Email* : |
|||||
| Country* : |
|||||
| Phone* : |
|||||
| Subject* : |
|||||
| Upload Homework : Upload another homework (upto 5 uploads max.)
|
|||||
| Due Date |
Time |
AM/PM |
Timezone |
||
| Instructions |
|||||
|
|||||