Lec3. C++ Functions
Lec3. C++ Functions
Function Basics
3-2
Predefined Functions
• Must "#include" appropriate library such as
– <cmath>, <cstdlib> (Original "C" libraries)
– <iostream> (for cout, cin)
3-5
Predefined void Functions
• No returned value: Performs an action, but sends no "answer"
– exit(1); // No return value, so not assigned
• This call terminates program
• void functions can still have arguments
• same as functions that "return a value“ but just don’t return a value!
#include <iostream>
#include <cstdlib>
using namespace std;
int main( ){
cout << "Hello Out There!\n";
exit(1);
cout << "This statement is pointless,\n"
<< "because it will never be executed.\n"
<< "This is just a toy program to illustrate exit.\n";
return 0;
}
3-6
Random Number Generator
• Return "randomly chosen" number
– rand() : Takes no arguments and returns value between 0 & RAND_MAX
– Scaling: Squeezes random number into smaller range
• rand() % 6 : Returns random value between 0 & 5
• "%" is modulus operator (remainder)
– Shifting : rand() % 6 + 1
• Shifts range between 1 & 6 (e.g., die roll)
• Random Examples
– Random double between 0.0 & 1.0:
(RAND_MAX – rand())/static_cast<double>(RAND_MAX)
• Type cast used to force double-precision division
– Random int between 10 & 20: rand() % 10 + 10
3-7
Random Number Seed
• Pseudorandom numbers
– Calls to rand() produce given "sequence“ of random numbers
• Use "seed" to alter sequence: srand(seed_value);
– void function that receives one argument, the "seed"
– Can use any seed value, including system time: srand(time(0));
• time() returns system time as numeric value
• Library <time> contains time() functions
3-8
A Function Using a Random
Number Generator
3-9
Programmer-Defined Functions
• Write your own functions!
• Building blocks of programs
– Divide & Conquer, Readability, Re-use
• Your "definition" can go in either:
– Same file as main()
– Separate file so others can use it, too
• Components of Function Use : 3 Pieces
– Function Declaration/prototype: Information for compiler to properly
interpret calls
– Function Definition: Actual implementation/code for what
function does
– Function Call : Transfer control to function
3-10
Function Declaration/prototype
– Syntax: <return_type> FnName(<formal-parameter-list>);
double totalCost(int numberParameter, double priceParameter);
• Placed before any calls in declaration space of main() or above
main() in global space
Function Definition
double totalCost(int numberParameter, double priceParameter)
{
const double TAXRATE = 0.05;
double subTotal;
subtotal = priceParameter * numberParameter;
return (subtotal + subtotal * TAXRATE);
}
3-12
Functions Calling Functions
• We’re already doing this!
– main() IS a function!
• Only requirement: Function’s declaration must appear first
• Common for functions to call many other functions
• Function can even call itself "Recursion"
3-13
Boolean Return-Type Functions & Void Function
• Boolean Return-Type Functions: returns "true" or "false"
bool appropriate (int rate) {
return (((rate>=10)&&(rate<20))||(rate==0);
}
3-15
main(): "Special"
• Recall: main() IS a function
• "Special" in that: One and only one function called main() will exist in a
program
• Who calls main()?
– Operating system
– Tradition holds it should have return statement
• Value returned to "caller" Here: operating system
– Should return "int" or "void"
3-16
Scope Rules
• Local variables: data declared inside body of given function are
available only within that function
• Can have variables with same names declared in different functions
– Scope is local: "that function is it’s scope"
• Global constants and clobal cariables: declared "outside"
function body and global to all functions in that file
– Global declarations typical for constants: const double TAXRATE = 0.05;
– Global variables: Possible, but SELDOM-USED
• Local variables preferred: Functions should declare whatever
local data needed to "do their job“
• Blocks-scope: scope of data declared inside compound statement
for (int ctr=0;ctr<10;ctr++){
sum+=ctr;
}
– Variable ctr has scope in loop body block only 3-17
Enter a radius to use for both a circle
and a sphere (in inches): 2
Radius = 2 inches
Area of circle = 12.5664 square inches
Volume of sphere = 33.5103 cubic inches
3-18