1 / 15

Subroutines

Learn how subroutines help organize and structure code, the difference between procedures and functions, and variable scope.

bchung
Download Presentation

Subroutines

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Subroutines

  2. Learning objectives • Explain how subroutines help to organise and structure code. • Explain the difference between procedures and functions. • Explain what variable scope is and the difference between local and global variables.

  3. Subroutines: Functions and Procedures Subroutines are a way of managing and organising programs in a structured way. This allows us to break up programs into smaller chunks. By now you will have written lots of programs and you will have noticed that they are getting quite long and increasingly more difficult to debug. Now is a good time to learn about subroutines because they will help you to make your code more modular.

  4. Functions • Subroutines that do not return values and do not have to have input parameters are called procedures. • Subroutines that allow us to have both input and output are called functions. Functions takes some input and convert them to an output and return at least 1 value. • input -> function -> output • The features of a function are: • Has a name: identifier • Has input parameters • Has a return • function_identifier(parameter1, parameter2, …) • return value

  5. Procedure with no input Procedures do not have to return values and do not have to have input parameters. A simple procedure with out input or input is given here. defgreeting(): print(“Hello") To call the procedure we simple write its name. Note that we still include the bracket even though there are no inputs. greeting()

  6. Procedure with 1 input parameter Procedures do not have to return values and do not have to have input parameters. A simple procedure with out input or input is given here. defgreeting(name): print(“Hello",name) To call the procedure we simple write its name, with the parameter value inside the brackets. greeting(“Fred”)

  7. Procedure with 2 parameters Procedures do not have to return values and do not have to have input parameters. A simple procedure with out input or input is given here. defgreeting(name,age): print("Wagwan",name,"youare",age) There are now two parameters in the brackets separated by a comma. greeting("Tom",14)

  8. Procedure with no input Procedures do not have to return values and do not have to have input parameters. A simple procedure with out input or input is given here. defgreeting(): print("Wagwan") To call the procedure we simple write its name. Note that we still include the bracket eve though there are no imputs. greeting()

  9. Function with 1 parameter and return value Functions have to return values do not have to return values and do not have to have input parameters. A simple procedure with out input or input is given here. defsqaure(x): x_sqaure=x*x returnx_sqaure To call a function write its name with the value of the parameter in the brackets square(9) To set the output of the function to a variable we can assign the output to a variable x2=square(9)

  10. Function with 1 parameter and return value The function below now has two input values: defadd(x,y): x_plus_y=x+y returnx_plus_y And to call we use: total=add(3,4) print(total)

  11. Variable scope The scope of a variable determines which parts of a program can access and use that variable. A global variable is a variable that can be used anywhere in a program. The issue with global variables is that one part of the code may inadvertently modify the value because global variables are hard to track. A local variable is a variable that can only be accessed within a certain block of code typically within a function. Local variables are simple not recognized outside a function unless they are returned. There is no way of modifying or changing the behavior of a local variable outside its scope.

  12. Function with 1 parameter and return value The function below now has one input value and returns two values: # Temperature conversion to Celsuis and Kelvin defconversion(f): frac=5/9 celsius=(f -32)*frac kelvin =celsius-273.0 returncelsius, kelvin c,k= conversion(83) print(c,k) In the function conversion the variables frac, celsius and kelvin are local variables. This means that they cannot be accessed at all from outside the function which is why we need to return kelvin and celsius if we want to do something else with variables. On the other hand frac is not useful to us outside the function so we do no need to return it.

  13. Function and procedures Exercises Write a program that uses a function that asks a user for a number and then divides that number in half Write a program with functions that returns the cube of a number. That is a number multiplied by itself 3 times. Write a program that asks the user for the width, length and height of a cuboid and calculates the volume Write a program that asks the user for the two sides of a right angled triangle and uses Pythagoras to calculate the hypotenuse.

  14. ''' Calculate hypotenuse of a right angle triangle ''' importmath defget_user_input(): print("Calculate hypotenuse of a right angle triangle") side1=float(input("Enter length of side 1: ")) side2=float(input("Enter length of side 2: ")) returnside1,side2 defprint_output(h): print("The hypotenuse is:",h) defpythag(x,y): h=math.sqrt(x*x+y*y) returnh defmain(): x,y=get_user_input() h=pythag(x,y) print_output(h) main()

  15. Plenary • What is the difference between a procedure and a function? • What do we mean by the scope of a variable? • How many parameters does this subroutine have? • What is missing from this function? defadd(x,y,z): sum=x+y+z

More Related