C++ Functions Revisited INFSY 307 C++. Must always consider three parts of the Program: Function Prototype: Can be in the header or top of a function.

Slides:



Advertisements
Similar presentations
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions.
Advertisements

Functions Prototypes, parameter passing, return values, activation frams.
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
Functions ROBERT REAVES. Functions  Interface – the formal description of what a subprogram does and how we communicate with it  Encapsulation – Hiding.
Overview Reference parameters Documenting functions A game of craps. Design, code, test and document.
CS 201 Functions Debzani Deb.
Functions Pass by Value Pass by Reference IC 210.
Chapter 6: User-Defined Functions I
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
COMPUTER PROGRAMMING. Functions What is a function? A function is a group of statements that is executed when it is called from some point of the program.
CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6: Functions Starting Out with C++ Early Objects Seventh Edition.
CMSC 1041 Functions II Functions that return a value.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
ECE122 Feb. 22, Any question on Vehicle sample code?
COMPUTER PROGRAMMING. Functions’ review What is a function? A function is a group of statements that is executed when it is called from some point of.
Executable Statements 1. Def. Executable statements are instructions to the computer to perform specific tasks. 2. Two examples: method calls and assignment.
Flow of Control and Program Style n Nested if statements n C++ struct n Program Style n Lab Exercise.
Lecture 15: Projects Using Similar Data. What is an Array? An array is a data structure consisting of related data items of the same type. Stored in a.
Sudeshna Sarkar, IIT Kharagpur 1 Functions Lecture
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
Functions Chapter 6. Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules Function: a collection.
Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Modularity using Functions Chapter 4. Modularity In programming blocks of code often can be "called up" and reused whenever necessary, for example code.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Functions (2)
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
1 UMBC CMSC 104, Section Fall 2002 Functions, Part 1 of 3 Topics Top-down Design The Function Concept Using Predefined Functions Programmer-Defined.
Lecture 4 – Function (Part 1) FTMK, UTeM – Sem /2014.
Lab 9 Exercises.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
Functions, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
Chapter 6 Functions. 6-2 Topics 6.1 Modular Programming 6.2 Defining and Calling Functions 6.3 Function Prototypes 6.4 Sending Data into a Function 6.5.
Lesson xx Why use functions Program that needs a function Function header Function body Program rewritten using a function.
Week 4 – Functions Coding Functions. Purpose of Coding Functions A function is written to perform a well-defined task; rather than having all logic in.
Chapter 6: User-Defined Functions I
Function Topic 4.
CSCI 161: Introduction to Programming Function
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Multiple Files Revisited
Functions I Creating a programming with small logical units of code.
Chapter 6: Functions Starting Out with C++ Early Objects Ninth Edition
Functions.
CS150 Introduction to Computer Science 1
Functions, Part 1 of 3 Topics Using Predefined Functions
Lec 14 Oct 23, 02.
Default Arguments.
CS150 Introduction to Computer Science 1
Chapter 6: User-Defined Functions I
Functions, Part 1 of 3 Topics Using Predefined Functions
CS150 Introduction to Computer Science 1
do/while Selection Structure
CS150 Introduction to Computer Science 1
Fundamental Programming
Functions Extra Examples.
Functions Imran Rashid CTO at ManiWeber Technologies.
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.
Presentation transcript:

C++ Functions Revisited INFSY 307 C++

Must always consider three parts of the Program: Function Prototype: Can be in the header or top of a function (including main) 2.Function Call: May be in ANY function (main ( ) or any other) 3.Function Header: Indicates the top of the function.

C++ Functions n Function Prototype void get_number (); n Function Call m.get_number (); n Function void math::get_number () Note: 1) format of each! 2) typing and argument consistency!

C++ Function – given a return value n Function Prototype int get_number (); n Function Call int x; x=m.get_number (); n Function int math::get_number () { int num; num=read_char_to_int (); return num; }

C++ Functions n Function name – Name must correspond to name in prototype n Function type must correspond to type in prototype n Local variables only available during function execution/global variables do not lose value int math::get_number () { int num; num=read_char_toint (); return num; }

C++ Functions CONSIDERATIONS! n Provide function PRE and POST condition comments under EACH the prototype Others must know what is the beginning state of associated variables, what the function does, and what to expect as complete after execution.

Function Arguments: 1.Are contained in each critical component: a)Function call b)Function prototype c)Function Heading 2.Must match as far as: a)type b)order

C++ Sample Function with argument list and a return value void samp::output (double value, int years, char name []) { cout <<setw (20)<<“ “<< “Name is: “<<name<<endl; cout<<setw (20)<<“ “<<“Years in Program: “years<<endl; cout<<setw (20)<<“ “<<“GPA: “<<value<<endl; return; } int main () samp s; char name []; int yrs; double gpa; s.output (gpa, yrs, name); //assume are defined.