Sie sind auf Seite 1von 11

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.

#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;
}

EXERCISE 2[edit]
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.

#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;
}

EXERCISE 3
Write a recursive function that finds the #n integer of the Fibonacci sequence. Then build a minimal
program to test it. For reference see

#include <iostream>

using namespace std;

unsigned fib(unsigned n);

int main()
{
// Printing the first 20 Fibonacci sequence values
for (unsigned i = 0; i < 20; i++){
cout << "fib(" << i << ") = " << fib(i) << endl;
}
}
unsigned fib(unsigned n)
{
if (n < 2)
return n;

return fib(n-2) + fib(n-1);


}

EXERCISE 4
Create a calculator that takes a number, a basic math operator (+,-,*,/,^), and a second number all
from user input, and have it print the result of the mathematical operation. The mathematical
operations should be wrapped inside of functions.

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
int x,y,sum;

cout << "Enter first number ";


cin >> x;
cout << endl;

cout << "Enter second number ";


cin >> y;
cout << endl;

cout << 1 << " +" << endl;


cout << 2 << " -" << endl;
cout << 3 << " *" << endl;
cout << 4 << " /" << endl;
cout << 5 << " ^" << endl << endl;
cout << "What math would you like to do? ";
cin >> sum;
cout << endl;

switch (sum){
case 1:
sum = x + y;
cout << "The answer to your addition is " << sum << endl;
break;

case 2:
sum = x - y;
cout << "The answer to your subtraction is " << sum << endl;
break;

case 3:
sum = x * y;
cout << "The answer to your multiplication is " << sum<< endl;
break;

case 4:
sum = x / y;
cout << "The answer to your division is " << sum << endl;
break;

case 5:
sum = pow(x,y);
cout << "The answer to your power function is " << sum << endl;
break;

default:
cout << "You have entered an invalid option " << endl;
break;
}

return 0;
}
C++ program to add two integers. Make a function add() to add integers and
display sum in main() function.

#include <iostream>
using namespace std;

// Function prototype (declaration)


int add(int, int);

int main()
{
int num1, num2, sum;
cout<<"Enters two numbers to add: ";
cin >> num1 >> num2;

// Function call
sum = add(num1, num2);
cout << "Sum = " << sum;
return 0;
}

// Function definition
int add(int a, int b)
{
int add;
add = a + b;

// Return statement
return add;
}
Example: Prime Numbers Between two
Intervals
#include <iostream>
using namespace std;

int checkPrimeNumber(int);

int main()
{
int n1, n2;
bool flag;

cout << "Enter two positive integers: ";


cin >> n1 >> n2;

cout << "Prime numbers between " << n1 << " and " << n2 << " are: ";

for(int i = n1+1; i < n2; ++i)


{
// If i is a prime number, flag will be equal to 1
flag = checkPrimeNumber(i);

if(flag == false)
cout << i << " ";
}
return 0;
}
// user-defined function to check prime number
int checkPrimeNumber(int n)
{
bool flag = true;

for(int j = 2; j <= n/2; ++j)


{
if (n%j == 0)
{
flag = false;
break;
}
}
return flag;
}

Example: Check Prime Number


#include <iostream>
using namespace std;

int checkPrimeNumber(int);

int main()
{
int n;

cout << "Enter a positive integer: ";


cin >> n;
if(checkPrimeNumber(n) == 0)
cout << n << " is a prime number.";
else
cout << n << " is not a prime number.";
return 0;
}
int checkPrimeNumber(int n)
{
bool flag = false;

for(int i = 2; i <= n/2; ++i)


{
if(n%i == 0)
{
flag = true;
break;
}
}
return flag;
}

To call a function, you simply need to pass the required parameters along
with function name, and if function returns a value, then you can store
returned value. For example:

#include <iostream>
using namespace std;
// function declaration
int max(int num1, int num2);

int main () {
// local variable declaration:
int a = 100;
int b = 200;
int ret;

// calling a function to get max value.


ret = max(a, b);

cout << "Max value is : " << ret << endl;

return 0;
}

// function returning the max between two numbers


int max(int num1, int num2) {
// local variable declaration
int result;

if (num1 > num2)


result = num1;
else
result = num2;

return result;
}
Default Values for Parameters
When you define a function, you can specify a default value for each of the
last parameters. This value will be used if the corresponding argument is
left blank when calling to the function.

This is done by using the assignment operator and assigning values for the
arguments in the function definition. If a value for that parameter is not
passed when the function is called, the default given value is used, but if a
value is specified, this default value is ignored and the passed value is used
instead. Consider the following example:

#include <iostream>
using namespace std;

int sum(int a, int b=20) {


int result;

result = a + b;

return (result);
}

int main () {
// local variable declaration:
int a = 100;
int b = 200;
int result;

// calling a function to add the values.


result = sum(a, b);
cout << "Total value is :" << result << endl;

// calling a function again as follows.


result = sum(a);
cout << "Total value is :" << result << endl;

return 0;
}

What will be the output of the following program?


#include<iostream.h>
void MyFunction(int a, int b = 40)
{
cout<< "a="<< a << "b=" << b << endl;
}
int main()
{
MyFunction(20, 30);
return 0;

a=20 b=30

14) What is the output of the following code?


void func(int x)
{
x = x * 2;
}
int main( )
{
int x = 10;
func(x);
cout << x << endl;
}

a) 20
b) 30
c) 10
d) 5

Das könnte Ihnen auch gefallen