Functions and Prog Structure
Functions and Prog Structure
• pow(x,y) - x raised to y
Sakthi Balan Muthiah
fl
ff
ff
fl
fl
Modularising or Functionalising
• int inside the brackets tells 3. Arguments order are the same
that there is one argument 4. The return value is consistent with the data type defined in
which is of integer type the prototype
statements
• semi-colon at the end is
necessary }
return-value-type function-name(parameter-list) {
statements
return 0;
Note
in the main( ) indicates
The compiler uses function if the program
prototypes to validate function executed correctly or
calls. not. ‘0’ indicates there
was no error.
Sakthi Balan Muthiah
Max of three integers using
function
• For example
• if one value is long double then other values are converted to long double
• If one is double then all other values are converted to double
• If one is float then others are converted to float
• Value can be converted to a lower type only by explicitly assigning to a variable de ned to be of
that type (normally this is not advised as the value changes)
• For example, square(4.5) will return 16, but not 20.25 or even 20 !
• Many compilers issue warnings for the above
Sakthi Balan Muthiah
ff
fi
Design a simple calculator program that gets input of two
positive integers m and n and then calculates according to
the user’s input either m+n, m*n, m-n, or m/n
Use switch statement to select the operation and design
functions to perform each of the four operations.