Overview creating your own functions calling your own
- Slides: 30
Overview • creating your own functions • calling your own functions
function definition return-value-type function-name (argument list) { declarations and statements } Example: int Square (int y) { int result; result = y * y; return result; }
function definition return-value-type function-name (argument list) { declarations and statements } • return-value-type - any valid data type, plus void abort () double sqrt (double) int main () • function-name - any valid identifier. Our standard verb, each word capitalized Get. Input Is. Valid Show. Message
function definition return-value-type function-name (argument list) { declarations and statements } • argument list - comma separated list of arguments. Each must have a data type. Okay if function has no arguments. bool Is. Empty() bool Is. Empty(void) double sqrt (double x) double pow( double x, double y ) • Each argument is a declaration. (See square example)
Returning control to caller - no return value void Display. Error. Message(int error. Number) { cout << “Error Message number is “ << error. Number; } or void Display. Error. Message(int error. Number) { cout << “Error Message number is “ << error. Number; return ; }
Returning control to caller with return value bool Is. Empty() { if (0 == buffer. Count ) return true; else return false; }
Exercises 1) write a function which accepts an integer value as input, and returns an integer which is the cube of that input. y int Cube int y 3 2) write a function that accepts a char as input and returns true if the char is a digit from 0 to 9 or false if the character is not a digit from 0 to 9. ch Is. Digit bool true if ch is 0 to 9 char false if ch is not 0 to 9
calling a function int main() { Function 1 (); return 0; } void Function 1() { return; }
function flow of control int main() { cout << “About to call Function 1” << endl; Function 1 (); cout << “Returned from Function 1” << endl; return 0; } void Function 1() { cout << “Inside Function 1” << endl; return; } Demonstrate this
function flow of control - 2 calls int main() { cout << “About to call Function 1” << endl; Function 1 (); cout << “Returned from Function 1” << endl; cout << “calling Function 1 again” << endl; Function 1 (); cout << “Returned from calling Function 1 again” << endl; return 0; } void Function 1() { cout << “Inside Function 1” << endl; return; } Demonstrate this
benefits of functions • Function code implemented once. Changes happen in one place • main routine easier to understand because details of Function 1 removed. • When writing main routine only have to think about main. When writing Function 1 only have to think about Function 1
cout statements as a debug technique • can determine flow of control • can determine how and when variables change.
Inputs and outputs int main () { int x. Squared, int x = 10; x. Squared = Square (x); cout << “ the square of “ << x << “ is “ << x. Squared; return 0; } int Square (int y) { int result; result = y * y; return result; } What happens to x. Squared, x, y, result? Walk through. Show memory
variables inside a function are not visible outside the function int main () { int x. Squared; for (int x = 1; x <= 10; x++) { x. Squared = Square (x); cout << x. Squared << “ “ ; } cout << result; // would cause a compiler error return 0; } int Square (int y) { int result; result = y * y; return result; }
y is not visible outside the function int main () { int x. Squared; for (int x = 1; x <= 10; x++) { x. Squared = Square (x); cout << x. Squared << “ “ ; } cout << y; // would cause a compiler error return 0; } int Square (int y) { int result; result = y * y; return result; }
Exercises 1) Write a main function which calls the Cube function that you wrote earlier. Have it calculate the cube of the numbers from 1 to 10 and display them. int Cube (int y) 2) Write a main function which calls the Is. Digit function that you wrote earlier. Have it read in 10 input characters from a user and for each one, display whether it is a digit or not. bool Is. Digit (char ch)
function prototype Tells compiler: • type of data returned from function • number of arguments function expects to receive • type of arguments the function expects to receive • order in which those arguments are expected. Example: int Square (int y);
function prototype Compiler must see either the function itself or the function prototype before the function is actually called in the code. int Square (int y); int main () { int x. Squared; for (int x = 1; x <= 10; x++) { x. Squared = Square (x); cout << x. Squared << “ “ ; } return 0; } Function itself may be in a different file as it is with library functions
Area of a Triangle side 1 (float) side 2 (float) side 3 (float) Area. Triangle area of the triangle (float)
#include < iostream> #include < cmath> using namespace std; float Area. Triangle (float side 1, float side 2, float side 3); // prototype int main () { float a, b, c; // the three sides of the triangle float area; cout << endl << "This program calculates the area of a triangle"; cout << endl << "with sides of length 3. 0, 4. 0, and 5. 0" << endl; a = 3. 0; b = 4. 0; c = 5. 0; area = Area. Triangle(a, b, c); cout << endl << "The area of the triangle is " << area << endl; return 0; } /* * PRE: side 1, side 2, and side 3 are positive numbers that * form the sides of a triangle * POST: returns the area of a triangle with sides side 1, * side 2, side 3 */ float Area. Triangle (float side 1, float side 2, float side 3) { float s; // local variable - the semiperimiter s = (side 1 + side 2 + side 3) / 2. 0; return (sqrt ( s * (s - side 1) * (s - side 2) * (s - side 3) ) ); }
variables s side 3 side 2 side 1 6. 0 5. 0 4. 0 3. 0 area c b a 6. 0 5. 0 4. 0 3. 0 s, side 1, side 2, side 3 are variables in the Area. Triangle function area, a, b and c are variables in the main program At runtime a copy of a, b, c is made and used to initialize side 1, side 2 and side 3
miscellaneous • you could skip writing a prototype all together and just put the function ahead of main. Not intuitive. Makes source code hard to read. • you could implement Area. Triangle initially as a stub with no other code except return 0;
1) Write a function named Smallest that takes three integer inputs and returns an integer that is the smallest of the three inputs. Write the prototype for the Smallest function. Write a program that gets 3 integers from a user and displays the smallest 2) Write a function that, given a letter of the alphabet, returns true if the letter is a vowel (lower or uppercase) and returns false if the letter is not a vowel. letter (char) Is. AVowel true if letter is a vowel false if letter is not a vowel 3) Write the prototype for Is. AVowel. Write a program to invoke the Is. AVowel function. Inputs a letter and prints out whether it is or is not a vowel.
Automatic conversions What if the parameter you want to send is different than that expected by the function? double Square (double y); int main () { double x. Squared; for (int x = 1; x <= 10; x++) { x. Squared = Square (x); cout << x. Squared << “ “ ; } return 0; } x converted to double. Works fine.
Automatic conversions What if the parameter you want to send is different than that expected by the function? int Square (int y); int main () { double d 1 = 9. 8; cout << Square (d 1); // displays 81 return 0; } d 1 converted to int. Information is lost. Beware!
Promotion rules • Specify which types can be converted to other types without losing data. • As long as you follow the promotion rules then conversions are okay. • Must promote to a data type higher in the hierarchy
Promotion hierarchy long double float unsigned long int unsigned short int unsigned char
Common programming error • Losing data by allowing the compiler to change a data type and not follow the promotion rules.
Exercises 1) Find the error in each of the following program segments and explain how to fix it. a) int sum (int x, int y) { int result; result = x + y; } b) int sum (int n) { if (0 == n) return 0; else n = n + n; } c) in main program: double x = 1 E 10; cout << "square of 1 E 10 = " << square (x) << endl; int square (int x) return x * x; } {
More Exercises 1) Find the error in the following function and fix it. void display. Error. Message (int error. Number) { switch (error. Number) { case 0: cout << "Fatal Error!" << endl; break; case 1: cout << "Error!" << endl; break; default: cout << "Invalid error code" << endl; } return true; }
- Declaration in writing
- What type of sae is when you own your own business
- Create your own planet
- Walk worthy of your calling
- One hope of your calling
- Know the hope of your calling
- You must disclose your name when calling the complyline
- Walk worthy of your calling
- Effortful processing definition
- Your conscious awareness of your own name and self identity
- Absolute value function to piecewise function
- I can evaluate functions
- Evaluating functions and operations on functions
- Recruiter call script to candidate
- Burger king name calling
- Real men use macs
- Examples of name-calling
- Whats plain folks
- Name calling examples
- Who is propaganda
- Bandwagon appeal meaning
- Glittering generalities
- Cloud pstn
- Name calling
- Bakit mahalagang magkaroon ng tamang direksyon sa buhay
- Baseball personification examples
- Cold calling basics
- Name calling commercials examples
- Walk worthy of the vocation
- Softly and tenderly jesus is calling
- Snob appeal propaganda definition