Difference between calling a task and dependsOn a task

Why if i call build task on root project, then it is called in all subprojects too.
But if i specify dependsOn build in root project for some other task and call that task, then build is not called in subprojects.

For example

task publish {
    dependsOn build
}

When publish is called, build is not called in subprojects.

What is the logic behind this?

It’s because the command-line tries to be helpful and searches for tasks in all subprojects.

task publish {
    dependsOn build
}

Means publish depends on the task named build. Gradle doesn’t look for any other task named build.

gradle build means "run all tasks that match the name build"
gradle :build means "run the task with the path :build"
gradle :subproject:build means "run the task with the path :subproject:build"