Functions BICSE-6A Mr. Naeem Khalid Lecturer, Dept. of Computing.

Slides:



Advertisements
Similar presentations
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.
Advertisements

This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
Multiple-Subscripted Array
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3.
Chapter 7 Arrays C++ Programming, Namiq Sultan1 Namiq Sultan University of Duhok Department of Electrical and Computer Engineering Reference: Starting.
Overview of Previous Lesson(s) Over View  C++  KeywordsReserved words  IdentifiersProgrammers defined variables  Variables A named storage location.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
C++ Arrays. Agenda What is an array? What is an array? Declaring C++ arrays Declaring C++ arrays Initializing one-dimensional arrays Initializing one-dimensional.
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
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.
Object Oriented Programming Spring COMSATS Institute of Information Technology Functions OOP in C++ by Robert Lafore - Chapter#5 Kaleem Ullah
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 11 Structured Data.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 3 More About Classes Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
C++ Functions. Objectives 1. Be able to implement C++ functions 2. Be able to share data among functions 2.
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 Lecture 04 Structural Programming in C++ You will learn: i) Operators: relational and logical ii) Conditional statements iii) Repetitive statements.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
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.
Copyright © 2012 Pearson Education, Inc. Chapter 11: Structured Data.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
1 Lecture 14 Functions Functions with Empty Parameter Lists Empty parameter lists  void or leave parameter list empty  Indicates function takes.
Introduction to C Programming Lecture 6. Functions – Call by value – Call by reference Arrays Today's Lecture Includes.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
#include using namespace std; // Declare a function. void check(int, double, double); int main() { check(1, 2.3, 4.56); check(7, 8.9, 10.11); } void check(int.
1 Arrays and Strings Lecture: Design Problem l Consider a program to calculate class average Why?? ?
Function Overloading Two different functions may have the same name as long as they differ in the number or types of arguments: int max(int x, int y) and.
Copyright © 2002 W. A. Tucker1 Chapter 9 Lecture Notes Bill Tucker Austin Community College COSC 1315.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Lecture 5 functions 1 © by Pearson Education, Inc. All Rights Reserved.
1 Chapter 12 Arrays. 2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating.
1 MORE ON MODULAR DESIGN: MODULE COMMUNICATIONS. 2 WHEN A FUNCTION IS INVOKED, MEMORY IS ALLOCATED LOCALLY FOR THE FORMAL PARAMETERS AND THE VALUE OF.
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
Review Pointer Pointer Variables Dynamic Memory Allocation Functions.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.15Functions with Empty Parameter Lists 3.16Inline Functions 3.17References.
Lecture 14: Arrays Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Arrays.
Module 1: Array ITEI222 - Advance Programming Language.
Chapter 05 (Part II) Control Statements: Part II.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays 4.5Passing.
Programming Languages -2 C++ Lecture 3 Method Passing Function Recursion Function Overloading Global and Local variables.
ADVANCED POINTERS. Overview Review on pointers and arrays Common troubles with pointers Multidimensional arrays Pointers as function arguments Functions.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014.
Functions Modules in C++ are called functions and classes. Main reason to use functions is : – get aid in conceptual organization.
Prepared by Andrew Jung. Contents A Simple program – C++ C++ Standard Library & Header files Inline Functions References and Reference Parameters Empty.
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.
Chapter Structured Data 11. Combining Data into Structures 11.2.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Arrays Arrays exist in almost every computer language.
Functions and an Introduction to Recursion
Chapter 5 Functions.
DATA HANDLING.
FUNCTIONS& FUNCTIONS OVERLOADING
Functions and an Introduction to Recursion
Functions Imran Rashid CTO at ManiWeber Technologies.
Pointers and dynamic objects
Functions Chapter No. 5.
Presentation transcript:

Functions BICSE-6A Mr. Naeem Khalid Lecturer, Dept. of Computing

Agenda Passing arrays to functions Overloaded functions Inline functions Default arguments

Arrays c[6] Name of array (Note that all elements of this array have the same name, c) c[0] c[1] c[2] c[3] c[11] c[10] c[9] c[8] c[7] c[5] c[4] Position number of the element within array c The values of each element are stored in consecutive memory locations

Declaring Arrays Declaring arrays –Type of elements –Name of array –Number of elements –Examples int c[ 10 ]; float hi[ 3284 ]; Declaring multiple arrays of same type –Similar format as other variables –Example int b[ 100 ], x[ 27 ];

Array Example // replay.cpp // gets four ages from user, displays them #include using namespace std; int main() { int age[4];//array ‘age’ of 4 ints for(int j=0; j<4; j++)//get 4 ages { cout << "Enter an age: "; cin >> age[j];//access array element } for( j=0; j<4; j++)//display 4 ages cout << "You entered " << age[j] << endl; return 0; }

Averaging Array Elements // sales.cpp // averages a weeks’s widget sales (6 days) #include using namespace std; int main() { const int SIZE = 6;//size of array double sales[SIZE];//array of 6 variables cout << "Enter widget sales for 6 days\n"; for (int j=0; j<SIZE; j++)//put figures in array cin >> sales[j]; double total =0; for(j=0; j<SIZE; j++)//read figures from array total += sales[j];//to find total double average = total / SIZE;// find average cout << "Average = " << average << endl; return 0; }

Passing Arrays to Function // salefunc.cpp // passes array as argument #include #include // for set precision, etc. using namespace std; const int DISTRICTS = 4;// array dimensions const int MONTHS = 3; void display( double[DISTRICTS][MONTHS] );//declaration int main() {// initialize two - dimensional array double sales[DISTRICTS][MONTHS] = {{ ,234.50, }, { , , }, { ,934.00, }, { , , } }; display (sales);// call function; array sa argument cout << endl; return 0; }//end main Cont…..

Passing Arrays to Function // //display() //function to display 2 - d array passed as argument void display ( double funsales[DISTRICTS][MONTHS] ) { int d, m; cout << "\n\n"; cout << "Month\n"; cout << " 12 3"; for (d=0; d<DISTRICTS; d++) { cout <<"\nDistrict " <<d+1; for( m=0; m<MONTHS; m++) cout << setiosflags(ios :: fixed) << setw(10) << setiosflags(ios:: showpoint) << setprecision(2) << funsales[d][m];// array element }//end for(d) }//end display

Function Declaration with Array Arguments Array arguments are represented by the data type and size of the array void display (float[DISTRICTS][MONTHS]); // declaration Function does not need the size of the first dimension void display (float[ ][MONTHS]); // declaration Declaring a function that used a one- dimensional array as an argument, we would not need to use the array size: void somefunc(int elem[ ] ); // declaration

Function Call with Array Argument When the function is called, only the name of the array is used as an argument display(sales);// function call Sales in this case actually represents the memory address of the array Using an address for an array argument is similar to using a reference argument The function works with the original array, although it refers to it by a different name. Duplicating an entire array in every function that called it would be both time-consuming and wasteful of memory. An address is not the same as reference. No ampersand (&) is used with the array name in the function declaration. Array are passed using their name alone, and that the function accesses the original array, not a duplicate.

Function Definition with Array Argument In the function definition the declaration looks like this: void display( double funsales[DISTRICTS][MONTH] ) The function definition uses funsales as array name but it refers to sales in main In the function definition elements are accessed by funsales[d][m]

Function Overloading A C++ programmer may use the same name for more than one function. This would typically be done for different but related functions that perform similar tasks. Function overloading defines a new meaning for a function name that is already in use.

Function Overloading Contd.. These two functions are different, even though they have the same name int sqrt (int x) { …. } double sqrt (double x) { …. }

Function Overloading Contd.. In C++, when the compiler sees a function call, it selects one from its list of known functions by examining both the called function name and the arguments provided in the call. When an overloaded function is called the compiler selects the proper function by examining the number, types and order of the arguments in the call.

Function Overloading Contd.. Function Signature –The combination of a function's name and its parameter list is called its signature. Every function must have a unique signature –Overloaded functions (functions with same name) can have different return types but must have different parameter list.

Examples : Function Overloading // some function prototypes void fun (void); int fun (int);// valid overloading double fun (int, int) // valid overloading double fun (int);// illegal redefinition int fun (void);// illegal redefinition

Function Overloading Example // overload.cpp // demonstrates function overloading #include using namespace std; void repchar();//declarations void repchar(char); void repchar(char, int); int main() { repchar(); repchar( '=' ); repchar( '+', 30); return 0; }

Function Overloading Example // // repchar() // displays 45 astericks void repchar() { for(int j=0; j<45; j++)//always loops 45 times cout << '*' ; //always prints astericks cout << endl; } // // repchar() // displays 45 copies of specified character void repchar (char ch) { for (int j=0; j<45; j++)// always loops 45 times cout << ch;// prints specified character cout << endl; } // // repchar() // displays specified number of copies of specified character void repchar(char ch, int n) { for(int j=0; j<n; j++)// loops n times cout << ch;//prints specified character cout << endl; }

Inline Functions To save execution time in short functions At a function call in the source file the actual code is inserted, instead of jump to the function

INLINE FUNCTION EXAMPLE //inline.cpp //demonstrate inline function #include using namespace std; //lbstokg() //converts pound to kilograms inline float lbstokg(float pounds) { return *pounds; } // int main() { float lbs; cout<<"\nEnter your weight in pounds: "; cin>>lbs; cout<<"Your weight in kilogram is " <<lbstokg(lbs)<<endl; return 0; }

Default Argument A function can be called without specifying all its argument Default argument is useful if you don’t want to go to the trouble of writing arguments i.e. almost always have the same value Using default arguments means that the existing function calls can continue to use the old number of arguments while new functions calls can use more

Default Argument Example //missarg.cpp //demonstrate missing and default argument #include using namespace std; void repchar(char='*',int=45); int main() { repchar(); repchar('='); repchar('+',30); return 0; } // //repcahr() //display line of characters void repchar(char ch, int n) { for (int j=0;j<n ; j++) cout <<ch; cout <<endl; }

THE END