Functions: simple examples
1a. “Hello world” program: sequential
#include <iostream>
using namespace std;
int main()
{
cout<<"Hello world"<<endl;
return 0;
}
1b. “Hello world” program: using a function ( void function with no arguments
passed to it)
#include <iostream>
using namespace std;
// function prototype.
void PrintHello(); // void function does not return any
values, empty parameter list –
// it does not receive any values
int main()
{
PrintHello(); //function call – we are not passing any values
to the function
return 0;
}
void PrintHello() //function header
{
cout <<"Hello
world!" <<endl; //function
body
return;
}
2a. Print your number: sequential.
#include <iostream>
using namespace std;
int main()
{
float
num;
cout <<
"Enter your favorite number: ";
cin >>
num;
cout<<"\n
You have entered "<<num<<endl;
return
0;
}
2b. Print your number: using a function ( void function with 1 argument
passed to it)
#include <iostream>
using namespace std;
// function prototype
void Print_Your_Number(float); // this function does not return any
values, but expects to
// receive one floating-point value
int main()
{
float num;
cout << "Enter your favorite number: ";
cin >> num;
Print_Your_Number(num); //function call – we are passing
one argument to the function
return 0;
}
//
function definition
void Print_Your_Number( float number) // it’s a good idea to use a different
name in the function
{
cout <<"\nYou
have entered "<<number<<endl;
return;
}
3a. Square your number: sequential
#include <iostream>
using namespace std;
int main()
{
float num, num_squared;
cout << "Enter your number: ";
cin >> num;
num_squared
= num*num;
cout <<"\nYour number squared is "<<num_squared<<endl;
return 0;
}
3b. Square your number: using a function (function that receives 1 argument
and returns a floating-point value)
#include <iostream>
using namespace std;
float Square_Your_Number(float); //this function receives one floating point
value and returns
//one floating point value to the calling function (main)
int main()
{
float num, num_squared;
cout << "Enter your number: ";
cin >> num;
num_squared
= Square_Your_Number(num); // assign value returned by the function
to num_squared.
cout <<"\nYour
number squared is "<<num_squared<<endl;
return 0;
}
float Square_Your_Number( float number)
{
float number_squared; // need to declare the variable for the
result
number_squared = number * number;
return number_squared; //return the result
}
4a. Sum your numbers: sequential
#include <iostream>
using namespace std;
int main()
{
int num1, num2, sum;
cout << "Enter two numbers: ";
cin >> num1 >> num2;
sum = num1 + num2;
cout <<"\nThe sum of your numbers is "<<sum<<endl;
return 0;
}
4b. Sum your numbers: using a function (function that receives 2 integer
arguments and returns an integer value)
#include <iostream>
using namespace std;
int Sum_Two_Numbers (int, int); //function prototype
int main()
{
int num1, num2, sum;
cout << "Enter two numbers: ";
cin >> num1 >> num2;
sum = Sum_Two_Numbers (num1, num2); //function call – passing two arguments
cout <<"\nThe sum of your numbers is "<<sum<<endl;
return 0;
}
int Sum_Two_Numbers (int number1, int number2) //function header
{
int sum_of_numbers;
sum_of_numbers
= number1 + number2; // function
body
return
sum_of_numbers;
}