Thread: "FUNCTIONS" New to C language. Confused about an example done in class.

  1. #1
    Registered User
    Join Date
    Apr 2015
    Posts
    2

    "FUNCTIONS" New to C language. Confused about an example done in class.

    This is an example that our prof gave us to solve. We basically had to fill in the blanks. I've bold the blanks that we filled in.
    My answer differs with the prof's.
    Here's the question: Assume a car moves with a constant speed, s. Given the travel time, t and speed, the total distance traveled is given by d = s X t.

    My answer:
    Code:
    #include<stdio.h>
    void dist(double d, double s, double t)
    {
    d= s*t;
    }
    int main(void)
    {
    double d,s=20, t=20;
    dist(d, s, t);
    printf("Distance traveled is %lf",d);
    return(0);
    }
    Professor's Answer:
    Code:
    #include<stdio.h>
    void dist(double *d, double s, double t)
    {
    *d= s*t;
    }
    int main(void)
    {
    double d,s=20, t=20;
    dist(&d, s, t);
    printf("Distance traveled is %lf",d);
    return(0);
    }
    I don't understand why he used "&d" and "*d" instead of "d" in the function and while calling function. I'll be really thankful if somebody explains that to me.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    When you pass a variable to a function, that function works on a copy of that variable. It can do whatever it wants, but the "original" variable you passed to it remains untouched.

    So in your case, whatever you do to the variable "d" in the function is invisible to the caller.

    If you want to actually change the value of the variable in the caller, you need to pass a pointer to that variable to the function.



    It's like ... if you mail a letter to a stranger, and that letter says "2", then they can do whatever they want to the letter, but you will always know it says "2".

    But if you mail that letter with a return address, they can modify it and send it back, and now it might say "6" instead.




    Here's a simple example that illustrates the point:

    Code:
    #include <stdio.h>
    
    void function_1(int x);
    void function_2(int *x);
    
    int main(void)
    {
        int x = 2;
    
        printf("in main, x = %d\n\n",x);
    
        function_1(x);
    
        printf("after function_1(), x = %d\n\n",x);
    
        function_2(&x);
    
        printf("after function_2(), x = %d\n\n",x);
    
        return 0;
    }
    
    void function_1(int x)
    {
        x = 4;
    
        printf("in function_1(), x = %d\n",x);
    }
    
    void function_2(int *x)
    {
        *x = 6;
    
        printf("in function_2(), x = %d\n",*x);
    }


    Note that a pointer is an address of a variable. So if you want to pass a pointer of a normal variable, you can use the "address-of" (&) operator.
    Last edited by Matticus; 04-06-2015 at 08:02 PM.

  3. #3
    Registered User
    Join Date
    Apr 2015
    Posts
    2
    Thank you so much.

  4. #4
    Registered User
    Join Date
    Apr 2015
    Posts
    3
    Another solution is to make a header file name it dist.h(or whateveryouwant.h) that will look like this and in your main program right next to stdio.h you add #include"dist.h" and boom your code works fine without modifying it.

    #ifndef
    HEADER_FILE
    #define HEADER_FILE

    void dist(double d, double s, double t);

    #endif

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Stephan-Alex View Post
    Another solution is to make a header file name it dist.h(or whateveryouwant.h) that will look like this and in your main program right next to stdio.h you add #include"dist.h" and boom your code works fine without modifying it.
    I really doubt it.
    It is like saying that
    Add 0 to you A+B sum and boom your result will be different.

    After applying preprocessor - the resulting code will be exactly the same - so no reason that compiler will produce different output using same input.
    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

  6. #6
    Registered User
    Join Date
    Mar 2015
    Location
    BE
    Posts
    66
    You do not need those stars and ampersands if you make your function returns a value, example:

    Code:
    #include <stdio.h>
    
    double dist(double s, double t) 
    {
        return s*t;
    }
    
    int main(void)
    {
        double s = 20, t = 20;
        printf("Distance traveled is %lf",dist(s, t));
        
        return(0);
    }
    But C language without pointers is just like a wedding without a bride.

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by Carnotter View Post
    You do not need those stars and ampersands if you make your function returns a value, example:
    It would appear that the point of the given exercise is to pass a variable by pointer and update it thusly.

    Quote Originally Posted by Carnotter View Post
    But C language without pointers is just like a wedding without a bride.
    ... and if you're not careful, either can cause undefined behavior

  8. #8
    Registered User
    Join Date
    Mar 2015
    Location
    BE
    Posts
    66
    Quote Originally Posted by Matticus View Post
    It would appear that the point of the given exercise is to pass a variable by pointer and update it thusly.
    ... and if you're not careful, either can cause undefined behavior
    Yes, I know and some collegues already made me afraid of those pointers. We didn't learn them yet, but I did some reading. We can pass a variable by reference without using * or &. An array in itself is a pointer, but no need to mention that using those symbols. Sometimes it's confusing. Here 2 programs, the first works and the second not!

    1.

    Code:
    #include <stdio.h>
    
    void dist(double d[1], double s, double t) 
    {
        d[0]= s*t;
    }
    
    int main(void)
    {
        double d[1];
        double s = 20, t = 20;
        dist(d, s, t);
        printf("Distance traveled is %lf",d[0]);
        
        return(0);
    }
    2.

    Code:
    #include <stdio.h>
    
    void dist(double d[1], double s, double t) 
    {
        d[0]= s*t;
    }
    
    int main(void)
    {
        double d[1];
        double s = 20, t = 20;
        dist(d[] s, t);
        printf("Distance traveled is %lf",d[0]);
        
        return(0);
    }

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    An array in itself is a pointer
    To be absolutely clear, an array name by itself acts as a pointer to the first element of that array.

    Arrays and pointers, however, are not the same thing.

    Question 6.3
    Question 6.4

    That whole site is worth a good read.

    Yes, I know and some collegues already made me afraid of those pointers.
    Do not be afraid of pointers. While they are usually one of the most challenging C concepts for beginners to understand, all it takes is some practice and experience to make it "click".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Nested functions for a "new language"
    By MeNeedsHelp in forum C Programming
    Replies: 12
    Last Post: 09-24-2014, 06:37 PM
  2. Marking functions as "Local" or "Global"
    By zyxwvuts in forum C Programming
    Replies: 10
    Last Post: 02-22-2014, 07:58 AM
  3. the operator "="error in class "String"
    By boyhailong in forum C++ Programming
    Replies: 9
    Last Post: 07-27-2011, 08:39 PM
  4. I am confused by "Linux thread" and "NPTL"
    By meili100 in forum Linux Programming
    Replies: 6
    Last Post: 03-27-2008, 12:14 PM
  5. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM

Tags for this Thread