0% found this document useful (0 votes)
111 views23 pages

Functions in C++

Functions in C++ can be called in different ways. Main functions provide the starting point for program execution and typically return integer values. Functions can be called by passing values directly or by reference to change the original variables. Inline functions allow the compiler to substitute function code directly into the calling code for improved efficiency.

Uploaded by

Abhishek Modi
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
111 views23 pages

Functions in C++

Functions in C++ can be called in different ways. Main functions provide the starting point for program execution and typically return integer values. Functions can be called by passing values directly or by reference to change the original variables. Inline functions allow the compiler to substitute function code directly into the calling code for improved efficiency.

Uploaded by

Abhishek Modi
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 23

Functions in C++

20/01/11

Outlines
Function Prototyping Calling the Functions Inline functions

The main Function


Function from where execution starts Return type of main() function is int by default

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

Calls made by main()


Main () function
Call to standalone function Call to member function

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

Calling the Function


Call By Value Call By Reference

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.

In detail we learn in next class

You might also like