C++ Function
C++ Function
▪ Function are used for divide a large code into module, due
to this we can easily debug and maintain the code.
▪ E.g. if we write a calculator programs at that time we can
write every logic in a separate function [For addition sum(),
for subtraction sub()].
▪ Any function can be called many times.
▪ Code Re-usability
#include<iostream.h> Sum: 33
void main()
{
sum(); // calling the function
}
void main(){
int a=111, b=222;
swap(a, b); // passing value to function
cout<<"Value of a: "<< a <<endl;
cout<<"Value of b: "<< b <<endl;
}
Computer Programming 3/20/2021 13
Call by Reference
#include<iostream.h>
void main(){
int a=111, b=222;
swap(&a, &b); // passing value to function
cout<<"Value of a: "<< a <<endl;
cout<<"Value of b: "<< b <<endl;
}
Computer Programming 3/20/2021 14
▪ Arguments passed by value and by reference.
For example:
#include<iostream.h>
void main()
{
show(); // Call it like a normal function
}