Sign in to follow this  

Undeclared identifier in a function

This topic is 3299 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi Guys, I am pretty new at programming and I have been trying to write a program with functions to be used for currency exchange. When I wrote it without the functions it worked fine but I am finding it a bit hard to implement functions correctly. Below is the script:
Quote:
/*This program will convert Currencies*/ #include <stdio.h> float exchangerate(float rate) { return r; } float euroconversion(float P) { return r*P; } float poundconversion(float E) { return (E/r); } int main(void) { float r,P,E,X1,X2; int menu; printf("Currency Conversion Program\n"); printf("-------- ---------- -------\n"); printf(" Enter the euro exchange rate for 1 pound:\n"); scanf("%f",&r); printf("Menu\n"); printf("1= Convert Pounds to Euros\n"); printf("2= Convert Euros to Pounds\n"); printf("3= Exit\n"); printf("Choose any of the above choices\n");/*Asks the user to choose one of the menu options*/ scanf("%d", &menu); switch (menu) { case 1: { printf("Choice: 1\n"); printf("Enter your currency in pounds:\n"); scanf("%f",&P); X1= euroconversion(r); /*Formula converts pounds to Euros*/ printf("%.2f pounds = %.2f euros today\n",P,X1); break; } case 2: { printf("Choice: 2\n"); printf("Enter your currency in euros:\n"); scanf("%f",&E); X2= poundconversion(r); /*Formula converts Euros to Pounds*/ printf("%.2f euros = %.2f pounds today\n",E,X2); break; } case 3: { printf("You have chosen to Exit\n"); break ;/*The Program Ends!*/ } } return 0; }
This is what is displayed when I try to compile it:
Quote:
1>------ Build started: Project: Lab 5 part A, Configuration: Debug Win32 ------ 1>Compiling... 1>CURRENCYEXCHANGE.c 1>c:\users\rodney\documents\visual studio 2005\projects\lab 5 part a\lab 5 part a\currencyexchange.c(8) : error C2065: 'r' : undeclared identifier 1>c:\users\rodney\documents\visual studio 2005\projects\lab 5 part a\lab 5 part a\currencyexchange.c(8) : warning C4244: 'return' : conversion from 'int' to 'float', possible loss of data 1>Build log was saved at "file://c:\Users\Rodney\Documents\Visual Studio 2005\Projects\Lab 5 part A\Lab 5 part A\Debug\BuildLog.htm" 1>Lab 5 part A - 1 error(s), 1 warning(s) ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Thanks in advance for your help.

Share this post


Link to post
Share on other sites
The r variable is declared in the main function, and this means that only the code there is able to 'see' it (use it). Either make it global or pass it as a parameter to the function.

Share this post


Link to post
Share on other sites
Here are a few examples to show you how functions work. Please study them very carefully.


int twice(int x) {
// You CANNOT use 'y' or 'z' here, because they do not belong to this
// function, but instead to the main function.
return x * 2;
}

int main() {
int y, z;
y = 10;
z = twice(y);
// the variable 'y' gets copied into the function parameter 'x',
// twice() does its work, and the returned value is assigned to 'z'.
// It does not matter that the name 'y' in main() does not match the function
// parameter's name. There is no correspondence there. You can pass anything
// you like (as long as the type is correct).

// At this point, y == 10 and z == 20.
y = twice(15);
// Now y == 30. Can you see why?

// Bonus question. Why didn't I call the function "double"? :)
}


You need to communicate to a function all the information that is needed to perform its task. In this case, the exchange rate is part of that; and the work done by the functions is so simple that nothing is really gained by using them (except for practice, of course).

Also, did you not notice the heading "For Beginners" at the top of the forum list? We have a whole separate forum for new programmers so they don't interrupt the discussion of more serious difficulties (and can be treated more nicely while we're at it).

Share this post


Link to post
Share on other sites

This topic is 3299 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this