0% found this document useful (0 votes)
6 views2 pages

Cits 3

This document provides an overview of writing functions in Python, including how to define and invoke them. It explains the concepts of function headers, parameters, and arguments, as well as the benefits of using functions for code simplicity, reuse, and testing. Additionally, it touches on built-in functions and how to import modules to utilize functions stored in them.

Uploaded by

Sedant.Tomc
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)
6 views2 pages

Cits 3

This document provides an overview of writing functions in Python, including how to define and invoke them. It explains the concepts of function headers, parameters, and arguments, as well as the benefits of using functions for code simplicity, reuse, and testing. Additionally, it touches on built-in functions and how to import modules to utilize functions stored in them.

Uploaded by

Sedant.Tomc
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/ 2

Lec#04: Writing Code in Python How to invoke/execute a function?

To execute a function, you must call it i.e., give the name of the function
followed by a set of parentheses.
What is a function?
For example: greet()
“A function is a group of statements that exist within a program for the
purpose of performing a specific task. programmers usually break down their
programs into small manageable pieces known as functions”.

Defining a function:

“A function definition specifies the name of a new function and the sequence
of statements that execute when the function is called.”
Function name

Function
header def greet():
Function Passing information to a function
print("Hello!") definition
Block of
statements print(“Welcome!") Information that's passed to a function is called an argument;
information that's received by a function is called a parameter.
“The first line is known as the function header. It marks the beginning of the Arguments are included in parentheses after the function's name, and
function definition. The function header begins with the key word def, parameters are listed in parentheses in the function's definition.
followed by the name of the function, followed by a set of parentheses, ◦ Example: Passing a single argument
followed by a colon.” parameter

“Beginning at the next line is a set of statements known as a block. A block is


def greet_user(username):
simply a set of statements that belong together as a group. Each line in a
print("Hello, " + username + "!")
block must be indented.” Note: A function
greet_user('jesse') must be called
“A function definition specifies what a function does, but it does not cause the greet_user('diana') arguments with the correct
function to execute.” greet_user('brandon') number of
arguments.

“Starting out with Python” 4th Edition, Tony Gaddis


Can a function be called without arguments? Built-in functions in Python
If the function is defined with parameters, the arguments should be passed Python provides a number of important built-in functions that we can
when the function is called: use without needing to provide the function definition.
#function defined without #function defined with
parameter(s) parameter(s)
Such as: type(), range(), int(), float(), str(), print() and input()
def hello(): def hello(name): What is a Module?
print("Hello!") print("Hello! ", name)
A file that contains a collection of related functions and other definitions.
# call the function without # call the function with
arguments argument(s)
hello() hello("julie") How to import Modules?
“In order to call a function that is stored in a module, you have to write an
import statement at the top of your program. An import statement tells the
Why use functions? interpreter the name of the module that contains the function.”

“Programmers use functions to reduce code duplication and to help


structure and modularise programs. Once a function is defined, it may be
called multiple times from many different places in a program.” Note: No .py
Benefits of breaking down a program into functions: suffix!
o Simpler Code: A program’s code tends to be simpler and easier to ◦ When Python imports a module, it executes each statement in the
understand when it is broken down into functions. module.
o Code Reuse: Functions also reduce the duplication of code within a ◦ You can call a function in a module by typing
program moduleFileName.functionName( …)
o Better Testing: When each task within a program is contained in its own
function, testing and debugging becomes simpler.
◦ E.g., >>> cel2fah.c2f()
>>> math.sqrt(2)
“Starting out with Python” 4th Edition, Tony Gaddis

You might also like