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

Functions in Python-Week 5

The document provides an overview of functions in Python, emphasizing the importance of breaking down large programs into manageable components. It covers topics such as defining and calling functions, built-in functions, default values for arguments, keyword arguments, and the concept of functions as first-class objects. Additionally, it notes that Python does not support function overloading and explains the significance of the None value in functions without explicit return statements.

Uploaded by

Quadri Alayo
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 in Python-Week 5

The document provides an overview of functions in Python, emphasizing the importance of breaking down large programs into manageable components. It covers topics such as defining and calling functions, built-in functions, default values for arguments, keyword arguments, and the concept of functions as first-class objects. Additionally, it notes that Python does not support function overloading and explains the significance of the None value in functions without explicit return statements.

Uploaded by

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

Functions in Python

CSC 301
Introduction

• Most computer programs that solve real-world problems are larger


than the programs presented in the previous examples.
• Experience has shown that the best way to develop and maintain
a large program is to construct it from smaller pieces or
components, each of which is more manageable than the original
program.
• This technique is called divide and conquer.

CSC 301
Program Components in Python

• Python programs are written by combining programmer-defined


(programmer-created) functions and classes with functions or
classes already available in existing Python modules.
• A module is a file that contains definitions of functions and
classes.
• Many modules can be grouped together into a collection, called a
package

CSC 301
Functions

• A function is a named block of code that performs a specific task


• Programmers can define functions to perform specific tasks that
execute at various points in a program. These functions are
referred to as programmer-defined functions.
• We have been using functions in Python since the first class.
• These functions include print, input, int, float, str, and type.
• The Python standard library includes many other functions useful
for common programming tasks.
CSC 301
Module math Functions

• The math module contains functions that allow programmers to


perform certain mathematical calculations

CSC 301
CSC 301
The Built-in Functions

• We have been using functions in Python since the first class.


• These functions include print, input, int, float, str, and type.
• These functions and many others reside in a module named
__builtins__.
• The __builtins__ module is special because its components are
automatically available to any Python program with—no import
statement is required.

CSC 301
Defining Functions
Function definition begins with “def.” Function name and its arguments.

def get_final_answer(filename):
“““Documentation String”””
line1
line2 Colon.
return total_counter

The indentation matters…


First line with less
indentation is considered to be The keyword ‘return’ indicates the
outside of the function definition. value to be sent back to the caller.

No header file or declaration of types of function


Calling a Function
• The syntax for a function call is:
>>> def myfun(x, y):
return x * y
>>> myfun(3, 4)
12
• Parameters in Python are Call by Assignment
– Old values for the variables that are parameter names are
hidden, and these variables are simply made to refer to the
new values
– All assignment in Python, including binding function
parameters, uses reference semantics.
CSC 301
CSC 301
Functions without returns

• All functions in Python have a return value, even if


no return line inside the code
• Functions without a return return the special value
None
– None is a special constant in the language
– None is used like NULL, void, or nil in other
languages
– None is also logically equivalent to False
– The interpreter’s REPL doesn’t print None
Function overloading? No.

• There is no function overloading in Python


– Unlike C++, a Python function is specified
by its name alone
The number, order, names, or types of arguments
cannot be used to distinguish between two functions
with the same name
– Two different functions can’t have the same
name, even if they have different arguments
• But: see operator overloading in later slides
(Note: van Rossum playing with function overloading for the future)
Default Values for Arguments
• You can provide default values for a function’s arguments
• These arguments are optional when the function is called

>>> def myfun(b, c=3, d=“hello”):


return b + c
>>> myfun(5,3,”hello”)
>>> myfun(5,3)
>>> myfun(5)

All of the above function calls return 8


Keyword Arguments
• Can call a function with some/all of its arguments out
of order as long as you specify their names
>>> def foo(x,y,z): return(2*x,4*y,8*z)
>>> foo(2,3,4)
(4, 12, 32)
>>> foo(z=4, y=2, x=3)
(6, 8, 32)
>>> foo(-2, z=-4, y=-3)
(-4, -12, -32)
• Can be combined with defaults, too
>>> def foo(x=1,y=2,z=3):
return(2*x,4*y,8*z)
>>> foo()
(2, 8, 24)
>>> foo(z=100)
(2, 8, 800)
Functions are first-class objects
Functions can be used as any other datatype,
eg:
– Arguments to function
– Return values of functions
– Assigned to variables
– Parts of tuples, lists, etc
>>> def square(x): return x*x
>>> def applier(q, x): return
q(x)
>>> applier(square, 7)
49

You might also like