Thread: function help please

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    31

    function help please

    4. Write a program with two functions both called from main(). The first function just prints "Hello". In the second function ask the user to enter a number. Calculate the square root of the number and return the result to main(). In main() print the square root value.

    Code:
    
    
    Code:
    #include<stdio.h>
    #include<math.h>
    
    float fun3 (float );    
    main()
    {
        float answer;
        float a;
        answer = fun3(a);    
        puts("\nBack in main");    
        printf("\nThe result is %.2f", answer);    
        getch();
    }
    
    float fun3 (float c)    
    {
        float x;
        float y;
        printf("\nHello");
        printf("\n\nPlease enter a number: ");  
        scanf("%f",&x);                            
        y = sqrt(x);                         
        return (y);                        
    }
    Can you help me Please? I made a logic mistake in function.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    First you need two functions, not one.
    The first function, will print the Hello and the other one will do the math.
    The math seems ok at first glance.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    55

    Fixed your code...

    Not sure what you were thinking with the variable passing to your square root function, but whatever. I changed your fun3's parameters to void, and created an actual function for the Hello thing. Also I fixed your getch function, by using a getc(stdin) instead. If you're trying to do what I think your doing though, it might not work. Compiliers (Like mine) usually just ignore it because they think there is no point in using it. You might want to replace it with a macro and the system functions. Anyways, this is the program I came up with after fixing yours.

    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    float fun3 ( void );
    void HelloWorld( void );
    
    
    int main()
    {
        float answer;
    
    
        HelloWorld();
    
    
        answer = fun3();
    
    
        puts("\nBack in main");
    
    
        printf("\nThe result is %.2f", answer);
    
    
        getc(stdin);
    
    
        return 0;
    }
    
    
    float fun3 ( void )
    {
        float x;
        float y;
    
    
        printf("\nPlease enter a number: ");
    
    
        scanf("%f",&x);
        y = sqrt(x);
        return (y);
    }
    
    
    void HelloWorld( void ) {
        puts("\nHello");
        return;
    }

  4. #4
    Registered User
    Join Date
    Mar 2013
    Posts
    31
    Thank You So Much BatchProgrammer! You help me a lot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 03-20-2012, 08:29 AM
  2. Replies: 2
    Last Post: 02-26-2009, 11:48 PM
  3. Print function: sending a function.. through a function?
    By scarlet00014 in forum C Programming
    Replies: 3
    Last Post: 11-05-2008, 05:03 PM
  4. Replies: 14
    Last Post: 03-02-2008, 01:27 PM
  5. Replies: 9
    Last Post: 01-02-2007, 04:22 PM