CS 1400 4 Oct 2006 Chap 6. Functions General form; type Name ( parameters ) { … return value ; }

Slides:



Advertisements
Similar presentations
Functions Prototypes, parameter passing, return values, activation frams.
Advertisements

Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
AU/MITM/1.6 By Mohammed A. Saleh 1. Arguments passed by reference  Until now, in all the functions we have seen, the arguments passed to the functions.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 5. Functions.
CS 1400 Chap 6. Functions General form; type Name ( parameters ) { … return value ; } parameters is a list of comma-separated declarations.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 6 Functions.
1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
1 CIS 205 Practice Test George Lamperti A word that has a predefined meaning in a C++ program and cannot be used as a variable name is known as.
1 11/05/07CS150 Introduction to Computer Science 1 Functions Chapter 6, page 303.
CS Feb 2007 Chap 6. Functions General form; type Name ( parameters ) { … return value ; }
CS 1400 March 30, 2007 Chapter 8 Searching and Sorting.
CS 1400 March 21, 2007 Odds and Ends Review. Reading to the end of a file Example test.txt : … Suppose we need.
CS 117 Spring 2002 Review for Exam 2 March 6, 2002 open book, 1 page of notes.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 6: Functions by.
CS 1400 Apr 6, 2007 Chapters 9, 10 C-strings and Pointers.
CS 1400 Chapter 7 ARRAYS. Array variables Simple variables can hold single values int x;// x can hold one integer float y; // y can hold one float Array.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
CS 1400 Chapters 9, 10 C-strings and Pointers. A few C-string library functions… #include int strcmp (char a[ ], char b[ ]); returns 0 if string a is.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
CS 1400 Chap 6 Functions. Library routines are functions! root = sqrt (a); power = pow (b, c); function name argument arguments.
CS 1400 Chapter 7 ARRAYS. Array variables Simple variables can hold single values int x;// x can hold one integer float y; // y can hold one float Array.
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
Program structure Four different storage-class specifications: –Automatic (auto): local to a function, normally declared variables are automatic. Does.
18-2 Understand “Scope” of an Identifier Know the Storage Classes of variables and functions Related Chapter: ABC 5.10, 5.11.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 6 Functions.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
Array Processing - 2. Objectives Demonstrate a swap. Demonstrate a linear search of an unsorted array Demonstrate how to search an array for a high value.
Chapter 6 Arrays Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Palindromes revisited Here's a simpler program for checking palindromes: int nums[100]; int i = 0, a; cin >> a; while(a > 0) { nums[i++] = a; cin >> a;
Chapter 7 Templates. Objectives Introduction Function Templates Class Templates Standard Template Library.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
Dale Roberts CSCI 230 Functions Scope, Parameter Passing, Storage Specifiers Department of Computer and Information Science, School of Science, IUPUI Dale.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
CS 261 – Data Structures C Pointers Review. C is Pass By Value Pass-by-value: a copy of the argument is passed in to a parameter void foo (int a) { a.
12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
Functions & Pointers in C Jordan Erenrich
Functions Sujana Jyothi C++ Workshop Day 2. Functions 3 Parameter transmission modes pass by value (default) pass by reference (&) pass by const reference.
More Array Access Examples Here is an example showing array access logic: const int MAXSTUDENTS = 100; int Test[MAXSTUDENTS]; int numStudents = 0;... //
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Brief Edition Chapter 6 Functions.
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 Introduction An array is a collection of identical boxes.
Function User defined function is a code segment (block) that perform an specific action. Function Definition: Function Definition: Return_DT F_name (
Functions Structured Programming. Topics to be covered Introduction to Functions Defining a function Calling a function Arguments, local variables and.
Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.
Class Method Read class Student { private: string id; string firstName, lastName; float gpa; public: // Will the method change any data members? // Yes!
1 Parameter passing Call by value The caller evaluates the actual parameters and passes copies of their values to the called function. Changes to the copies.
13/10/2016CS150 Introduction to Computer Science 1 Multidimensional Arrays  Arrays can have more than one column  Two dimensional arrays have two columns.
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.
Passing Function Arguments by Reference : A Sample Lesson for CS 1 using the C Programming Language Andy D. Digh Friday, May 29th, 1998.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
Static Variables. Function Scope  “Normal” local variables go out of scope and are deallocated when a function terminates.
A Lecture for the c++ Course
CS 1430: Programming in C++.
Scope, Parameter Passing, Storage Specifiers
CS150 Introduction to Computer Science 1
CMSC 202 Lesson 22 Templates I.
Parameter Passing in Java
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Templates I CMSC 202.
Fundamental Programming
CS150 Introduction to Computer Science 1
Names of variables, functions, classes
Presentation transcript:

CS Oct 2006 Chap 6

Functions General form; type Name ( parameters ) { … return value ; }

types… The return value of a function must match the function type. Arguments passed to function parameters must match in type and in position. if either of these rules is neglected, the computer will force (cast) a match.

value passing… int MyFunc (float x, char y) { … return 55; } int main() { int z; z = MyFunc (3.14, ‘F’);

Global variables… Variables declared outside of main() or a function are globally accessible. int x; int main() { cout << x; … Local variables may eclipse global variables int x; int main() { int x; cout << x; …

Static variables… Static variables retain their values when a function exits and are only created and initialized once void MyFunc () { static int counter = 0; cout << counter++; } int main() {MyFunc();// outputs 0 MyFunc();// outputs 1 MyFunc();// outputs 2…

Reference parameters… A function using reference parameters can modify corresponding arguments void Swap (int &a, int &b) { int temp = a; a = b; b = temp; } int main() { … Swap (x, y); Swap (cost, rate);

The value passed or copied into a reference parameter is a forwarding address… main() Swap() x y cost rate a b see x in main() see y in main() temp Swap (x, y);

Results… main() Swap() x y cost rate a b see x in main() see y in main() temp

main() Swap() x y cost rate a b see cost in main() see rate in main() temp Swap (cost, rate);

main() Swap() x y cost rate a b see cost in main() see rate in main() temp Results… 3912

Example: MaxMin() The function MaxMin() is intended to determine two values; the maximum and minimum values of a list of 10 numbers input by the user

Quiz Suppose the library function abs() was not available… Write a possible substitution for this function that could be added to your program. Example segment using abs() cout << abs(x); cout << abs(y-x);

Linear Search Search array list with size elements: find the position of variable value. bool found = false; int position = -1; for (int n=0; n<size && !found; n++) { if (list[n] == value) { position = n; found = true; }

Passing arrays as parameters… Array parameters are declared with empty brackets; int MyFunction (float array[ ]); Usually, a function with an array parameter will need to know the size of the array or the number of elements to be considered; int MyFunction2 (float array[ ], int size); Array parameters are always reference parameters!

Linear Search as a function… int LinearSearch (int list[], int size, int value) { bool found = false; int position = -1; for (int n=0; n<size && !found; n++) { if (list[n] == value) { position = n; found = true; } return position; }