170 likes | 304 Views
Functions. Prototypes, parameter passing, return values, activation frams. Why Functions?. Suppose you want to print the maximum of two numbers. ... int i1 = 5, i2 = 23; if (i1 > i2) cout << “The max of ” << i1 << “ and ” << i2 << “ is ” << i1 << endl; else
E N D
Functions Prototypes, parameter passing, return values, activation frams
Why Functions? • Suppose you want to print the maximum of two numbers ... int i1 = 5, i2 = 23; if (i1 > i2) • cout << “The max of ” << i1 << “ and ” • << i2 << “ is ” << i1 << endl; else • cout << “The max of ” << i1 << “ and ” • << i2 << “ is ” << i2 << endl; ...
Why Functions? • Now suppose you want to print the maximum of two numbers many times int i1 = 5, i2 = 23; if (i1 > i2) cout << “The max of ” << i1 << “ and ” << i2 << “ is ” << i1 << endl; else cout << “The max of ” << i1 << “ and ” << i2 << “ is ” << i2 << endl; i1 = 17; i2 = 13; if (i1 > i2) cout << “The max of ” << i1 << “ and ” << i2 << “ is ” << i1 << endl; else cout << “The max of ” << i1 << “ and ” << i2 << “ is ” << i2 << endl; i1 = 33; i2 = 33; if (i1 > i2) cout << “The max of ” << i1 << “ and ” << i2 << “ is ” << i1 << endl; else cout << “The max of ” << i1 << “ and ” << i2 << “ is ” << i2 << endl;
A Better Approach int max(int i, int j){ int maxNumber; if (i > j) maxNumber = i; else maxNumber = j; return maxNumber; } int i1 = 5, i2 = 23; cout << “The max of ” << i1 << “ and ” << i2 << “ is ” << max(i1, i2) << endl; i1 = 17; i2 = 13; cout << “The max of ” << i1 << “ and ” << i2 << “ is ” << max(i1, i2) << endl; i1 = 33; i2 = 33; cout << “The max of ” << i1 << “ and ” << i2 << “ is ” << max(i1, i2) << endl;
Why Functions? • Functions • Isolate code for specific task • Make code easier to read and to understand • Confine error search to function definition if bugs • Support and encourage reuse
Example Function /* prime_test.cpp * Author: Richard Newman * Date: 5/25/14 * Test input numbers for primality */ #include <iostream> #include <iomanip> using namespace std; bool isPrime(int candidate){ // naive prime test • // note i <= needed else 4 returns true for (int i = 2; i <= candidate/2; ++i) { if (candidate % i == 0) return false; } return true; }
int main() { int i = 2; cout << "Test for primality" << endl; cout << "Enter 1 to quit" << endl; while (i > 1) { cout << "Number to test: " << flush; cin >> i; if (i != 1) cout << i; if (i < 1) cout << " is not valid" << endl; else if (i > 1) { if (isPrime(i)) cout << " is prime" << endl; else cout << " is not prime" << endl; } } cout << "Bye" << endl; Return 0; }
What is happening? The function has two parts: • Function header • Function body (or definition) bool isPrime(int candidate) { // naive prime test • // note i <= needed else 4 returns true for (int i = 2; i <= candidate/2; ++i) { if (candidate % i == 0) return false; } return true; }
What is happening? The function header has several parts: • Return value type – bool • Function name – isPrime • Formal parameter(s) – int candidate Function signature = Name + formal parameter list bool isPrime(int candidate)
Function Prototypes: Forward Declaration To find the right function to call the compiler has to build into the symbol table not just the function name, but its signature and type. This allows the right call to be made, the right amount of space to be pushed onto the stack, and the types to be checked at compile time bool isPrime(int candidate);
Example Prototype /* prime_test.cpp * Author: Richard Newman * Date: 5/25/14 * Test input numbers for primality */ #include <iostream> #include <iomanip> using namespace std; bool isPrime(int candidate); ... Main() { ... } ... bool isPrime(int candidate){ ... }
Command Line Args Often want to pass command line arguments into program to allow for user to specify inputs, modes, etc. without user interaction while the program is running Great for use in scripts! $ prime_test2 -h -s
Modify to take cmdline args ... const string HELP = "prime_test2 [-s] [-h] \n" "\tCommand line parameters:\n" "\t\t-s: silent mode - no prompts, 0/1 output\n" "\t\t-h: print this help info first\n" "\tInput:\n" "\t\t1 to quit\n" "\t\tnumbers greater than 1 to output primality\n" "\t\tnumbers <1 are invalid\n" "\tOutput:\n" "\t\tOn valid input, states if input is prime or not\n" "\t\tIn silent mode, output is 1 if prime, 0 if not\n"; const string SOPT = "-s"; const string HOPT = "-h";
int main(int argc, char *argv[]) { bool silent = false; // silent mode const bool debug = true; // print info to debug if (debug) { cout << argc << endl; for (int i = 0; i < argc; ++i) { cout << argv[i] << endl; } } for (int n = 1; n < argc; n++) { // parse cmdline if (argv[n] == SOPT) silent = true; else if (argv[n] == HOPT) cout << HELP << endl; else cerr << "Invalid commandline argument: " << argv[n] << endl; } int i = 2; if (!silent) { cout << "Test for primality" << endl; cout << "Enter 1 to quit" << endl; }
while (i > 1) { if (!silent) { cout << "Number to test: " << flush; } cin >> i; if (!silent & (i != 1)) { cout << i; } if (i < 1) { if (!silent) { cout << " is not valid" << endl; } } else if (i > 1) { if (isPrime(i)) { // prime if (!silent) { cout << " is prime" << endl; } else { cout << "1" << endl; } } else { // not prime ...
else { // not prime ... if (!silent) { cout << " is not prime" << endl; } else { cout << "0" << endl; } } } } if (!silent) { cout << "Bye" << endl; } return 0; }