Q3: Write a full program that adds three integers. Your solution should have a function named getSum that accepts three integer parameters and returns the sum of those three integers. Your main function should call the getSum function and assigns the return value to an int variable named result that is declared inside the main function. Your program should then print out the value stored in result. Submit the code and a copy of your output. Q4. Explain why, given your function from Q3, getSum(1,2,3.0) is a syntax error.
#include <iostream>
using namespace std;
int getSum(int a,int b, int c);
int main()
{
int num1;
int num2;
int num3;
int sum;
cout<<"Enter first number: ";
cin>>num1;
cout<<"Enter second number: ";
cin>>num2;
cout<<"Enter third number: ";
cin>>num3;
sum=getSum(num1,num2,num3);
cout<<"Sum is: "<<sum<<endl;
return 0;
}
int getSum(int a,int b,int c)
{
return (a+b+c);
}
Sample output:
Enter first number: 10
Enter second number: 20
Enter third number: 30
Sum is: 60
getSum(1,2,3.0) gives a syntax error because the function getSum() finds the sum of integers only yet 3.0 is not an integer.
Comments
Leave a comment