mtber75
mtber75

Reputation: 978

C++ Syntax error when declaring function inside function

I am trying to create a program that allows me to find the nth root of a number. I tried running this on xcode and, I get an error that prevents me from running it. I'm getting an error for this line:

double f(double x) {

In this line, I am trying to declare a function, but it seems I'm declaring it incorrectly. xcode says expected ;. How can I address this issue?

#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>
#include <cctype>
#include <stdio.h>
#include <ctype.h>
#include "helloworld.h"
#include <locale>

using namespace std;
double newton( int n, int number);
string lowercase (string word);

int main()
{
    int n;
    int number;
    cout << "This program can find the nth root of a number without actually solving the problem" <<endl;
    cout << "Tell me what the value of n is: ";
    cin >> n;
    cout << "What is the number that you want to get rooted";
    cin >> number;

    newton(n, number); 
}

double newton( int n, int number) {
    const double epsilon = .0001;
    double x0;
    double x1 = number;

    double f(double x) {
        return (double) pow(x, n) - number;
    }
    double der_f(double x) {
        return (double) n*pow(x, n-1);
    }

    while( abs(x1-x0) < epsilon) {
        x0 = x1;
        x1 = x0 -f(x0)/der_f(x0); 
    }
    return x1;
}

Upvotes: 1

Views: 890

Answers (4)

kassak
kassak

Reputation: 4184

If you really want function inside function - there is a hack. You could define struct with static function inside your function.

Example:

double newton( int n, int number) {
    const double epsilon = .0001;
    double x0;
    double x1 = number;

    struct wrap {
       static int n;
       static double f(double x) {
           return (double) pow(x, n) - number;
       }
       static double der_f(double x) {
           return (double) n*pow(x, n-1);
       }
    };
    wrap::n = n;

    while( abs(x1-x0) < epsilon) {
        x0 = x1;
        x1 = x0 -wrap::f(x0)/wrap::der_f(x0); 
    }
    return x1;
}

Something like this.

Upvotes: 2

Mikhail
Mikhail

Reputation: 21749

As it is already pointed out, you have to get rid of local functions. Another way of doing it in C++11 is to use lambda-functions:

auto f = [=](double x) {
  return (double) pow(x, n) - number;
};
auto der_f = [=](double x) {
  return (double) n*pow(x, n-1);
};

All you need in this case is to replace double func_name(double x) with auto func_name = [=](double x).

The [=] is a lambda-introducer. By = you're telling that you want all of your local variables to be accessible in a lambda-function by value. This is the most appropriate way in your case since you only have variables of basic types and you don't want to modify them from your local functions.

The auto keyword tells a compiler to automatically deduce type of a variable for storing a function from the initialization clause. One can std::function<double(double)> instead of auto here.

Upvotes: 0

L0j1k
L0j1k

Reputation: 12635

This is happening because you're declaring new functions inside of a function, which is not possible. Try this:

#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>
#include <cctype>
#include <stdio.h>
#include <ctype.h>
#include "helloworld.h"
#include <locale>

using namespace std;
double newton( int n, int number);
string lowercase (string word);


double f(double x) {
    return (double) pow(x, n) - number;
}

double der_f(double x) {
    return (double) n*pow(x, n-1);
}

int main() {
  int n;
  int number;
  cout << "This program can find the nth root of a number without actually solving the problem" <<endl;
  cout << "Tell me what the value of n is: ";
  cin >> n;
  cout << "What is the number that you want to get rooted";
  cin >> number;
  newton(n, number);
}

double newton( int n, int number) {
  const double epsilon = .0001;
  double x0;
  double x1 = number;
  // and here call the functions
  f();
  der_f();
  while( abs(x1-x0) < epsilon ) {
    x0 = x1;
    x1 = x0 -f(x0)/der_f(x0); 
  }
  return x1;
}

Upvotes: 1

Preet Sangha
Preet Sangha

Reputation: 65496

Move

   double f(double x) {
        return (double) pow(x, n) - number;
    }
    double der_f(double x) {
        return (double) n*pow(x, n-1);
    }

to outside of double newton( int n, int number) {

Upvotes: 1

Related Questions