Functions Overview Functions are sequence of statements with its own local variables supports modularity, reduces code duplication Data transfer between function to be called and caller function by means of parameters currently one-way from caller function into function to be called We will see how to return data back to the caller function
Functions Overview. Functions are sequence of statements with its own local variables supports modularity, reduces code duplication Data transfer between function to be called and caller function by means of parameters currently one-way from caller function into function to be called - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Functions OverviewFunctions are
sequence of statements with its own local variablessupports modularity, reduces code duplication
Data transfer between function to be called and caller functionby means of parameterscurrently one-way
from caller function into function to be calledWe will see how to return data back to the caller function
Function Prototype (from 2.6)Functions definition has two parts
function heading name, parameters, return type
function body (local variables and statements within curly brackets)
Function Syntaxdouble SphereVol(double radius){ return 4.0*radius*radius*radius*acos(-1)/3;}
Function heading/prototype shows return type. return type can be any type (including string) theoretically return type may be Robot too, but in practice Robot class is not
designed to be used as the return type you do not see any syntax error, but execution may be problematic. So do not
return a Robot from a function Function body may have several statements in it return statement is used to determine the value returned from
function, so the expression after it must be of the return type Function body must include at least one return statement The return statement causes the function to exit immediately and to return
the value after return A function can have more than one return statements, but only one is
executed when the function is called (see next example) Only one return is a good programming style to have control of bigger functions
Functions can return strings
string WeekDay(int day)
// precondition: 0 <= day <= 6
// postcondition: return "Sunday" for 0,
// "Monday" for 1,
// … "Saturday" for 6
{ if (0 == day) return "Sunday"; else if (1 == day) return "Monday"; else if (2 == day) return "Tuesday"; else if (3 == day) return "Wednesday"; else if (4 == day) return "Thursday"; else if (5 == day) return "Friday"; else if (6 == day) return "Saturday";}
A program piece that uses that function
string dayName;int dayNum;cout << " enter day (0-6): ";cin >> dayNum;dayName = WeekDay(dayNum);
Which is/are correct use of WeekDay function? Why?
cout << WeekDay(5) << endl;
int j = WeekDay(0);
cout << WeekDay(2.1) << endl;
string s = WeekDay(22);
WeekDay(3);
Function documentationFunctions usually have a precondition
What conditions (e.g. value of parameters) must be true for the function to work as intended?
If there are no parameters, then no preconditionSome functions work for every parameter value
no precondition
Functions always have a postconditionIf precondition is satisfied what does the function do? What does
the function return?
Example – Compare cost of pizza sizes
Problem: Calculate and compare price per square inch of large and small size pizzas
Solution:A function, say Cost, that takes the pizza radius and price as parameters
and returns price per square inchIn main()
input radiuses and prices of large and small pizzas calculate the per square inch costs by calling the cost function display the results on screen compare the unit costs to find out which one is best value
See pizza2.cpp
Example - When is a year a leap year?Every year divisible by four is a leap year
Except years divisible by 100 are not Except years divisible by 400 are
Alternatively:Every year divisible by 400 is a leap yearOtherwise, years divisible by 100 are not leap years Otherwise, years divisible by 4 are leap yearsOtherwise, not a leap year
Boolean functionbool IsLeapYear(int year);// pre: year > 0// post: return true if year is a leap year
Implementation and use of leap year function
bool IsLeapYear(int year)// precondition: year > 0// postcondition: returns true if year is a leap year, else returns false { if (year % 400 == 0) // divisible by 400 { return true; } else if (year % 100 == 0) // divisible by 100 { return false; } else if (year % 4 == 0) // divisible by 4 { return true; } return false;}int main(){ int year; cout << "enter a year ";
cin >> year; if (IsLeapYear(year)) { cout << year << " has 366 days, it is a leap year" << endl; } else { cout << year << " has 365 days, it is NOT a leap year" << endl; } return 0;}
See isleap.cpp
There’s more than one way No if/else necessary in the function body
bool IsLeapYear(int year)// precondition: year > 0// post: return true if year is a leap year{ return ( year % 400 == 0 ) || ( year % 4 == 0 && year % 100 != 0);}
How does this work? Is this version more efficient? Are these two versions different from user perspective?