Today we will discuss a very important C++ Phenomenon ie Functions
these smaller pieces or components help in Reusability of code, instead of writing same code again and again, Such Small, independent piece of code/module is a ‘Function’ in C++
this is basically calling/invoking
a function, for example:
–square(x);
Introduction:
Functions basically work on the principle of "Divide and conquer" ie:- Construct a program from smaller pieces or components.
- Each piece more manageable than the original program
these smaller pieces or components help in Reusability of code, instead of writing same code again and again, Such Small, independent piece of code/module is a ‘Function’ in C++
–Programmers
use ‘new’ or ‘prepackaged’ modules
•New:
programmer-defined functions
•Prepackaged:
from the standard library
For
using
a function, one should only
know how
to call it….That is how to make a ‘function call’.
Functions and Terminology:
Function Prototype:
A function prototype contains;
return_data_type function_name(parameter_data_types);
e.g int square(int);
it is
not required if ‘definition’ appears before the function is called
No
Parameter names are required in a function prototype , all you need is parameter type.
Function Definition:
Format for a function definition is given below:
return-value-type function-name( parameter-list ){
declarations and statements }
– Parameter list
• Comma separated list of parameters
– Data type needed for each parameter
• If no parameter, use void or leave blank
– Return-value-type
• Data type of result returned (use void if nothing returned)
• Function Prototype and function header must
match
– Function prototype
– Tells compiler argument type and return type of
function
– int square( int );
• Function takes an int and returns an int
– return keyword
– Returns data, and control goes to calling
function
• If no data to return, use return;
– If no return statement is written,
control returns to calling function when reaches right brace of called function
– Functions cannot be defined inside other
functions
declarations and statements }
Function Call:
this is basically calling/invoking
a function, for example:–square(x);
•Function
name and information (arguments) it needs (argument x)
•After
finished, passes back result to calling function
given below are two examples to show the creation and usage of programmer defined functions.
Math Library Functions:
Perform common mathematical calculations
– Include the header file <cmath>
• Functions is called by writing
– functionName (argument);
or
– functionName(argument1, argument2,…);
• for Example:
cout << sqrt( 900.0 );
– sqrt (square root) function The preceding
statement would print 30.0
– All functions in math library return a double
– Function arguments can be
– Constants
– sqrt( 4 );
– Variables
– sqrt( x );
– Expressions
– sqrt( sqrt( x ) ) ;
– sqrt( 3 - 6x );
given below is a table of some math library functions:
Detailed Discussion of Function Prototypes:
•
Function prototype contains
–
Function name
–
Parameters (data type)
–
Return type (void if returns nothing)
–
Only needed if function definition after
function call
•
Prototype must match function definition header
–
Function prototype
double maximum( double, double, double );
–
Function Header
double maximum( double x, double y, double z )
{
…
}
•
A function call that does not match the function
prototype is a syntax error
•
Function signature:
–
Part of prototype with name and parameters
•
double maximum( double, double, double );
–
Force arguments to be of proper type ie Argument Coercion
•
Converting int (4) to double (4.0)
cout << sqrt(4)
–
Conversion rules
•
Arguments usually converted automatically
•
Changing from double to int can
truncate data
–
3.4 to 3
Author is awesome (Y) (Y)
ReplyDelete