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

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

User Defined Functions
1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Starting Out with Java: From Control Structures through Objects Fourth.
PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
Copyright © 2012 Pearson Education, Inc. Chapter 6 Modularizing Your Code with Methods.
Inline Function. 2 Expanded in a line when it is invoked Ie compiler replace the function call with function code To make a function inline the function.
C++ fundamentals.
Introduction to Methods
Chapter 6: Functions.
Unit 2: Java Introduction to Programming 2.1 Initial Example.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++
CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
CPS120: Introduction to Computer Science Functions.
Functions Top-down design Breaking a complex problem into smaller parts that we can understand is a common practice. The process of subdividing a problem.
CPS120: Introduction to Computer Science Lecture 14 Functions.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
A First Book of ANSI C, Fourth Edition1 Functions for Modularity 04/24/15.
Starting Out with Java: From Control Structures through Objects Fifth Edition by Tony Gaddis Chapter 5: Methods.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
1 Methods Introduction to Methods Passing Arguments to a Method More About Local Variables Returning a Value from a Method Problem Solving with Methods.
Python Functions : chapter 3
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
INLS 560 – F UNCTIONS Instructor: Jason Carter.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Methods CSCI 1301 What is a method? A method is a collection of statements that performs a specific task. Pre-Defined methods: available in Java library.
Introduction to Functions CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
Computer Programming A simple example /* HelloWorld: A simple C program */ #include int main (void) { printf (“Hello world!\n”); return.
CSC1201: Programming Language 2 1 Functions. 2 Function declaration: return_type FuncName( Type arg1, Type arg2,….. Type argN) { function body } A program.
1 UMBC CMSC 104, Section Fall 2002 Functions, Part 1 of 3 Topics Top-down Design The Function Concept Using Predefined Functions Programmer-Defined.
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
Functions, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
Programming Fundamentals Enumerations and Functions.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Introduction to Methods ISYS 350. Methods Methods can be used to break a complex program into small, manageable pieces – This approach is known as divide.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
Chapter 6: User-Defined Functions I
REPETITION CONTROL STRUCTURE
Chapter 10: Void Functions
2011/11/20: Lecture 15 CMSC 104, Section 4 Richard Chang
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Starting Out with Programming Logic & Design
Engineering 1020 Introduction to Programming
User Defined Functions
Functions I Creating a programming with small logical units of code.
Chapter 5 Function Basics
Functions, Part 1 of 3 Topics Using Predefined Functions
Modular Programming with Functions
Topics Introduction to Functions Defining and Calling a Void Function
Chapter 6: User-Defined Functions I
Chapter 9: Value-Returning Functions
Starting Out with Programming Logic & Design
Functions, Part 1 of 3 Topics Using Predefined Functions
CS149D Elements of Computer Science
In C Programming Language
Functions, Part 1 of 3 Topics Using Predefined Functions
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Functions I Creating a programming with small logical units of code.
CPS125.
Presentation transcript:

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) int main() { statement; … } Modular (Divide and Conquer) int main() { statement; } void function1() { statement; } void function2() { 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: 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!

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

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.