Thread: function program, return value to remove warning message (math.h)

  1. #1
    Registered User
    Join Date
    Feb 2018
    Location
    San Diego, CA
    Posts
    122

    Exclamation function program, return value to remove warning message (math.h)

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    double sqrt_function (double number);
    
    int main (void)
    {
    	double val;
    	double p = 0;
    	printf("input number: ");
    	scanf("%lf", &val);
    
    	p = sqrt_function (val);	
    
    	printf("\n\n\t%f\n\n", p);
    	return 0;
    }
    
    double sqrt_function (double number)
    {
    	sqrt (number);
    
    	return;
    }
    please, with -Wall and -lm on Debian linux...

    Code:
    jamie@box6:~/git/square_c$ cc -Wall -lm lost.c
    lost.c: In function ‘sqrt_function’:
    lost.c:24:2: warning: ‘return’ with no value, in function returning non-void
      return;
      ^~~~~~
    lost.c:20:8: note: declared here
     double sqrt_function (double number)
            ^~~~~~~~~~~~~
    jamie@box6:~/git/square_c$
    code works, what would be a better way to write the sqrt_function?

    Thanks!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,811
    > code works, what would be a better way to write the sqrt_function?
    Only by dumb luck.

    Code:
    double sqrt_function (double number)
    {
        double answer = sqrt (number);// you were just ignoring the result
     
        return answer; // this is a value to return
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2019
    Posts
    46
    Quote Originally Posted by _jamie View Post

    Code:
    jamie@box6:~/git/square_c$ cc -Wall -lm lost.c
    lost.c: In function ‘sqrt_function’:
    lost.c:24:2: warning: ‘return’ with no value, in function returning non-void
      return;
      ^~~~~~
    lost.c:20:8: note: declared here
     double sqrt_function (double number)
            ^~~~~~~~~~~~~
    jamie@box6:~/git/square_c$
    code works, what would be a better way to write the sqrt_function?

    Thanks!
    Haha. Interdersting. Had to try this out. It actually works, yes.

    It would have been great to test this out with clang but:

    Code:
    Testsqrt.c:24:5: error: non-void function 'sqrt_function' should return a value [-Wreturn-type]
        return;
        ^
    1 error generated.
    It reports an error instead of a warning ;-(

  4. #4
    Registered User
    Join Date
    Feb 2018
    Location
    San Diego, CA
    Posts
    122
    Quote Originally Posted by Salem View Post
    > code works, what would be a better way to write the sqrt_function?
    Only by dumb luck.

    Code:
    double sqrt_function (double number)
    {
        double answer = sqrt (number);// you were just ignoring the result
     
        return answer; // this is a value to return
    }

    thanks, looks good, and works great.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help (warning 'return' with a value in function returning void
    By Suleman MenGal in forum C Programming
    Replies: 2
    Last Post: 12-11-2017, 08:54 AM
  2. Why it give warning message, how will it remove
    By hitlar in forum C Programming
    Replies: 2
    Last Post: 01-31-2014, 02:50 PM
  3. remove warning message multi dimensional pointers
    By sandbucket in forum C Programming
    Replies: 28
    Last Post: 11-30-2012, 04:19 AM
  4. How to remove an error message from a p C++ program
    By tweetybrd22 in forum C++ Programming
    Replies: 2
    Last Post: 10-04-2010, 07:00 PM
  5. Replies: 4
    Last Post: 01-02-2002, 10:57 PM

Tags for this Thread