output generated even though there's no return in function ?

This is a discussion on output generated even though there's no return in function ? within the C Programming forums, part of the General Programming Boards category; Code: #include<stdio.h> int power(int,int); main() { int x,y,o=1; while(o==1) { printf("\nEnter the 2 desired numbers : "); scanf("%d%d",&x,&y); printf("\nThe answer ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    15

    output generated even though there's no return in function ?

    Code:
    #include<stdio.h>
    int power(int,int);
    main()
    {
    	int x,y,o=1;
    	while(o==1)
    	{
    	printf("\nEnter the 2 desired numbers : ");
    	scanf("%d%d",&x,&y);
    	printf("\nThe answer is %d.\n",power(x,y));
    	printf("\nWant another one ?\n1.Yes\n2.No\n");
    	scanf("%d",&o);
    	}
    }
    int power(int a,int b)
    {
    	int i,res=1;
    	for(i=1;i<=b;i++)
    	{
    		res=res*a;
    	}
    }
    I haven't given any return for power function. But I'm getting an output of y+1 !!!

    Why is this happening ?

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Yes, it's actually a feature.
    I wrote my rand() function using this feature.

    Code:
    int rand() {
    // no return !
    }

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    15
    but the output is fixed...I'm getting the 2nd number +1 as the output...why ?

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    "Why" is because you declared it as returning int.

    So you get whatever junk value happens to be in the return location in the code generated by the compiler.
    Most of the time, this looks something like the result of the last arithmetic operation in the code.

    In any event, use a better compiler and write better code.
    Code:
    $ gcc -Wall bar.c
    bar.c:4: warning: return type defaults to ‘int’
    bar.c: In function ‘power’:
    bar.c:22: warning: control reaches end of non-void function
    bar.c: In function ‘main’:
    bar.c:14: warning: control reaches end of non-void function
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Sep 2010
    Posts
    19
    Quote Originally Posted by Salem View Post
    "Why" is because you declared it as returning int.

    So you get whatever junk value happens to be in the return location in the code generated by the compiler.
    Most of the time, this looks something like the result of the last arithmetic operation in the code.
    which is the value of i, the last computation before the termination of the loop.
    In any event, use a better compiler and write better code.
    Nothing is necessarily wrong w/ his compiler, just had to compile with warning option/strict mode etc. like you did

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    Do you equate "0 warnings 0 errors" with being "bug free" ?

    Even when you know "the answer", it isn't information you can use in any other program / OS / Compiler combination.

    It's like arguing over what is x++ + x++ or fflush(stdin), the answer is deep in undefined behaviour. So spending time analysing a specific case is a complete waste of effort, because you won't be able to use what you know in any other program.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Sep 2010
    Posts
    19
    couldn't agree more.
    just explained where the return value comes from, and yeah, it is an undefined behaviour.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can I make this code more elegant?
    By ejohns85 in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2009, 08:55 AM
  2. New string functions
    By Elysia in forum C Programming
    Replies: 11
    Last Post: 03-28-2009, 05:03 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  5. C++ FTP class won't work
    By lord mazdak in forum C++ Programming
    Replies: 8
    Last Post: 12-18-2005, 06:57 AM

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