Functions in C++
Functions in C++
20/01/11
Outlines
Function Prototyping Calling the Functions Inline functions
Contd..
The definition of main function is : return type main() { statements // body of main() function return 0 ; // return statement for termination } When return statement is not included in the program, the C++ compiler display the warning: Function should return a value
Object
Function
Member function
Function Prototyping
Used to expand the function declaration to provide details to the compiler about the number and data type of arguments used by the function. The declaration statement for a function prototype is : type function_name(argument list) ;
The function prototype enables you to use the function before it is actually defined. Write a program to calculate the simple interest
Example
#include<iostream.h> // function prototyping int simple_interest(int amount, int time, int rate); int main() { int x; //calling the simple_interest function and storing //the result in the x variable x = simple_interest(1000,2,2); cout << the simple interest is :<<x ;
Contd..
return 0; }//end main() //function definition int simple_interest(int amount, int time, int rate) { int simple; simple = (amount*time*rate)/100 ; return simple ; }
Example
Write a program to interchange the values of two variables #include<iostream.h> Void swap(int x, int y); int main() { int a = 5; int b = 10; swap(a , b);
Contd..
cout<<\n a = <<a<<endl <<b = <<b ; return 0; } void swap(int x, int y) { int temp ; temp = x; x = y; y = temp ; }
23
45
Contd..
It shows that a call to the function, swap, is made to interchange of values of variables a and b, which is done only on the copies of these variables. The output of the program is : a=5 b = 10 Value is same as at the time of initialization
Call By Reference
The call by reference mechanism is used to change the values of the variables in the calling function, which calls the particular function. It copies the address of the actual arguments into formal arguments. This means when the called function is manipulating the values of its arguments, then it actually manipulating the actual arguments.
Example
#include<iostream.h> Void swap(int *, int *); int main() { int a = 5; int b = 10; swap(&a , &b);
Contd
cout<<\n a = <<a<<endl <<b = <<b ; return 0; } void swap(int x, int y) { int temp ; temp = *x; *x = *y; *y = temp ; }
Example
Program to calculate the percentage obtained by the student using array as function arguments
#include<iostream.h> //prototype float average_marks(int array[], int size); int main() { int marks[5] ;//declaring the array //assigning values to array elements cout << Enter the marks:<<endl ; // assigning values to array elements for(int i=0; i<5; i++) { cin >>marks[i]; }
Contd..
float average = average_marks(marks, 5); cout <<The percentage is:<<average ; return 0; } //Definition of the function average_marks float average_marks(int array[], int size) { int total = 0; for (int i = 0; i<size; i++) {
Contd..
total = total + array[i] ; } float percentage1 = (( total*100)/500); return percentage1 ; }
Inline Functions
The Inline keyword is used to declare an inline function. The use of inline function allows the compiler to substitute the called function body in the code itself, rather than transfering the control to the called function.