General Computer Science for Engineers CISC 106 Lecture 30 Dr. John Cavazos Computer and Information Sciences 05/04/2009.

Slides:



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

Lecture Computer Science I - Martin Hardwick Strings #include using namespace std; int main () { string word; cout
Copyright © 2003 Pearson Education, Inc. Slide 1.
Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 5 Functions for All Subtasks.
General Computer Science for Engineers CISC 106 Lecture 28 Dr. John Cavazos Computer and Information Sciences 04/29/2009.
More on Functions Programming. COMP104 Lecture 19 / Slide 2 Passing Parameters by Reference l To have a function with multiple outputs, we have to use.
1 11/05/07CS150 Introduction to Computer Science 1 Functions Chapter 6, page 303.
General Computer Science for Engineers CISC 106 Lecture 32 Dr. John Cavazos Computer and Information Sciences 05/08/2009.
Computer Science 1620 Functions. Given a number n, the factorial of n, written n!, is computed as follows: note: 0! = 1 examples: n! = n x (n-1) x (n-2)
// Functions that take no arguments #include using namespace std; void function1(); void function2( void ); int main() { function1(); function2(); return.
Monday, 9/23/02, Slide #1 CS 106 Intro to CS 1 Monday, 9/23/02  QUESTIONS??  Today:  Discuss Lab 3  Do Exercises  Introduction to functions  Reading:
General Computer Science for Engineers CISC 106 Final Exam Review Dr. John Cavazos Computer and Information Sciences 05/18/2009.
General Computer Science for Engineers CISC 106 Lecture 31 Dr. John Cavazos Computer and Information Sciences 05/06/2009.
Chapter 4 Summation.
Functions:Passing Parameters by Value Programming.
General Computer Science for Engineers CISC 106 Lecture 25 Dr. John Cavazos Computer and Information Sciences 04/20/2009.
General Computer Science for Engineers CISC 106 Lecture 34 Dr. John Cavazos Computer and Information Sciences 05/13/2009.
General Computer Science for Engineers CISC 106 Lecture 33 Dr. John Cavazos Computer and Information Sciences 05/11/2009.
General Computer Science for Engineers CISC 106 Lecture 26 Dr. John Cavazos Computer and Information Sciences 04/24/2009.
#include using namespace std; void main() { cout
Functions. COMP104 Lecture 13 / Slide 2 Function Prototype * The function prototype declares the interface, or input and output parameters of the function,
1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3.
Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.
Functions Modules in C++ are called functions and classes
Chapter 7 Functions.
Programming Functions: Passing Parameters by Reference.
Functions Parameters & Variable Scope Chapter 6. 2 Overview  Using Function Arguments and Parameters  Differences between Value Parameters and Reference.
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
Functions Pass by Reference Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005.
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.
Current Assignments Homework 3 is due tonight. Iteration and basic functions. Exam 1 on Monday.
Functions Modules in C++ are called functions and classes Functions are block of code separated from main() which do a certain task every C++ program must.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
Chapter 6 User-Defined Functions I. Objectives Standard (predefined) functions What are they, and How to use them User-Defined Functions Value returning.
111/15/2015CS150 Introduction to Computer Science 1 Summary  Exam: Friday, October 17,  Assignment: Wednesday, October 15, 2003  We have completed.
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
1 10/18/04CS150 Introduction to Computer Science 1 Functions Divide and Conquer.
1 COMS 261 Computer Science I Title: Functions Date: October 12, 2005 Lecture Number: 17.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.
Function 2. User-Defined Functions C++ programs usually have the following form: // include statements // function prototypes // main() function // function.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
FUNCTIONS - What Is A Function? - Advantages Function Declaration
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
CS1201: Programming Language 2 Function I By: Nouf Aljaffan Edited by : Nouf Almunyif.
1 Structure of Simple C++ Program Chapter 1 09/09/13.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
General Computer Science for Engineers CISC 106 Lecture 27 Dr. John Cavazos Computer and Information Sciences 04/27/2009.
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
Intro to Programming Week # 6 Repetition Structure Lecture # 10
Revision.
Functions and an Introduction to Recursion
CO1401 Programming Design and Implementation
Chapter 5 Function Basics
Name: Rubaisha Rajpoot
CS150 Introduction to Computer Science 1
Starting Out with C++: From Control Structures through Objects
CS150 Introduction to Computer Science 1
Functions and an Introduction to Recursion
Functions Divide and Conquer
CS149D Elements of Computer Science
Functions Imran Rashid CTO at ManiWeber Technologies.
TOPIC: FUNCTION OVERLOADING
Presentation transcript:

General Computer Science for Engineers CISC 106 Lecture 30 Dr. John Cavazos Computer and Information Sciences 05/04/2009

Lecture Overview Definitions and Prototypes Examples Read Chapter 5 (Ullman and Singer)

C++ Function Definition Is the code that implements a function. Example 1: int main() { cout << “Hello World” << endl; } Example 2: int squareArg(int x) { return x * 2; }

C++ Function Definition Example 1: 1. int squareArg(int x) 2. { 3. return x * 2; 4. } 1. Return type, function name, argument lists 2. Open curly brace 3. Function body a) 0 or more return statements 4. Close curly brace

C++ Function Definition Function can take multiple arguments Example 1: int Power(int x, int n) { int powerof = x; for (int i =2; i<=n i++) powerof = powerof * x; return powerof; }

C++ Function Calling Main function calls the function squareArg. Example 1: int squareArg(int x) { return x * 2; } int main() { int x = squareArg(100); cout << “square of 100 is ” << x << endl; }

C++ Function Prototypes When you need to call a function before you call it Function prototypes have a specific structure. They require the following parts: Return Type Function Name Argument (aka. Parameter) List

C++ Function Prototypes Here is what a function prototype looks like: functionName( ); A simple function prototype as a demonstration: int squareArg(int); void printStudentName(string);

C++ Function Prototype Example int i = 100; int squareArg(int); // function prototype int main() { d = squareArg(i); } int squareArg(int x) // function definition { return x * 2; }

C++ Function Calling Mistakes! int i = 100; int squareArg(int); // function prototype int main() { d = squareArg(“Hello”); } int squareArg(int x) // function definition { return x * 2; }

C++ Function Calling Mistakes! int i = 100; int squareArg(int); // function prototype int main() { d = squareArg(i); } int squareArg(int x) // function definition { return “Hello”; }

Putting all together Create a function to sum number from 1 to N What do we need?

C++ Functions: Putting all together #include using namespace std; int sumFrom1ToN(int); int main() { int n = 0, sum = 0; cout << “Enter a number \n”; cin >> n; sum = sumFrom1ToN(n); cout << “The sum of numbers to ” << n; cout << “ is ” << sum << endl; } // the rest of program is on next slide

C++ Functions: Putting all together // main function above this int sumFrom1ToN(int n) { int sum = 0; for (int i = 1; i <= n; i++) { sum = sum + i; } return sum; }