Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Functions BICSE-6A Mr. Naeem Khalid Lecturer, Dept. of Computing."— Presentation transcript:

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

2 Agenda Passing arrays to functions Overloaded functions Inline functions Default arguments

3 Arrays c[6] -45 6 0 72 1543 -89 0 62 -3 1 6453 78 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

4 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 ];

5 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; }

6 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; }

7 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] = {{ 1432.07,234.50, 654.01 }, { 322.00, 13838.32,17589.88 }, { 9328.34,934.00,4492.30 }, { 12838.29,2332.63, 32.93 } }; display (sales);// call function; array sa argument cout << endl; return 0; }//end main Cont…..

8 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

9 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

10 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.

11 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]

12 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.

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

14 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.

15 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.

16 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

17 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; }

18 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; }

19 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

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

21 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

22 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; }

23 THE END


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

Similar presentations


Ads by Google