PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, April 28, 2022

[FIXED] Why does the function foo cannot return properly?

 April 28, 2022     c, debugging, recursion, warnings     No comments   

Issue

I am trying to write a recursive function in C programming but the compiler keeps give me warning: control may reach end of non-void function. I don't know where do i get wrong.

int foo(int* ptr){
  int flag = 0;
  --(*ptr);
  if((*ptr)!=0) flag = 1;
  if(flag == 0) return 1;
  else foo(ptr);
}

int main()
{
  int count=10;
  int* ptr = &count;

  int n = foo(ptr);
  printf("%d", n);

  return 0;
}

Thank you.


Solution

in your else statement in function foo you are not returning anything.

it should be like this:

int foo(int* ptr) {
    int flag = 0;
    --(*ptr);
    if ((*ptr) != 0) 
        flag = 1;
    if (flag == 0) 
        return 1;
    else return foo(ptr);
}


Answered By - hanie
Answer Checked By - Pedro (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing