// Lab 9 Lecture // comment the examples and run individually or several at once // by commenting out the indicated sections of code. // (Eclipse: ctrl+/ to comment and uncomment) // (Code Blocks: ctrl+shift+C to comment and ctrl+shift+x to uncomment) // ---------------------------- Intro to Functions ----------------------------- //// This is a hello world program that uses a function. //// hello_world function will print "Hello World" when called. // //// Note that 'main' is also a function. You have been using functions //// all along and may not have even realized it. // //// this is the syntax for a function definition (with no parameters). //// type name () //// { //// statements //// } // //// Where will this program start? At hello_world or main? // //#include //using namespace std; // //// this is a function definition //void hello_world () //{ // cout << "Hello World"; //} // //int main() //{ // // this is a function call // hello_world(); // // return 0; //} // --------------------------- Function Prototype ----------------------------- //// This program is the same other than it uses a prototype. A prototype //// lets the compiler know that the function definition can be found //// after when it is first called for. // //// Remember, the compiler goes through your program sequentially, so if it //// comes to a function call but has not found the definition yet it will //// get confused. The prototype simply lets the compiler know it will be //// defined later. // // //#include //using namespace std; // //// this is a prototype. Make sure to note the semicolon at the end. //// type name ( parameter1, parameter2, ...) ; //void hello_world(); // //int main() //{ // // Function call // hello_world(); // // return 0; //} // //// Function definition //void hello_world () //{ // cout << "Hello World"; //} // ---------------------------- Function Returns ------------------------------ //// C++ Syntax requires that the declaration of a program begins with a type. //// This is the data type that is returned by the function. If the function //// does not need to return a value 'void' can be used to indicate that the //// function will not return anything. // //// Note that main has an 'int' type, while our hello_world functions have //// a 'void' type. At the end of our main function we have 'return 0'. This //// means that when our main function ends, it will return the value '0' to //// the function that called it. Our hello_world function has no 'return' //// because its type is void. // //// Here are several more examples of functions with different return types //// and how they return values. // //#include //using namespace std; // //// prototypes //int int_func(); //double double_func(); //bool bool_func(); //void void_func(); // //int main() //{ // cout << "At top of main. calling functions: " << endl; // // int i; // double d; // bool b; // // // the value that is returned can be stored in a variable or used as a literal // i = int_func(); // d = double_func(); // b = bool_func(); // // // Why will you get an error if you uncomment this code? //// int v = void_func(); // // void_func(); // // cout << "At bottom of main:" << endl; // cout << "i is : " << i << " d is : " << d << " b is : " << b << endl; // // return 0; //} // //// int_func definition //int int_func() //{ // cout << "Inside int_func, will return value of 100"<< endl; // return 100; //} // //// double_func definition //double double_func() //{ // cout << "Inside double_func, will return value of 2.414"<< endl; // return 2.414; //} // //// bool_func definition //bool bool_func() //{ // cout << "inside bool_func, will return 'true'"<< endl; // return true; //} // //void void_func() //{ // cout << "inside void_func, will not return anything" << endl; // // you can use a return with no value, or nothing at all. // return; //} // ---------------------------- Functions Example ----------------------------- // //#include //using namespace std; // //// prototypes //int get_sum (); // //int main() //{ // int sum; // // sum = get_sum(); // // cout << "sum is :" << sum; // return 0; //} // //// get_sum function takes 2 numbers from a user and returns the sum //int get_sum () //{ // // Note that we need to declare variables inside the function // // because variables in main do not exist here. We will learn // // more about this in the later lessons. // int num1, num2, total; // // cout << "enter two numbers to be added" << endl; // cin >> num1 >> num2; // // total = num1 + num2; // // return total; //}