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

Calling Functions

The document discusses functions in programming, describing what they are, how they are defined and used, and their benefits for organizing and reusing code. Functions are blocks of code that are given a name and can be executed by calling that name, passing in parameters. They help organize programs into logical chunks, allow code to be reused by calling the function with different parameter values, and aid in testing and modifying programs.

Uploaded by

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

Calling Functions

The document discusses functions in programming, describing what they are, how they are defined and used, and their benefits for organizing and reusing code. Functions are blocks of code that are given a name and can be executed by calling that name, passing in parameters. They help organize programs into logical chunks, allow code to be reused by calling the function with different parameter values, and aid in testing and modifying programs.

Uploaded by

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

Functions

Function: A chunk of code in a program that has a name and can


be executed by using that name.

name of the function parameters

def distance(x1, y1, x2, y2):


A function x_diff = (x1 x2)
definition y_diff = (y1 y2)
square_x = x_diff * x_diff
square_y = y_diff * y_diff

return math.sqrt(square_x + square_y)


Why use functions?
def distance(x1, y1, x2, y2):
...
1. Organize into logical chunks. Helps:
def average(...):
Test the program (unit testing) def get_points(...):
...

...
Modify the program
def maximize(...):
Write correct code ...

2. Re-use: Call the function with def distance(x1, y1, x2, y2):
different values for the x_diff = (x1 x2)
y_diff = (y1 y2)
parameters square_x = x_diff * x_diff
square_y = y_diff * y_diff
d1 = distance(8, 4, 7, 7)
d2 = distance(0, 0, 12, 5) return math.sqrt(square_x + square_y)
d3 = distance(5, 3, 9, 20)
A function call
Calling Functions
Call (aka invoke) a function def blah():
by writing its name ...
d = distance(8, 4, 7, 7)
followed by parentheses, ...

with the actual arguments Step 4 Step 1

to be sent to the function


inside those parentheses. def distance(x1, y1, x2, y2):
...
... Step 2
distance(8, 4, 7, 7) ...
Step 3
...
return math.sqrt(square_x + square_y)

Functions help organize programs and can


be re-used with different arguments to get Key idea: The 4-step process when a
many effects from a single function. function is called (as shown above).

You might also like