Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to C++ Functions Chapter 6 Sections 6.1 – 6.3 Computer II.

Similar presentations

Presentation on theme: "Introduction to C++ Functions Chapter 6 Sections 6.1 – 6.3 Computer II."— Presentation transcript:

1 Introduction to C++ Functions Chapter 6 Sections 6.1 – 6.3 Computer II

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

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

4 Programming Types Sequential (The “normal” way) int main() { statement; … } Modular (Divide and Conquer) int main() { statement; } void function1() { statement; } void function2() { statement; }

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

6 Creating a User Made Function Syntax of a Function Definition: returnType name (parameterList) { statement1; statement2; … } The line before the body of the function is known as the function header. – Thus, the function header is returnType name (parameterList) For now, this will be empty. The parentheses ARE always placed, even if there is no parameter list!

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

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

9 Using a Function Functions are used by performing a function call. 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!!!

10 Example Function: void printGoodbye() { cout << “Goodbye!”; } The function header is void printGoodbye() The function call will be printGoodbye();

11 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”); }

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

Download ppt "Introduction to C++ Functions Chapter 6 Sections 6.1 – 6.3 Computer II."

Similar presentations

Ads by Google