Thread: Function Calling

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    5

    Function Calling

    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.

  2. #2
    Registered User catasturslykid's Avatar
    Join Date
    Jul 2013
    Location
    Barcelona, Spain
    Posts
    72
    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.

    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;
    
    }
    So, you will see in the screen:


    x = 5 y = 9
    Last edited by catasturslykid; 08-17-2013 at 07:11 AM.

  3. #3
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    void returnX();
    void returnY();

    And both of these functions return an int? I want some of what you're smoking.

  4. #4
    Registered User catasturslykid's Avatar
    Join Date
    Jul 2013
    Location
    Barcelona, Spain
    Posts
    72
    Haha, what a fail! Sorry! Corrected!

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    but let me explain how to call a function in C if you want to return a int,
    It would help if you explained it correctly. There are several flaws in your example program.

    First main() should be defined to return in int, not void, and you should return an int from this function.

    Code:
    int main(void)
    {
    
       return 0;
    }
    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 returnX(void)
    {
       int aux;
    
       aux = 5;
     
       return aux; //The function returns the number contained in the variable AUX. (In that case, 5).
     
    }
    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.


    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.

    Code:
    void modifyTwoValues( int *value1, int *value2);
    
    ...
    
    void modifyTwoValues( int *value1, int *value2)
    {
       *value1 = 5;
       *value2 = 78;
    }
    Jim

  6. #6
    Registered User
    Join Date
    Aug 2013
    Posts
    5
    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;
    }

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    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

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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

  9. #9
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    Quote Originally Posted by Coll View Post
    [I] want to convert a polar vector to a rectangular vector.
    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.)

    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 assign, copy, pass, and return variables of polar_t and cartesian_t type just as if they were normal number types.

    You can read for example a cartesian vector from standard input using e.g.
    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 */
        }
    but I also included the "constructor" functions, so you can use e.g. cartesian(1.0,0.0) or polar(0.0,1.0).
    (Both yield a vector one unit from origin towards positive x axis).

  10. #10
    Registered User
    Join Date
    Aug 2013
    Posts
    5
    Thanks guys,i used structures and the program frame work is working perfectly fine.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 06-10-2013, 04:03 PM
  2. Replies: 3
    Last Post: 10-23-2012, 02:37 PM
  3. Replies: 15
    Last Post: 06-09-2009, 02:19 AM
  4. Calling Cdocument function in Cview function
    By RancidWannaRiot in forum Windows Programming
    Replies: 5
    Last Post: 09-22-2005, 12:09 PM
  5. Question on function syntax and calling function
    By cbrman in forum C Programming
    Replies: 10
    Last Post: 10-05-2003, 05:32 PM