1 / 16

Functions

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

coral
Download Presentation

Functions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Functions Prototypes, parameter passing, return values, activation frams

  2. 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; ...

  3. 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;

  4. 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;

  5. 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

  6. 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; }

  7. 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; }

  8. 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; }

  9. 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)

  10. 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);

  11. 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){ ... }

  12. 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

  13. 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";

  14. 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; }

  15. 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 ...

  16. else { // not prime ... if (!silent) { cout << " is not prime" << endl; } else { cout << "0" << endl; } } } } if (!silent) { cout << "Bye" << endl; } return 0; }

More Related