0% found this document useful (0 votes)
4 views9 pages

L06P

The document provides an introduction to functions in Python, explaining their purpose as groups of statements that perform specific tasks, and the divide and conquer approach to programming. It distinguishes between void functions, which execute statements without returning a value, and value-returning functions, which return a value to the caller. Additionally, it covers function naming rules, the importance of indentation, and the concept of local variables and their scope within functions.

Uploaded by

mohaalhabshi515
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)
4 views9 pages

L06P

The document provides an introduction to functions in Python, explaining their purpose as groups of statements that perform specific tasks, and the divide and conquer approach to programming. It distinguishes between void functions, which execute statements without returning a value, and value-returning functions, which return a value to the caller. Additionally, it covers function naming rules, the importance of indentation, and the concept of local variables and their scope within functions.

Uploaded by

mohaalhabshi515
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/ 9

Python

Functions
Salem Belgurzi
Introduction to Functions (1 of 2)
• Function: group of statements within a program that perform as
specific task
 Usually one task of a large program
 Functions can be executed in order to perform overall program task
 Known as divide and conquer approach
Introduction to Functions (2 of 2)

Using functions to divide and conquer a large task


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 (1 of 2)
 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
Defining and Calling a Function (2 of 2)
 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
Indentation in Python
• Each block must be indented
 Lines in block must begin with the same number of spaces
 IDLE automatically indents the lines in a block
 Blank lines that appear in a block are ignored
Local Variables (1 of 2)
• 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 (2 of 2)
• 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

You might also like