/*
  Defining functions. see Nyhoff P155-159

  Code with loops or structures that are much larger than what can be shown on the screen are almost always wrong.
  We can use functions to replace code by function calls, that makes code much faster and easier to read and
  hence avoid making bugs.
  
  Functions look like:

  <return type>  name( <arguments> )
   {
      <statements>

      return <result>;
   }


  Some functions does not return any result, they are void

  void name( <arguments> )
   {
      <statements>
   }

  Function can only return a single value. We will return to how to return multiple values later.  
  
  Hint always document that you aim for the function to do.

  Exercise:
    1) Write a function numberrealroots()  that return the number of real roots of a 2nd polynomium
*/


#include <iostream>
#include <cmath>
using namespace std;

// Function that does not return anything
void printnumber(int n)
{
   cout << "The number is " << n << endl;
}


// Function that takes an integer argument and returns the value * 42
int times42(int n)     // Scope of variables:
{                      // n    
   int m=n*42;         // :     m
   return m;           // :     :
}

// Function to evaluate 1^2+2^2+3^2 + .. + n^2
int sumNsquare(int n)
{   
   int sum=0;
   for (int i=1; i<=n; i++) sum+=i*i;   // i is only defined in this statement
   return sum;
}

// Function that calculates discriminant of polynomium a x^2 + b x + c
double discriminant(double a,double b,double c)
{   
   return b*b-4.0*a*c;
}

// Boolean function
bool has2roots(double a,double b,double c)
{
  return discriminant(a,b,c)>0.0;
}



int main()
{
   printnumber(4);

   int n=3;
   printnumber(n);
   cout << times42(n) << endl;

   cout << sumNsquare(10) << endl;

   double a=5.5, b=1.0, c=2.0;
   cout << discriminant(a,b,c) << endl;
   cout << has2roots(a,b,c) << endl;
   
   return 0;
}
