Write Syntax for each of the following in C++ environment: i. Function Declaration ii. Function Definition iii. Function Overloading
#include <iostream>
using namespace std;
int fun(int a, int b); //function declaration
int fun(int a, int b, int c); //overloaded function declaration
//function definition
int fun(int a, int b){
cout<<"This function accepts two arguments and returns their sum."<<endl;
cout<<"The arguments are: "<<a<<" "<<b<<endl;
return (a+b);
}
//overloaded function definition
int fun(int a, int b, int c){
cout<<"This function accepts three arguments and returns their sum."<<endl;
cout<<"The arguments are: "<<a<<" "<<b<<" "<<c<<endl;
return (a+b+c);
}
int main(){
//this is main function
int a, b, c, s;
cout<<"Enter value of a, b, c in order: ";
cin>>a>>b>>c;
cout<<"Calling function with two arguments."<<endl;
s = fun(a, b);
cout<<"Sum of a, b: "<<s<<"\n";
cout<<"Calling function with three arguments."<<endl;
s = fun(a, b, c);
cout<<"Sum of a, b, c: "<<s<<"\n";
return 0;
}
Comments
Leave a comment