Beginner question: passing values between functions

This is a discussion on Beginner question: passing values between functions within the C Programming forums, part of the General Programming Boards category; I'm trying to read in values with one function and then do a simple calculation and print the result using ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    5

    Beginner question: passing values between functions

    I'm trying to read in values with one function and then do a simple calculation and print the result using second function. In the end the program is going to be more complicated but that's what I started from.

    However I ran into problems already. As the result I'm expecting ln(first input number) but that's not what I'm getting. I'm having doubts that the way I do it now wont pass values to second function but I'm not really sure either where the problem is.

    That's the code I'm using:
    Code:
    #include <stdio.h>
    #include <math.h>
    
    double sisestus(double, double, double);
    double arvutus(double, double);
    
    main(void)
    {
        double a,b,h;
        sisestus(a,b,h);
        
        double summa;
        arvutus(a, summa);
        
        printf("Vajuta suvalisele klahvile...");
        getch(); 
        return 0;
    }
    
    
    /*Sisestus*/
    double sisestus(double s1, double s2, double s3)
    {    
        printf("Sisesta argumendi vähim väärtus.\n");
        scanf("%lf",&s1);
        printf("Vähim väärtus on %3.2lf\n", s1);
        
        printf("Sisesta argumendi suurim väärtus.\n");
        scanf("%lf",&s2);
        printf("suurim väärtus on %3.2lf\n", s2);
        
        printf("Sisesta samm.\n");
        scanf("%lf",&s3);
        printf("Samm on %3.2lf\n", s3);	
    }
    
    
    /*Arvutamine*/
    double arvutus(double s1, double s2)
    {
        s2=log(s1);
        printf("%lf\n", s2);
    }
    Any help and explanation is appreciated.

  2. #2
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Ok, you're not passing be reference. You need to send the address of your variables if you want your function to actually change their values (instead of just a copy of them).
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    You only pass the address of a value if you are using pointers
    I'm just trying to be a better person - My Name Is Earl

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Code:
    printf("%lf\n", s2);
    scanf() uses %f for floats and %lf for doubles, while printf() uses %lf for floats and doubles.

    getch() is typically in <conio.h>.

    [edit]
    Code:
    printf("V&#228;him v&#228;&#228;rtus on %3.2lf\n", s1);
    I think "&#228;" is an extended ASCII character; to use it in a program, you should probably figure out the ASCII value for it and convert it to octal, using it like this
    Code:
    "V\123him"
    or convert it to hex and use it like this
    Code:
    "V\x1ahim"
    You might want to look at these tutorials:
    [/edit]
    Last edited by dwks; 11-18-2006 at 04:26 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Quote Originally Posted by swgh
    You only pass the address of a value if you are using pointers
    Yes, which is typically what you'd want to do to alter an argument being passed to your function.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  6. #6
    Registered User
    Join Date
    Jun 2006
    Posts
    75
    Quote Originally Posted by dwks
    while printf() uses %lf for floats and doubles.
    No, printf() uses %f for floats and doubles.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 12-13-2008, 08:32 AM
  2. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 02:29 PM
  3. Replies: 12
    Last Post: 10-23-2006, 07:45 AM
  4. Passing Values Question...
    By tinkerbell20 in forum C++ Programming
    Replies: 9
    Last Post: 06-27-2005, 11:33 AM
  5. structure question: passing to functions
    By kocika73 in forum C Programming
    Replies: 4
    Last Post: 03-07-2005, 07:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21