Last modified on 23 May 2014, at 05:45

C++ Programming/Exercises/Functions/Pages

EXERCISE 1Edit

Write a program with a function that takes two int parameters, adds them together, then returns the sum. The program should ask the user for two numbers, then call the function with the numbers as arguments, and tell the user the sum.

Solution

Solution #1

#include <iostream>
using namespace std;
 
int AddTwo (int addend1, int addend2)  {
	return addend1 + addend2;
}
 
int main ()  {
	int number1, number2, sum;
 
	cout << "Enter two integers:\n";
	cin >> number1 >> number2;
	sum = AddTwo(number1, number2);
	cout << "\nThe sum is " << sum << ".";
 
 
}

Solution #2

//by blazzer12
//Input two values. Call a function that returns the sum of the values.
 
#include<iostream>
 
using namespace std;
 
int getSum(int, int);
 
int main()
{
	int number1, number2, sum;
 
	//get values
	cout<<"Enter two integers to add"<<endl;
 
	cout<<"Number1 :";
	cin>>number1;
	cout<<"Number2 :";
	cin>>number2;
 
	//call getSum() and store result in sum
	sum = getSum(number1, number2);
 
	//print result
	cout<<number1<<" + "<<number2<<" = "<<sum;
}
 
int getSum(int addend1, int addend2)
{
	return addend1 + addend2;
}

The solution in C.

#include <stdio.h>
 
int add(int, int);
 
int main()
{
        int a, b, sum;
 
        printf("A: ");
        scanf("%d", &a);
 
        printf("B: ");
        scanf("%d", &b);
 
        sum = add(a, b);
 
        printf("The sum of %d and %d is %d.\n", a, b, sum);
 
        return 0;
}
 
int add(int a, int b)
{
        return a + b;
}


// Another solution:
# include <iostream>
 
using namespace std;
 
int sum (int number1, int number2);
 
int number1;
int number2;
 
int main()
{
 
    cout<<"Give me a number amigo: ";
    cin>>number1;
 
    cout<<"Give me another number dude: ";
    cin>>number2;
 
    cout<<"The sum of "<<number1<<" and "<<number2<<" is: "<<sum(number1,number2)<<"."<<endl;
 
    return 0;
 
 
}
 
int sum (int number1, int number2)
{
    return number1+number2;
}
 
// by neuroalchemist


EXERCISE 2Edit

Basically the same as exercise 1, but this time, the function that adds the numbers should be void, and takes a third, pass by reference parameter; then puts the sum in that.

Solution

Solution #1

#include <iostream>
using namespace std;
 
void AddTwo (int addend1, int addend2, int &sum)  {
	sum = addend1 + addend2;
}
 
int main ()  {
	int number1, number2, sum;
 
	cout << "Enter two integers:\n";
	cin >> number1 >> number2;
	AddTwo(number1, number2, sum);
	cout << "\nThe sum is " << sum << ".";
 
	return 0;
}

Solution #2

//by blazzer12
//adds two integers using a "pass by reference" type function call.
 
#include <iostream>
using namespace std;
 
int addNum(int, int, int&);
 
int main()
{
	int number1,number2,sum;
 
	//get values;
	cout<<"Enter two integers to add"<<endl;
 
	cout<<"Enter Number 1: ";
	cin>>number1;
	cout<<"Enter Number 2: ";
	cin>>number2;
 
	//call addNum to add the numbers
	addNum(number1, number2, sum);
 
	//print sum
	cout<<number1<<" + "<<number2<<" = "<<sum;
 return 0;
}
int addNum(int addend1, int addend2, int &sum)
{
	sum = addend1 + addend2;
}