First of all you should add the sensor in Robot/Motors and sensors menu. I read in the documentation that the S4 is best for the Sonar sensor, so make sure you connect it to the appropriate place. You should write a name for the sonar for example sonarSensor and select the sonar as type.
You can write something like this:
Here rightDistance and leftDistance are global variables, so you can read/write their values from anywhere in your code. If you don't need the measured distances, you can declare leftDistance and rightDistance in the whichWayToGo function. You have to write your turning/moving codes to the appropriate places.