Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

return type is not working for a function #3129

Closed
vishcodex opened this issue Jun 26, 2024 · 2 comments
Closed

return type is not working for a function #3129

vishcodex opened this issue Jun 26, 2024 · 2 comments
Labels
bug Something isn't working mojo-repo Tag all issues with this label

Comments

@vishcodex
Copy link

vishcodex commented Jun 26, 2024

Bug description

Summary: return statement is not working on a function

Description. While going through the documentation i was testing the functionalities of mojo, when a function with return statement is called in the main function its giving error: cannot implicitly convert 'Int' value to 'None' in return value error. Updating return to print statement is working well.

Environment details: Mac M1
Mojo Compiler Version: mojo 24.4.0
Operating System version: MacOS Sonoma Version 14.5
Hardware Specifications: 32 GB RAM
Severity/frequency: Low

Steps to reproduce

  • Include relevant code snippet or link to code that did not work as expected.
  • return statement
fn sum_values(*values: Int):
    var sum: Int = 0
    for value in values:
        sum += value
   return sum

fn main() raises:
    var result = sum_values(3,5)
  • print statement
fn sum_values(*values: Int):
    var sum: Int = 0
    for value in values:
        sum += value
    print(sum)

fn main() raises:
    var result = sum_values(3,5)
  • If applicable, add screenshots to help explain the problem.

with return type :

image

with print statement:

image
  • If using the Playground, name the pre-existing notebook that failed and the steps that led to failure.
  • Include anything else that might help us debug the issue.

System information

- What OS did you do install Mojo on? 
MacOS Sonoma Version 14.5
- Provide version information for Mojo by pasting the output of `mojo -v` 
mojo 24.4.0
- Provide Modular CLI version by pasting the output of `modular -v`
modular 0.8.0
@vishcodex vishcodex added bug Something isn't working mojo-repo Tag all issues with this label labels Jun 26, 2024
@soraros
Copy link
Contributor

soraros commented Jun 26, 2024

It's not a bug. You have to specify the return type in sum_value:

fn sum_values(*values: Int) -> Int:  # <<<
    var s = 0
    for value in values:
        s += value
   return s

fn main():
    var result = sum_values(3, 5)
    print(result)

print works in the other example because fn f(...): ... is the same as fn f(...) -> None: ....

@vishcodex
Copy link
Author

thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working mojo-repo Tag all issues with this label
Projects
None yet
Development

No branches or pull requests

2 participants