0% found this document useful (0 votes)
8 views

Functions Slides

The document discusses Python functions. It defines several functions like print(), input(), int(), and randint() and explains what they do. It then discusses defining custom functions, how to call functions, and the differences between local and global scope for variables used in functions. Functions allow breaking programs into reusable pieces of code and organizing code logically. Defining functions involves specifying the function name, parameters, and body. Functions can return values.

Uploaded by

yeooko
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Functions Slides

The document discusses Python functions. It defines several functions like print(), input(), int(), and randint() and explains what they do. It then discusses defining custom functions, how to call functions, and the differences between local and global scope for variables used in functions. Functions allow breaking programs into reusable pieces of code and organizing code logically. Defining functions involves specifying the function name, parameters, and body. Functions can return values.

Uploaded by

yeooko
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Python Functions

Sarah Holderness
PLURALSIGHT AUTHOR
@dr_holderness
Functions We’ve Used
Functions are like mini-programs that complete a specific task.
We might not know how these work, but they give the expected results.

print() takes in one string (or multiple strings)


print('Hello World')
and prints them to the console
Functions We’ve Used
Functions are like mini-programs that complete a specific task.
We might not know how these work, but they give the expected results.

print('Hello World')

input() prompts the user for input


name = input('Enter your name:\n')
and returns the string they entered.
Functions We’ve Used
Functions are like mini-programs that complete a specific task.
We might not know how these work, but they give the expected results.

print('Hello World')

name = input('Enter your name:\n')

int() converts the given number to


amount = int(10.6)
an integer.
Functions We’ve Used
Functions are like mini-programs that complete a specific task.
We might not know how these work, but they give the expected results.

print('Hello World')

name = input('Enter your name:\n')

amount = int(10.6)

roll = random.randint(1,6) randint() takes in a low and high bound and


returns a random integer within that range.
Functions We’ve Used
Functions are like mini-programs that complete a specific task.
We might not know how these work, but they give the expected results.

print('Hello World')

name = input('Enter your name:\n')

amount = int(10.6)

roll = random.randint(1,6)

We can define a function to do anything we want


and once we do we can use it over and over again.
Defining a Function
We want a simple function that prints a greeting for a given name.

def Function 0 to many


Keyword name Parameter(s)

def greeting(name):
The function body is
print('Hello', name)
indented below the definition.
Defining a Function

greetings.py

def greeting(name):
print('Hello', name) The function definition

# Main program The program starts running


input_name = input('Enter your name:\n') here. This is called the
main body of the program.
greeting(input_name)
Order Matters

greetings.py

def greeting(name):
The functions need to
print('Hello', name)
be defined first…

# Main program
input_name = input('Enter your name:\n')

greeting(input_name) Before they are called.


Flow Through a Program

greetings.py

def greeting(name): > python3 greetings.py


print('Hello', name) Enter your name:
Sarah

# Main program
input_name = input('Enter your name:\n') The 1st line of code that
isn’t in a function definition
greeting(input_name) is where the program starts.
Flow Through a Program

greetings.py

def greeting(name): > python3 greetings.py


print('Hello', name) Enter your name:
Sarah

# Main program
input_name = input('Enter your name:\n')

greeting(input_name) Call the greeting() function


Flow Through a Program

greetings.py

def greeting(name): Enter the function, name > python3 greetings.py


print('Hello', name) has the value of input_name Enter your name:
which is "Sarah". Sarah

# Main program
input_name = input('Enter your name:\n')

greeting(input_name)
Flow Through a Program

greetings.py

def greeting(name): > python3 greetings.py


print('Hello', name) Prints "Hello Sarah" Enter your name:
Sarah
Hello Sarah
# Main program
input_name = input('Enter your name:\n')

greeting(input_name)
Flow Through a Program

greetings.py

def greeting(name): > python3 greetings.py


print('Hello', name) Enter your name:
Sarah
Hello Sarah
# Main program
input_name = input('Enter your name:\n')

greeting(input_name)
End of the program
Scope
A variable created inside a function can only be used inside that function.
This is called local scope.

greetings.py

def greeting(name): The variable name only exists inside


print('Hello', name) this function where it was defined.

# Main program
input_name = input('Enter your name:\n')

greeting(input_name)
print('Thanks', name) The variable name doesn’t exist here, outside
of the function, so this would give us an error.
Scope
A variable created in the main body of the program is a global variable
and has global scope. That means it can be used anywhere.

greetings.py

def greeting():
The variable name is global so we can
print('Hello', name)
reference it inside this function.

# Main program
name = input('Enter your name:\n')
The variable name is global.
greeting() We don’t need a parameter for greeting()
since it can reference the global variable name.
Global Scope

greetings.py

> python3 greetings.py


def greeting():
Enter your name:
print('Hello', name)
Sarah
Hello Sarah
# Main program
name = input('Enter your name:\n')
greeting()
The program using the global name
variable works the same as before.
Global Scope
Using global variables can become messy.

greetings.py

def greeting():
print('Hello', name)
The variable name is global.
# Main program
name = input('Enter your name:\n')
greeting()
name2 = input('Enter another name:\n')
Now how do we use the greeting()
function with name2?
name = name2
greeting() We could save name2 to the name variable. But then the
value for name is gone… Let’s try local scope again.
Local Scope

greetings.py

def greeting(name):
Now we can use the greeting() function
print('Hello', name)
with any passed in value for name.

# Main program
name1 = input('Enter your name:\n')
We have two different name values and
greeting(name1)
name2 = input('Enter another name:\n') we can use the greeting() function
for both of them.
greeting(name2)
Local Scope

greetings.py

def greeting(name): > python3 greetings.py


print('Hello', name) Enter your name:
Sarah
Hello Sarah
# Main program Enter another name:
name1 = input('Enter your name:\n') Bob
greeting(name1) Hello Bob
name2 = input('Enter another name:\n')
greeting(name2)
Local scope allows us to reuse the
greeting() function with different values.
Reasons to Create a Function

You want to reuse that chunk of code over and over.

You want to organize your code by logical units.


Another Example Function
We want a simple function that adds two numbers and returns the result.

def Function 2
Keyword name parameters

def addition( a, b ):
return a + b

return
Keyword

Some functions return a value,


or even a sequence of values.
Defining Our Function

addition.py

# Creating our addition function


def addition( a, b ):
return a + b
The function definition

# Main program
num1 = float(input('Enter your 1st number:\n')) The main program starts
num2 = float(input('Enter your 2nd number:\n')) running here.
# Calling our function
result = addition(num1, num2)
print('The result is', result)
Defining Our Function

addition.py

# Creating our addition function


4 def addition( a, b ): > python3 addition.py
5 return a + b Enter your 1st number:
25
# Main program Enter your 2nd number:
1 num1 = float(input('Enter your 1st number:\n')) 37
2 num2 = float(input('Enter your 2nd number:\n')) The result is 62
# Calling our function
3 result = addition(num1, num2)
6 print('The result is', result)
7
Organizing Our Main Code into a Function

addition.py

# Creating our addition function


def addition( a, b ):
return a + b

# Main program Let’s move the whole


num1 = float(input('Enter your 1st number:\n')) main body of code to
num2 = float(input('Enter your 2nd number:\n')) its own function.

result = addition(num1, num2)


print('The result is', result)
Organizing Our Main Code into a Function

addition.py

# Creating our addition function


def addition( a, b ):
return a + b

def main():
Now all of the program
num1 = float(input('Enter your 1st number:\n')) code is contained inside
num2 = float(input('Enter your 2nd number:\n')) this main() function.

result = addition(num1, num2)


print('The result is', result)

main() We still need to call main() after the functions are declared.
Reasons to Create a Function

You want to reuse that chunk of code over and over.

You want to organize your code by logical units.

You might also like