Different
forms of User-defined Function declarations
The
following program is an example for a function with no return and no arguments
passed .
The
name of the function is display(),
its return data type is void and it does not have any argument.
#include<iostream>
using namespace std;
void display()
{
cout<<"First
C++ Program with Function";
}
int main()
{
display(); //
Function calling statement//
return(0);
}
First C++ Program with Function
The
name of the function is display(),
its return type is int and it does not have any argument. The return statement returns a value to the
calling function and transfers the program control back to the calling
statement.
#include<iostream>
using namespace std;
int display()
{
int a, b, s;
cout<<"Enter
2 numbers: ";
cin>>a>>b;
s=a+b;
return s;
}
int main()
{
int m=display();
cout<<"\nThe
Sum="<<m;
return(0);
}
Enter 2 numbers: 10 30
The Sum=40
The
name of the function is display(),
its return type is void and it has two parameters or arguments x and y to receive two values. The return
statement returns the control back to the calling statement.
#include<iostream>
using namespace std;
void display(int x, int y)
{
int s=x+y;
cout<<"The
Sum of Passed Values: "<<s;
}
int main()
{
int a,b;
cout<<"\nEnter
the First Number :";
cin>>a;
cout<<"\nEnter
the Second Number :";
cin>>b;
display(a,b);
return(0);
}
Enter the First Number :50
Enter the Second Number :45
The Sum of Passed Values: 95
The
name of the function is display(), its return type is int and it has two
parameters or arguments x and y to receive two values. The return
statement returns the control back to the calling statement.
#include<iostream>
using namespace std;
int display(int x, int y)
{
int s=x+y;
return s;
}
int main()
{
int a,b;
cout<<"\nEnter
the First Number :";
cin>>a;
cout<<"\nEnter
the Second Number :";
cin>>b;
int s=display(a,b);
cout<<”\nExample:Function with Return Value and with
Arguments”;
cout<<"\nThe Sum of Passed Values: "<<s;
return(0);
}
Enter the First Number :45
Enter the Second Number :67
Example: Function with Return Value and with Arguments
The Sum of Passed Values: 112
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.