[PYTHON] [For beginners] I don't understand "return" An article for myself two years ago (a little if-else story)

What you can learn by reading this article

・ People who are new to programming and still do not know how to use "return" ・ I was 3 years ago

Background

I was writing a program at graduate school three years ago. I somehow remember writing desperately because it was necessary for the analysis of experiments in university research.

For the time being, the analysis program I want to do is working, I was delighted to use the program for analysis.

After a paragraph, if you look at the program, I thought, "The source is pretty dirty ..." and tried to fix it.

However, the spaghetti feeling * of the sauce is not perfect, I couldn't fix it easily, and the source was the same as last time, I remember rewriting from scratch.

What was happening with the source three years ago

It's an approximate image, but it looks like this.

sample.swift


func yabaiCode() {
if condition 1{
Process 1(15 lines or more)
Process A(10 lines or more)
Process 4(10 lines or more)
    }else if condition 2{
Process 2(15 lines or more)
Process A`(10 lines or more)
Process 5(10 lines or more)
    }if condition 3{
Process 3(15 lines or more)
Process A``(10 lines or more)
Process 6(10 lines or more)
    } else {
Process 7(20 lines or more)
    }
}

What I want to say is that the processing inside the conditional branch is ridiculously long. However, I want to divide the processing, but I don't know how to divide it. Apparently, if you divide it by method, you can process it more easily.

But I couldn't divide by method. I didn't know how to pass values between methods ...

So, after all, I graduated leaving the code as it is without knowing it (sorry for my junior). Now that I understand the return statement to some extent, I wrote this article to teach myself the return in the past.

There are two main uses for return

1. End the method

sample.swift


func return1() {

    num = 5 //Enter your favorite number

    if num == 4 {
        print("4")
        return
    } else if num == 5 {
        print("5")
        return 
        //This method should end here
        //No further processing
    }  if num == 6 {
        print("6")
        return
    } else {
        print("I do not understand")
        return
    }
}

This return can ** eliminate unnecessary processing **. If you get an answer on the way, you can end the process at that point, Also, thanks to the return, you don't have to use ʻif-else`.

Since if-else requires a lot of processing and it is assumed that if and else are at the same time. The processing per method may increase. It can be difficult to read. Depending on the workplace, there may be "ʻif-else` prohibited!" (It was in my department)

How much will the code be easier to read without if-else? I would like to refer to the FizzBuzz statement.

First, the one using the if-else statement.

FizzBuzz.swift


func fizzBuzz() {
    for i in 1...30 {
        judgeFizzBuzz(num: i)
    }
}

func judgeFizzBuzz(num : Int) {
    if num % 15 == 0 {
        print("FizzBuzz")
    } else if num % 3 == 0 {
        print("Fizz")
    } else if num % 5 == 0 {
        print("Buzz")
    } else {
        print(num)
    }
}

It's not really bad, but if you want to include processing for multiples of 7, If you're not careful, all the processing will be destroyed. (Of course you need to be careful when using return)

Now, let's use the return statement.

FizzBuzz.swift


func fizzBuzz() {
    for i in 1...30 {
        judgeFizzBuzz(num: i)
    }
}

func judgeFizzBuzz(num : Int) {
    if num % 15 == 0 {
        print("FizzBuzz")
        return
    } 

    if num % 3 == 0 {
        print("Fizz")
        return
    } 

    if num % 5 == 0 {
        print("Buzz")
        return
    }
    
    print(num)
}

Since the if statement is made into parts, it is really easy to see. it is really amazing. It is obvious that num will be printed if there is nothing.

2. Returns the value

Here we use a method that returns a number

sample.swift


func say() {
    num = 5
    doubleNum = doubleNum(num) //10 is returned
}

//A function that returns a doubled number
func doubleNum(num :Int) -> Int {
    return num * 2 //Returns a number
}

It is also easy to calculate with another method and have the processing result returned.

You may find out more with FizzBuzz.

FizzBuzz.swift


func fizzBuzz() {
    for i in 1...30 {
        print(judgeFizzBuzz(num: i))
    }
}

func judgeFizzBuzz(num : Int) -> String {
    if num % 15 == 0 {
        return "FizzBuzz"
    }
    
    if num % 3 == 0 {
        return "Fizz"
    }

    if num % 5 == 0 {
        return "Buzz"
    }

    return String(num)
    
}

I think it's even easier to see than the one I wrote in 1. Now that judgeFizzBuzz returns a String, the overall amount of code is also reduced (print)

Summary

If you don't use return well, the method will be overwhelming, so if you haven't used it, please use it. I often graduated from college without knowing this, I ...

Recommended Posts

[For beginners] I don't understand "return" An article for myself two years ago (a little if-else story)