Passing Arrays to functions

Slides:



Advertisements
Similar presentations
Lecture Computer Science I - Martin Hardwick Strings #include using namespace std; int main () { string word; cout
Advertisements

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)
Chapter Five Functions
1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
Tinaliah, S. Kom.. * * * * * * * * * * * * * * * * * #include using namespace std; void main () { for (int i = 1; i
Triana Elizabeth, S.Kom. #include using namespace std; void main () { for (int i = 1; i
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 34 Dr. John Cavazos Computer and Information Sciences 05/13/2009.
C++ function call by value The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter.
Functions Pass by Reference Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005.
Searching and Sorting Techniques 1. To learn and appreciate the following concepts Searching Technique  Linear Search Sorting Technique  Bubble Sort.
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.
Engineering Problem Solving with C++, Second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 5 Parameter Passing 11/06/13.
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
Lecture 5 functions 1 © by Pearson Education, Inc. All Rights Reserved.
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014.
Prepared by Andrew Jung. Accessing Pointer Data Pointer can be used to access the contents of an array Look at the following syntax: /* Declaration and.
Class and Objects UNIT II.
Command Line Arguments
Functions and an Introduction to Recursion
CSC113: Computer Programming (Theory = 03, Lab = 01)
CSC113: Computer Programming (Theory = 03, Lab = 01)
Lecture 8 – 9 Arrays with in a class
Dynamic Memory Allocation Reference Variables
User-defined Functions
CSC1201: Programming Language 2
Arrays Skill Area 315 Part A
Contents Introduction to Constructor Characteristics of Constructor
CS1201: Programming Language 2
Name: Rubaisha Rajpoot
User-defined Functions
CS150 Introduction to Computer Science 1
OBJECTIVE QUESTIONS.
Pointers & Functions.
Anatomy of a Function Part 1
Control Structures Repetition
Computer Skills2 for Scientific Colleges
Introduction to Programming - 3
Multidimensional Arrays
Introduction to Programming
Topics discussed in this section:
Dr. Khizar Hayat Associate Prof. of Computer Science
CISC181 Introduction to Computer Science Dr
Multiple Base Classes Inheritance
CS1201: Programming Language 2
CS1201: Programming Language 2
Functions and an Introduction to Recursion
CHAPTER 2 Arrays and Vectors.
Function Defaults C++ permits functions to be declared with default values for some, or all, of its parameters Allows for the function to be called without.
CSC1201: Programming Language 2
CHAPTER 2 Arrays and Vectors.
Introduction to Algorithms and Programming
Control Structures Selection
Functions Imran Rashid CTO at ManiWeber Technologies.
Pointers and dynamic objects
Using string type variables
Pointers & Functions.
Dr. Khizar Hayat Associate Prof. of Computer Science
CS1201: Programming Language 2
Anatomy of a Function Part 1
TOPIC: FUNCTION OVERLOADING
Introduction to Algorithms and Programming
Control Structures contd… Selection
Introduction to Algorithms and Programming COMP151
Introduction to Programming - 1
Introduction to Algorithms and Programming COMP151
Data Structure(s) A way of storing and organizing data in a computer so that it can be used efficiently. e.g. Arrays Linked Lists stacks Queues Trees.
Dr. Khizar Hayat Associate Prof. of Computer Science
Presentation transcript:

Passing Arrays to functions Dr. Khizar Hayat Associate Prof. of Computer Science

#include<iostream> using namespace std; void fun2(int); int c=3; int main() { int a=1; int c=4; c=c/2; cout<<"The value of c is"<<c<<endl; fun2(c); fun2(::c); if(a<10) int c=7; } c++; ::c=::c+3; cout<<"The value of c is"<<::c<<endl; return(0);   void fun2(int x) { int c; c=x*x; cout<<"The value of c is"<<c<<endl; ::c++; cout<<"The value of c is"<<::c<<endl; }  

Array as a parameter to a function The identifier of an array refers to its address. Arrays can only be passed to functions, by reference and not by value In a function declaration, array parameters are specified by data type followed by empty [ ] – the name of array identifier is optional, e.g. void display(int[]); OR void display(int b[]); The function display() takes an array parameter b of type int. In a function call, you would pass just the identifier of the array as argument.

Passing Array to Function: Example #include <iostream> using namespace std; void print(int[],int); int main () { int a[20],n,i; cout<<“Enter the no. of elements of the array“<<endl; cin>>n; for (i=0;i<n;i++) cout<<“Enter the value of ith element”<<endl; cin>>a[i]; } print(a,n); //see next slide for definition return 0;

Passing Array to Function: Example contd… void print(int b[],int m) { int i; for (i=0;i<m;i++) cout<<“The value of b[“<<i<<“] is ”<<b[i]<<endl; }