Thread: passing to a function y should be 4 but its zero

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    808

    passing to a function y should be 4 but its zero

    here is a simplfied verstion of the function i am passing x and y to but it doesnt work y should equate to 4 when passed to the function but for some reason its zero.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int func(int x, int y);
    
    int main()
    {
        int coordinate_x=2, coordinate_y=6;
        int multiply_x = 1, multiply_y = -1;
    
        if (func(coordinate_x + (2 * multiply_x), coordinate_y + (2 * multiply_y) == 0)) //check in case more than one jump
        {
            printf("func worked\n");
        }
    
        return 0;
    }
    
    int func(int x, int y)
    {
        printf("x = %d and y = %d\n", x, y);
        if (x ==4 && y ==4)
        {
            printf("this bit worked\n");
            return 0;
        }
        else
        {
            printf("it didnt\n");
            return -1;
        }
    }
    i been staring at this for 2 hours and i cant see what is wrong. the same method works 20 times in other places.

    many thanks
    coop

  2. #2
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Line 11 reads:

    Code:
    if ( func(coordinate_x + (2 * multiply_x), 
              coordinate_y + (2 * multiply_y) == 0) )
    Notice the second argument is cordinate_y + (2 * multiply_y) == 0.
    It should be:
    Code:
    if ( func(coordinate_x + (2 * multiply_x), 
              coordinate_y + (2 * multiply_y)) == 0)

  3. #3
    Registered User
    Join Date
    Apr 2019
    Posts
    62
    If your text editor or IDE has a "highlight matching parenthesis" feature, it's a really useful tool for wrangling nested parentheses like this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 10-02-2018, 09:52 AM
  2. Passing the address of a function to a function.
    By SqueezyPeas in forum C Programming
    Replies: 2
    Last Post: 09-17-2015, 07:42 AM
  3. Passing variable from function - main - function
    By ulti-killer in forum C Programming
    Replies: 2
    Last Post: 11-01-2012, 12:14 PM
  4. Replies: 1
    Last Post: 09-04-2007, 10:00 PM
  5. Passing values from function to function
    By itld in forum C++ Programming
    Replies: 2
    Last Post: 10-24-2001, 07:28 AM

Tags for this Thread