Handout on Functions Passing Values to Functions #include int mult(int, int); int main() { int a = 10, b = 20; cout << mult(a, b); //cout << x << y; //

Slides:



Advertisements
Similar presentations
1 Pointers and Strings Section 5.4, , Lecture 12.
Advertisements

1 Programming in C++ Lecture Notes 9 Functions (Returning Values) Andreas Savva.
More Pointers Write a program that: –Calls a function to input an integer value –The above function calls another function that will double the input value.
Engineering Problem Solving With C++ An Object Based Approach Chapter 9 Pointers and Creating Data Structures.
Friday, January 19, 2007 Anyone who has never made a mistake has never tried anything new. -Albert Einstein.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
Tuesday, January 23, 2007 "We can't solve problems by using the same kind of thinking we used when we created them." -Albert Einstein.
Tuesday, February 6, 2007 Invest yourself in everything you do. There’s fun in being serious.” “Invest yourself in everything you do. There’s fun in being.
Command-line arguments CS 201 Fundamental Structures of Computer Science.
Tuesday, January 16, 2007 How would a car function if it were designed like a computer? Occasionally, executing a maneuver would cause your car to stop.
Programming Review: Functions, pointers and strings.
Thursday, January 18, 2007 The question of whether computers can think is just like the question of whether submarines can swim. -Edsger W. Dijkstra (1930.
CS 192 Lecture 15 Winter 2003 January 16, 2004 Dr. Shafay Shamail.
1 Functions and Structured Programming. 2 Structured Programming Structured programming is a problem-solving strategy and a programming methodology. –The.
Exercise 10 Review: pointers, strings and recursion.
1 Chapter 9 Pointers. 2 Topics 8.1 Getting the Address of a Variable 8.2 Pointer Variables 8.3 Relationship Between Arrays and Pointers 8.4 Pointer Arithmetic.
Command line arguments. – main can take two arguments conventionally called argc and argv. – Information regarding command line arguments are passed to.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
Lesson 9 Pointers CS 1 Lesson 9 -- John Cole1. Pointers in Wonderland The name of the song is called ‘Haddock’s Eyes’.” “Oh, that’s the name of the song,
Variables, Functions & Parameter Passing CSci 588 Fall 2013 All material not from online sources copyright © Travis Desell, 2011.
Chapter 9 Pointers Fall 2005 Csc 125 Introduction to C++
Pointers Chapter 9. Getting The Address Of A Variable Each variable in program is stored at a unique address Use address operator & to get address of.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9: Pointers.
0 Chap. 5 Pointers and Arrays 5.3Pointers and Arrays 5.4Address Arithmetic 5.5Character Pointers and Functions 5.6Pointer Arrays; Pointers to Pointers.
Introduction to Computer Algorithmics and Programming Ceng 113 Functions and Recursive Functions.
ITEC 320 C++ Examples.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Command Line Arguments.
 Building blocks of a C++ program  Each function has a name, which is used to call the function; functions call each other  You will write your own.
1 Command-Line Processing In many operating systems, command-line options are allowed to input parameters to the program SomeProgram Param1 Param2 Param3.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
1 CSE 2341 Object Oriented Programming with C++ Note Set #2.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 22: Pointers.
Functions & Pointers in C Jordan Erenrich
Pointers *, &, array similarities, functions, sizeof.
12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction.
1 Programming Principles II Lecture Notes 4 Functions (Returning Values) Andreas Savva.
1 CSC103: Introduction to Computer and Programming Lecture No 19.
DCT1063 Programming 2 CHAPTER 1 POINTERS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
1 Object-Oriented Programming Using C++ A tutorial for pointers.
Pointers and Arrays An array's name is a constant whose value is the address of the array's first element. For this reason, the value of an array's name.
Arrays, Strings, and Memory. Command Line Arguments #include int main(int argc, char *argv[]) { int i; printf("Arg# Contents\n"); for (i = 0; i < argc;
1. Pointers –Powerful, but difficult to master –Simulate pass-by-reference –Close relationship with arrays and strings 2.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
1  Lecture 12 – Pointer FTMK, UTeM – Sem /2014.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Pointers  * symbol and & symbol  Pointer operations  Pointer.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
Chapter 5 Pointers and Arrays Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
Pointers A variable that holds an address value is called a pointer variable, or simply a pointer.  What is the data type of pointer variables? It’s not.
Pointers Data Structures CSI 312 CSI Dept. Dr. Yousef Qawqzeh.
Standard Version of Starting Out with C++, 4th Edition
Command Line Arguments
Command Line Arguments
Functions and Structured Programming
Learning Objectives Pointers Pointer in function call
CSE 303 Concepts and Tools for Software Development
Command Line Arguments
Programming -2 برمجة -2 المحاضرة-5 Lecture-5.
Pointers & Functions.
Lec 14 Oct 23, 02.
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Strings Adapted from Dr. Mary Eberlein, UT Austin.
Pointers and dynamic objects
Pointers & Functions.
Standard Version of Starting Out with C++, 4th Edition
Strings Adapted from Dr. Mary Eberlein, UT Austin.
C++, Sorting, Convex Hull
Presentation transcript:

Handout on Functions Passing Values to Functions #include int mult(int, int); int main() { int a = 10, b = 20; cout << mult(a, b); //cout << x << y; // *** Error *** --unknown identifiers x, y return 0; } int mult(int x, int y)// can have different names here { return x*y; }

Handout on Functions Passing Pointers to Functions #include void f(int *j);//or void f(int *); int main() { int i; int *p; p = &i; // p now points to i f(p); cout << i; // i is now 100 return 0; } void f(int *j) { *j = 100; // var pointed to by j is assigned 100 }

Handout on Functions Passing Pointers to Functions #include void f(int *j); int main() { int i; f(&i); cout << i; return 0; } void f(int *j) { *j = 100; // var pointed to by j // is assigned 100 } #include int sqr_it(int x); int main() { int t=10; cout << sqr_it(t) << ' ' << t; //output? } int sqr_it(int x) { x = x*x; return x; }

Handout on Functions Passing Arrays to Functions #include void display(int num[10]); //or display(int [10]); int main() { int t[10],i; for(i=0; i<10; ++i) t[i]=i; display(t); // pass array t to a function cout <<endl; for(i=0; i<10; i++) cout << t[i] << ' '; //output? } // Print some numbers. void display(int num[10]) { int i; for(i=0; i<10; i++) cout << num[i] << ' '; //output? for(i=0; i<10; i++) (num[i] = num[i] + 1); }

Handout on Functions Passing Arrays to Functions #include void cube(int *n, int num); int main() { int i, nums[10]; for(i=0; i<10; i++) nums[i] = i+1; cout << "Original contents: "; for(i=0; i<10; i++) cout << nums[i] << ' '; cout << '\n'; cube(nums, 10); // compute cubes cout << "Altered contents: "; for(i=0; i<10; i++) cout << nums[i] << ' '; return 0; } void cube(int *n, int num) { while(num) { *n = *n * *n * *n; num--; n++; } Original contents: Altered contents:

Handout on Functions Passing Strings to Functions #include void stringupper(char *str); int main() { char str[80]; strcpy(str, "this is a test"); stringupper(str); cout << str; // display uppercase string return 0; } void stringupper(char *str) { while(*str) { *str = toupper(*str); // uppercase one char str++; // move on to next char }

Handout on Functions Returning Pointers #include char *get_substr(char *sub, char *str); int main() { char *substr; substr = get_substr("three", "one two three four"); cout << "substring found: " << substr; return 0; } char *get_substr(char *sub, char *str) { int t; char *p, *p2, *start; for(t=0; str[t]; t++) { p = &str[t]; // reset pointers start = p; p2 = sub; while(*p2 && *p2==*p) { //check for substring p++; p2++; } /* If at end of p2 (i.e., substring), then a match has been found. */ if(!*p2) return start; /* return pointer to beginning of substring */ } return 0; // no match found }

Handout on Functions Command Line Arguments #include int main(int argc, char *argv[]) { if(argc!=2) { cout << "You forgot to type your name.\n"; return 1; } cout << "Hello " << argv[1] << '\n'; return 0; } #include int main(int argc, char *argv[]) { int t, i; for(t=0; t<argc; ++t) {// t denotes the t th string i = 0; while(argv[t][i]) {// t[i] accesses the i th character of t cout << argv[t][i]; ++i; cout << ' '; } cout << ' '; } return 0; }

Handout on Functions Command Line Arguments #include int main(int argc, char *argv[]) { double a, b; a = atof(argv[1]); b = atof(argv[2]); cout << a + b; return 0; } #include int main(int argc, char *argv[]) { while(--argc > 0) cout << *++argv << endl; return 0; } #include int main(int argc, char *argv[]) { for(int j=0; j<argc; j++) cout << argv[j] << endl; return 0; } #include int main(int argc, char *argv[]) { char **argvector = argv; double a, b; a = atof(*++argvector); b = atof(*++argvector); cout << a + b; return 0; }

Handout on Functions Passing Reference to Functions #include void sqr_it(int &x); int main() { int t = 10; cout << "Old value for t: " << t << '\n'; sqr_it(t); // pass address of t to sqr_it() cout << "New value for t: " << t << '\n'; return 0; } void sqr_it(int &x) { x *= x; // this modifies calling argument t }

Handout on Functions Returning References from Functions #include double &f(); double val = 100.0; int main() { double newval; cout << f() << '\n'; // display val's value newval = f(); // assign value of val to newval cout << newval << '\n'; // display newval's value f() = 99.1; // change val's value cout << f() << '\n'; // display val's new value return 0; } double &f() { return val; // return reference to val }

Handout on Functions Returning References from Functions #include double &change_it(int i); // return a reference double vals[] = {1.1, 2.2, 3.3, 4.4, 5.5}; int main() { int i; cout << "Here are the original values: "; for(i=0; i<5; i++) cout << vals[i] << ' '; cout << '\n'; change_it(1) = ; // change 2nd element change_it(3) = -98.8; // change 4th element cout << "Here are the changed values: "; for(i=0; i<5; i++) cout << vals[i] << ' '; cout << '\n'; return 0; } double &change_it(int i) { return vals[i]; // return a reference to the ith element } Output: (the changed values)