160 likes | 172 Views
Learn how subroutines help organize and structure code, the difference between procedures and functions, and variable scope.
E N D
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.
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.
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
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()
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”)
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)
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()
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)
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)
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.
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.
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.
''' 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()
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