120 likes | 202 Views
Introduction to C++ Functions. Chapter 6 Sections 6.1 – 6.3 Computer II. Modular Programming. The idea that a program can be created from smaller programs, rather than one large program.
E N D
Introduction to C++ Functions Chapter 6 Sections 6.1 – 6.3 Computer II
Modular Programming • The idea that a program can be created from smaller programs, rather than one large program. • “Divide and Conquer” – Method of problem solving by dividing a large problem into several smaller programs. • In C++, a method of modular programming is the use of functions.
What is a Function? • Function – a collection of statements that performs a specific task. • Functions allow: • “Divide and Conquer” programming • Code Reuse ability to reuse code when you need to, rather than continually typing the same statements.
Programming Types Sequential (The “normal” way) Modular (Divide and Conquer) int main() { statement; statement; statement; } void function1() { statement; statement; } void function2() { statement; } int main() { statement; statement; statement; statement; statement; statement; statement; statement; … }
Creating a User-Made Function • To create a function, you must write a function definition. This includes a: • Return type If the function will send back data, what is the data type of it? • Name User-made identifier for the function • Parameter List List of values that are sent to the function (if any). For now, we will not have a parameter list. • Body The statements of code you want to be executed when the function runs.
Creating a User Made Function • Syntax of a Function Definition: returnTypename (parameterList) { statement1; statement2; … } • The line before the body of the function is known as the function header. • Thus, the function header is returnTypename (parameterList) For now, this will be empty. The parentheses ARE always placed, even if there is no parameter list!
The Return Type • Functions can be designed to return data of any type. • For now, we will create functions that do NOT return any data. When a function does not return any data, the data type of the function is void. • void Functions a void function is a function that does not return any data.
What a Void Function Looks Like //Function to print hello on the screen void hello() { cout << “Hello”; } • void is the data type when no data is sent back in a function.
Using a Function • Functions are used by performing a functioncall. When a function is “called”, control in the program moves to the function that is identified. • To perform a function call, the name of the function is used along with its parameter list. • There will ALWAYS be a set of parentheses after the function name!!!
Example • Function: void printGoodbye() { cout << “Goodbye!”; } • The function header is void printGoodbye() • The function call will be printGoodbye();
Function Calls in a Program void displayMessage() { cout << “Hello from displayMessage.\n”; } int main() { cout << “Hello from main.\n”; displayMessage(); //This is the function call cout << “Back in main function.\n”; system(“pause”); }
Functions and Program Flow • When a function is called, the program will branch off to that function and complete its instructions. • Once the function is completed, the program will return to the main function and continue with the next line after the function call.