Download presentation
Presentation is loading. Please wait.
1
FUNCTIONS In C++
2
Introduction Functions are the main tool of Structured Programming.
Functions decrease the size of program by calling the function at different places in the program. See the Syntax in C: void show (); main() { show (); } void show () ………………
3
Basically Functions in C and C++ have the same importance and the applications.
Also in C++ functions and the operators can be Overloaded. MAIN Function: No return type for main() in C. In C++ main() always returns an integer value to the operating system. That is why we always have a return() statement at the end of the main(). Operating system checks the return value as the function has been successfully executed or not.
4
Function Prototyping Function Prototype is a kind of template giving details such as number and type of arguments and type of return values It is used by compiler to ensure that proper arguments are passed and return value is treated correctly. Earlier C did not has the prototyping, It was firstly introduced in C++ then it was adopted in ANSI C. However it is optional in C but compulsory in C++. returntype function name (argument list) The names of the arguments are optional in declaration but must in the function definition. Eg:- float volume(float x, float y, float z);;
5
Call by Reference When the values are passed to the functions they are using the copies of the original variables. It will create a problem when we have to change the original variables. But in case of Call be Reference we pass the addresses of the variables hence all the operations are performed on the original variables. Example: Swapping void swap(int a, int b) { int t; t=a; a=b; b=t; } swap(m,n); void swap(int *a, int *b) { int t=*a; *a=*b; *b=t; Swap(&x, &y);
6
Return by Reference int & max(int a, int b) { if (x>y) return x; else return y; } This function will return the address of either x or y.
7
Inline Functions When a function is called substantial time is wasted the shifting of the control. It adds more overheads when the size of the function is very small. One alternative to it is Macros, but their errors are not checked during the compilation. In C++ we have inline functions. Here the compiler replaces the function call with the corresponding function code. inline function-header { function body } inline float cube(float a) { return(a*a*a); } Calling: c=cube(3.0); d=cube( );
8
Inline Functions contd..
The Inline functions are advantageous only when the size of the function is too small, if we use the same technique for the bigger functions the benefits of the Inline functions will be lost. In case of Inline function the control doesn't go any where. The Inline Keyword is just a request to the compiler not the command, the compiler may ignore the request if the size of the function is too large, Then it will be treated as normal functions. The Inline will not work in the following cases; Functions having the loop, switch or goto statement. Functions not returning any values if a return statement exists. If functions have static variables. If functions are recursive. Using the Inline functions every time the function is invoked new memory is allocated so trade-off becomes necessary.
9
Inline Function Output 1000 157.26 112500 #include<iostream.h> inline float mul(float x ,float y) { return(x*y); } int main() float a=12.345; float b=9.842; cout<<mul(a,b)<<“\n”; return 0;
10
Default Arguments We can call a function in C++, without specifying all the arguments. We can give some default values in the function prototype. float amount(float p, int t, float rate =0.15); Now if we call value = amount(5000,7); Then internally It will be converted into value = amount(5000,7,0.15); Important here is that the default values must be added from right to left. Egs:- int mul(int i,int j=5,int k=10); int mul(int i=5, int j); int mul(int i=0,int j=5,int k=10);
11
Contd……. Default Argument is checked for type at the time of declaration and evaluated at the time of call Cannot provide value to a particular argument in the middle of an argument list Advantages:- To add new parameters to existing functions To combine similar functions into one
12
Const Arguments This tells the compiler that the function should not modify the argument. int strlen (const char *p); int length (const string &s); Specially it is used when we pass arguments as reference of as pointers.
13
Overloading; Using the same thing for different purposes.
Function Overloading Overloading; Using the same thing for different purposes. When a same function name performs a variety of tasks is also called function polymorphism in OOP. The operation to be performed by the function will depend upon the type and the no. of arguments passed to the function. It is done like this : The compiler will try to find the exact match, actual type and no. of arguments. If not the compiler uses integral promotions char to int float to double 3 Then built in conversions are done, and if there is no match then compiler will generate an error message. Precautions: Unrelated functions should not be overloaded. Default arguments can be used sometimes at place of overloading.
14
Contd….. Overloaded functions must differ in argument list in either following ways:- Differ in number of arguments Differ in data types of arguments Differ in both
15
Function Overloading int volume(int s) { return(s*s*s); } int volume(double r, int h) { return( *r*r*h); int volume(long I, int b, int h) { return(l*b*h); Output 1000 157.26 112500 #include<iostream.h> int volume (int); double volume (double, int); long volume (long int, int); int main() { cout<<Volume(10)<<“\n”; cout<<Volume(2.5,8)<<“\n”; cout<<Volume(100L,75,15)<<“\n”; return 0; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.