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

BSCS1350-6(1)

The document provides an introduction to programming functions, covering topics such as defining and calling void and value-returning functions, local and global variables, and passing arguments. It emphasizes the benefits of modular programming, including code reuse and easier debugging. Additionally, it discusses function parameters, including positional, keyword, and keyword-only parameters, along with their usage and rules.

Uploaded by

haifa derjan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

BSCS1350-6(1)

The document provides an introduction to programming functions, covering topics such as defining and calling void and value-returning functions, local and global variables, and passing arguments. It emphasizes the benefits of modular programming, including code reuse and easier debugging. Additionally, it discusses function parameters, including positional, keyword, and keyword-only parameters, along with their usage and rules.

Uploaded by

haifa derjan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

Introduction to Programming

Functions
(BSCS 1350)
Dr. Ghadah Alghamdi
[email protected]

1
Topics
• Introduction to Functions
• Defining and Calling a Void Function
• Designing a Program to Use Functions
• Local Variables
• Passing Arguments to Functions
• Global Variables and Global Constants
Topics (cont’d.)
• Introduction to Value-Returning Functions: Generating Random
Numbers
• Writing Your Own Value-Returning Functions
• The math Module
• Storing Functions in Modules
Introduction to Functions
• Function: group of statements within a program that perform a
specific task
• Usually one task of a large program
• Functions can be executed in order to perform overall program task
• Modularized program: program wherein each task within the program
is in its own function
Benefits of Modularizing a Program with
Functions
• The benefits of using functions include:
• Simpler code
• Code reuse
• write the code once and call it multiple times
• Better testing and debugging
• Can test and debug each function individually
• Faster development
• Easier facilitation of teamwork
• Different team members can write different functions
Void Functions and Value-Returning Functions
• A void function:
• Simply executes the statements it contains and then terminates.
• A value-returning function:
• Executes the statements it contains, and then it returns a value back to the
statement that called it.
• The input, int, and float functions are examples of value-returning functions.
Defining and Calling a Function
• Functions are given names
• Function naming rules:
• Cannot use keywords as a function name
• Cannot contain spaces
• First character must be a letter or underscore
• All other characters must be a letter, number or underscore
• Uppercase and lowercase characters are distinct
• Function names are case-sensitive
Defining and Calling a Function (cont’d.)
• Function name should be descriptive of the task carried out by the
function
• Often includes a verb. Examples: calculate_gross_pay, get_hours…
• Function definition: specifies what function does
def function_name():
statement
statement
Defining and Calling a Function (cont’d.)
• Function header: first line of function
– Includes keyword def and function name, followed by parentheses and colon
• Block: set of statements that belong together as a group
– Example: the statements included in a function
Defining and Calling a Function (cont’d.)
• Call a function to execute it
• When a function is called:
• Interpreter jumps to the function and executes statements in the block
• Interpreter jumps back to part of program that called the function
• Known as function return
Defining and Calling a Function (cont’d.)
•main function: called when the program starts
• Calls other functions when they are needed
• Defines the mainline logic of the program
Indentation in Python
• Each block must be indented
• Lines in block must begin with the same number of spaces
• Use tabs or spaces to indent lines in a block, but not both as this can confuse the Python
interpreter
• IDLE automatically indents the lines in a block
• Blank lines that appear in a block are ignored
Using the pass Keyword
• You can use the pass keyword to create empty functions
• The pass keyword is ignored by the Python interpreter
• This can be helpful when designing a program

def step1():
pass

def step2():
pass
Local Variables
• Local variable: variable that is assigned a value inside a function
• Belongs to the function in which it was created
• Only statements inside that function can access it, error will occur if another function
tries to access the variable
• Scope: the part of a program in which a variable may be accessed
• For local variable: function in which created
Local Variables (cont’d.)
• Local variable cannot be accessed by statements inside its function
which precede its creation
• Different functions may have local variables with the same name
• Each function does not see the other function’s local variables, so no
confusion
Passing Arguments to Functions
• Argument: piece of data that is sent into a function
• Function can use argument in calculations
• When calling the function, the argument is placed in parentheses following
the function name
Passing Arguments to Functions (cont’d.)
Passing Arguments to Functions (cont’d.)
• Parameter variable: variable that is assigned the value of an argument
when the function is called
• The parameter and the argument reference the same value
• General format:
• def function_name(parameter):
• Scope of a parameter: the function in which the parameter is used
Passing Arguments to Functions (cont’d.)
Passing Multiple Arguments
• Python allows writing a function that accepts multiple arguments
• Parameter list replaces single parameter
• Parameter list items separated by comma
• Arguments are passed by position to corresponding parameters
• First parameter receives value of first argument, second parameter receives
value of second argument, etc.
Passing Multiple Arguments (cont’d.)
Making Changes to Parameters
• Changes made to a parameter value within the function do not affect
the argument
• Known as pass by value
• Provides a way for unidirectional communication between one function and
another function
• Calling function can communicate with called function
Making Changes to Parameters (cont’d.)
Making Changes to Parameters (cont’d.)
• Figure 5-18
• The value variable passed to the change_me function
cannot be changed by it
Keyword Arguments
• Keyword argument: argument that specifies which parameter the
value should be passed to
• Position when calling function is irrelevant
• General Format:
• function_name(parameter=value)
• Possible to mix keyword and positional arguments when calling a
function
• Positional arguments must appear first
Keyword-Only Parameters
• In a function definition, you can require that all arguments be passed
as keyword arguments
• Write an asterisk, followed by a comma, at the beginning of the
function's parameter list
Keyword-Only Parameters
• Example:
def show_sum(*, a, b, c, d):
print(a + b + c + d)

• All the parameters appearing after the asterisk are keyword-only


parameters
• When the function is called, the keyword-only parameters will accept
only keyword arguments
Keyword-Only Parameters
• Example:
def show_sum(*, a, b, c, d):
print(a + b + c + d)

• This function call will work:


show_sum(a=10, b=20, c=30, d=40)

• But this one will not:


show_sum(10, b=20, c=30, d=40)
Keyword-Only Parameters
• The * can appear at any position in the parameter list, but only the
parameters that appear after it will become keyword-only parameters

def show_sum(a, b, *, c, d):


print(a + b + c + d)
• In this example, only the c and d parameters are keyword-only
parameters.
Positional-Only Parameters
• A positional-only parameter will accept only positional arguments
• To declare positional-only parameters, insert a forward-slash into the
parameter list
• All the parameters that appear before the forward-slash will be
positional-only parameters
Positional-Only Parameters
• Example:
def show_sum(a, b, c, d, /):
print(a + b + c + d)

• The a, b, c, and d parameters are positional-only


• These parameters will not accept keyword arguments
Positional-Only Parameters
• Example:
def show_sum(a, b, c, d, /):
print(a + b + c + d)

• This function call will work:


show_sum(10, 20, 30, 40)

• But this one will not:


show_sum(10, 20, 30, d=40)
Positional-Only Parameters
• The / can appear at any position in the parameter list, but only the
parameters that appear before it will become positional-only parameters
def show_sum(a, b, /, c, d):
print(a + b + c + d)
• In this example, only the a and b parameters are positional-only
parameters
• The c and d parameters can accept keyword arguments or positional
arguments
Positional-Only Parameters
• When you call a function in Python, you cannot pass a positional
argument after a keyword argument
def show_sum(a, b, /, c, d):
print(a + b + c + d)

• In this example, if you pass a keyword argument to c, you must also


pass a keyword argument to d
Thank you

39

You might also like