Hi
I have i want to call a function with two results for example x = 1 and y = 2.How do i return this function in c and how do i call such a function in the main program.
Hi
I have i want to call a function with two results for example x = 1 and y = 2.How do i return this function in c and how do i call such a function in the main program.
I don't understand your question at all, but let me explain how to call a function in C if you want to return a int, for example.
So, you will see in the screen:Code:void main(){ int x; int y; x = returnX(); //The function returnX return a number that is assigned to the variable X. y = returnY(); //Exactly the same than the last function, but now with the variable Y. printf("x =%d y = %d", x,y) } int returnX(){ int aux; aux = 5; return aux; //The function returns the number contained in the variable AUX. (In that case, 5). } int returnY(){ //Then, do the same with the function returnY int aux; aux = 9; return aux; }
x = 5 y = 9
Last edited by catasturslykid; 08-17-2013 at 07:11 AM.
void returnX();
void returnY();
And both of these functions return an int? I want some of what you're smoking.
Haha, what a fail! Sorry! Corrected!
It would help if you explained it correctly. There are several flaws in your example program.but let me explain how to call a function in C if you want to return a int,
First main() should be defined to return in int, not void, and you should return an int from this function.
Second you can't return anything from a function you declared would be return nothing (void), you should be declaring your function to return an int.Code:int main(void) { return 0; }
You must also know that your program won't compile, cleanly, because main() doesn't know anything about your functions because they are declared after you are already trying to use them, provide the proper function prototypes.Code:int returnX(void) { int aux; aux = 5; return aux; //The function returns the number contained in the variable AUX. (In that case, 5). }
Lastly you need to find an indentation style and use it consistently.
Now to answer the OP's question. You can't return two values from a function using return. However you can pass the variables into the function so they can be modified.
JimCode:void modifyTwoValues( int *value1, int *value2); ... void modifyTwoValues( int *value1, int *value2) { *value1 = 5; *value2 = 78; }
Thanks for response.
My question is related to the question that i am attempting.i want to convert a polar vector to a rectangular vector.I have the main function and i have the function to convert the vector.Please see the the program below and advise how i can make it work. Thanks.
Code://prototypes //function declaration void discription(void); float polRecCon(int angle,int magnitude); void recPolCon(void); //------------------------------------------------------------------------------ //****************************************************************************** //****************************************************************************** //main programe int main(int argc, char *argv[]) { //declaring variables int choice; int again = 1; int horizontal; int vertical; //printing the menu puts("-----------------MENU--------------------\n"); printf("1.Discription of Application\n"); printf("2.Polar to rectangular Conversion\n"); printf("3.Rectangular to Polar Conversion\n"); printf("4.Exit Application\n"); printf("\n"); while (again == 1) { printf("Please enter your choice\n");//entering choice scanf("%d",& choice); system("cls"); //choosing one of the options if ( choice == 1) { discription(); } else if ( choice == 2) polRecCon(int horizontal,int vertical); else if ( choice == 3 ) recPolCon(); else exit(0); printf("\n"); printf("If you want to continue please press 1 or 0 to quit\n"); scanf("%d",& again); system("cls"); printf("\n"); puts("-----------------MENU--------------------\n"); printf("1.Discription of Application\n"); printf("2.Polar to rectangular Conversion\n"); printf("3.Rectangular to Polar Conversion\n"); printf("4.Exit Application\n"); printf("\n"); //printf("Please enter your choice\n"); // scanf("%d",& choice); // system("cls"); } system("PAUSE"); return 0; } //------------------------------------------------------------------------------ //****************************************************************************** //****************************************************************************** /*MENU */ /*DESCRIPTION FUNCTION */ void discription(void) { printf("The software application will calculate and display the equivalent \ rectangular coodinates of a vector whose polar form is given, or the equivalent \ polar coodinates of a vector whose rectangular form is given \n\n"); system("PAUSE"); system("cls"); // return 0; } /*POLAR TO RECTANGULAR CONVERSION*/ float polRecCon(int angle,int magnitude) { //declaring variables int x; int y; //prompting angle and magnitude printf("Please enter angle\n"); scanf("%d",&angle); printf("Please enter the magnitude\n"); scanf("%d",&magnitude); //calculating the value of x and y x = magnitude * cos(angle); y = magnitude * sin(angle); system("PAUSE"); system("cls"); return x; return y; } /*RECTANGULAR TO POLAR CONVERSION */ void recPolCon(void) { printf("Rectangular to polar conversion is done here\n\n"); system("PAUSE"); system("cls"); return 0; }
Why are you passing parameters into the polRecCon() function?
Since you're retrieving input from the user into these parameter variables there is no real reason to be passing these variables into the function.
Why are you trying to return an int value from a function you declared would be returning a float value?
Do you realize that int variables have no fractional parts?
Do you realize that both cos() and sin() expect a floating point value, and return a floating point value?
Do you know the difference between an int, float, and double?
You may want to review your documentation for how to create and use functions. This tutorial may also help.
Jim
to change values of 2 vars in a fuction - you pass them using pointers
Code:#include <stdio.h> static void f_test(int*x, int* y); int main() { int a = 5; int b = 6; printf("Original values a=%d, b=%d\n", a,b); f_test(&a,&b); printf("Changed values a=%d, b=%d\n", a,b); return 0; } static void f_test(int*x, int* y) { *x = 7; *y = 8; }
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
Use a structure to define a cartesian vector, and another to define a polar vector. (You can use a single structure for both, but that makes it just harder for us programmers.)
You can assign, copy, pass, and return variables of polar_t and cartesian_t type just as if they were normal number types.Code:#include <math.h> /* Cartesian 2D vector type. */ typedef struct { double x; double y; } cartesian_t; /* Polar 2D vector type. */ typedef struct { double angle; /* Radians: +x: 0, +y: Pi/2, -x: Pi or -Pi, -y: 3Pi/2 or -Pi/2 */ double length; } polar_t; /* This yields angle 'degrees' in radians. */ #define RADIANS(degrees) ((degrees)*3.14159265358979323846/180.0) /* This yields angle 'radians' in degrees. */ #define DEGREES(radians) ((radians)*180.0/3.14159265358979323846) cartesian_t cartesian(const double x, const double y) { const cartesian_t c = { x, y }; return c; } polar_t polar(const double angle, const double length) { const polar_t p = { angle, length }; return p; } cartesian_t polar_to_cartesian(const polar_t p) { const cartesian_t c = { cos(p.angle) * p.length, sin(p.angle) * p.length }; return c; } polar_t cartesian_to_polar(const cartesian_t c) { const polar_t p = { atan2(c.y, c.x), sqrt(c.x*c.x + c.y*c.y) }; return p; }
You can read for example a cartesian vector from standard input using e.g.
but I also included the "constructor" functions, so you can use e.g. cartesian(1.0,0.0) or polar(0.0,1.0).Code:cartesian_t vector; if (scanf(" %lf %lf", &vector.x, &vector.y) == 2) { /* have vector */ } else { /* bad input (not discarded), * complain and quit, or discard * from standard input somehow */ }
(Both yield a vector one unit from origin towards positive x axis).
Thanks guys,i used structures and the program frame work is working perfectly fine.