1 / 12

Introduction to C++ Functions

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.

magnar
Télécharger la présentation

Introduction to C++ 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. 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) 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; … }

  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: 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!

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

  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.

More Related