SlideShare a Scribd company logo
FUNCTION RETURNS
THE RETURN STATEMENT• Used to end execution of a function
• Can be placed anywhere in a function
• Statements that follow the return statement will not be executed
• Can be used to prevent abnormal termination of program
• A function can return a value or expression back to the statement that called the function.
• You've already seen the randint function, which returns a value:
x = random.random()
• Also many math functions return a value:
• exp(x,y);
• sqrt(x);
• sin(x);, etc.
• In a value-returning function, the return statement can be used to return a value from function
to the point of call.
• assign it to a variable
• send it to print
• use it in an expression
FUNCTION WITH RETURN EXAMPLE
VARIABLE SCOPE: LOCAL AND GLOBAL
• Variables defined inside a function are local to that
function. They are hidden from the statements in other
functions, which normally cannot access them. When
the function terminates the values are released.
• Because the variables defined in a function are hidden,
other functions may have separate, distinct variables
with the same name.
• Question: What does the above statement mean?
LOCAL VARIABLES
• A function’s local variables exist only while the
function is executing. This is known as the lifetime of
a local variable.
• When the function begins, its local variables and its
parameter variables are created in memory, and
when the function ends, the local variables and
parameter variables are destroyed.
• This means that any value stored in a local variable is
lost between calls to the function in which the
variable is declared.
GLOBAL VARIABLES AND GLOBAL CONSTANTS
• A global variable is any variable defined outside all the
functions in a program.
• The scope of a global variable is the portion of the program
from the variable definition to the end.
• This means that a global variable can be accessed by all
functions that are defined after the global variable is
defined.
Bad Programming: The use of global variables
Good Programming: Only creating global variables that are
constant
SCOPE EXAMPLE

More Related Content

PPTX
Functions
PPTX
Function Parameters
PPS
Introduction to php 5
PPTX
Intro to Functions
PPT
PPTX
User defined functions.1
PPTX
Functions in c language1
PPTX
Variable scope in php
Functions
Function Parameters
Introduction to php 5
Intro to Functions
User defined functions.1
Functions in c language1
Variable scope in php

What's hot (20)

PPTX
Java script function
PDF
Notes: Verilog Part 5 - Tasks and Functions
PPTX
Cold fusion best practice
PDF
Functions and tasks in verilog
PPTX
Inline function in C++
PPTX
Command Line Arguments in C#
PDF
Functional Programming for OO Programmers (part 1)
PPT
Savitch ch 05
PPTX
Function
PPTX
Inline functions & macros
PPTX
Function & procedure
PPTX
Global variables, sorting static variables,function and arrays,
PPT
Command line arguments.21
PPTX
C++ Functions
PDF
Functions in php
DOCX
Functions assignment
PPT
Savitch Ch 05
PDF
Command line-arguments-in-java-tutorial
DOCX
Inline function(oops)
Java script function
Notes: Verilog Part 5 - Tasks and Functions
Cold fusion best practice
Functions and tasks in verilog
Inline function in C++
Command Line Arguments in C#
Functional Programming for OO Programmers (part 1)
Savitch ch 05
Function
Inline functions & macros
Function & procedure
Global variables, sorting static variables,function and arrays,
Command line arguments.21
C++ Functions
Functions in php
Functions assignment
Savitch Ch 05
Command line-arguments-in-java-tutorial
Inline function(oops)
Ad

Viewers also liked (6)

PPT
Intro to Lists
PPT
Formatting Output
PPT
Intro to Jeroo Python
PPT
Intro to Functions Python
PPTX
For Loops and Nesting in Python
PPT
Conditional Loops Python
Intro to Lists
Formatting Output
Intro to Jeroo Python
Intro to Functions Python
For Loops and Nesting in Python
Conditional Loops Python
Ad

Similar to Function Returns (20)

PPTX
Variable Scope
PDF
Starting Out With C++ From Control Structures To Objects 9th Edition Gaddis S...
PDF
Starting Out With C++ From Control Structures To Objects 9th Edition Gaddis S...
PDF
Starting Out With C++ From Control Structures To Objects 9th Edition Gaddis S...
PDF
Starting Out With C++ From Control Structures To Objects 9th Edition Gaddis S...
PDF
Starting Out With C++ From Control Structures To Objects 9th Edition Gaddis S...
PDF
Starting Out With C++ From Control Structures To Objects 9th Edition Gaddis S...
PPTX
Lecture 5 Functions all about business categories
PPTX
Function
PDF
JavaScript - Chapter 6 - Basic Functions
PPTX
04. WORKING WITH FUNCTIONS-2 (1).pptx
PDF
PPTX
Variable Scopes
PPTX
GlobalLocal_ExceptionHndlng_InPython.pptx
PPTX
FUNCTION CPU
PPTX
Functions
PDF
Functions in Python.pdfnsjiwshkwijjahuwjwjw
PPT
JavaScript Introductin to Functions
PPTX
LOCAL VARIABLES AND GLOBAL VARIABLES.pptx
Variable Scope
Starting Out With C++ From Control Structures To Objects 9th Edition Gaddis S...
Starting Out With C++ From Control Structures To Objects 9th Edition Gaddis S...
Starting Out With C++ From Control Structures To Objects 9th Edition Gaddis S...
Starting Out With C++ From Control Structures To Objects 9th Edition Gaddis S...
Starting Out With C++ From Control Structures To Objects 9th Edition Gaddis S...
Starting Out With C++ From Control Structures To Objects 9th Edition Gaddis S...
Lecture 5 Functions all about business categories
Function
JavaScript - Chapter 6 - Basic Functions
04. WORKING WITH FUNCTIONS-2 (1).pptx
Variable Scopes
GlobalLocal_ExceptionHndlng_InPython.pptx
FUNCTION CPU
Functions
Functions in Python.pdfnsjiwshkwijjahuwjwjw
JavaScript Introductin to Functions
LOCAL VARIABLES AND GLOBAL VARIABLES.pptx

More from primeteacher32 (20)

PPT
Software Development Life Cycle
PPTX
Returning Data
PPTX
Introduction to GUIs with guizero
PPTX
Function Parameters
PPTX
Nested Loops
PPT
Conditional Loops
PPTX
Introduction to Repetition Structures
PPTX
Input Validation
PPTX
Windows File Systems
PPTX
Nesting Conditionals
PPTX
Conditionals
PPT
Intro to Python with GPIO
PPTX
Variables and Statements
PPTX
Variables and User Input
PPT
Intro to Python
PPTX
Raspberry Pi
PPT
Hardware vs. Software Presentations
PPTX
Block chain security
PPTX
PPTX
System Administration
Software Development Life Cycle
Returning Data
Introduction to GUIs with guizero
Function Parameters
Nested Loops
Conditional Loops
Introduction to Repetition Structures
Input Validation
Windows File Systems
Nesting Conditionals
Conditionals
Intro to Python with GPIO
Variables and Statements
Variables and User Input
Intro to Python
Raspberry Pi
Hardware vs. Software Presentations
Block chain security
System Administration

Recently uploaded (20)

PDF
313302 DBMS UNIT 1 PPT for diploma Computer Eng Unit 2
PPT
APPROACH TO DEVELOPMENTALlllllllllllllllll
PPTX
Sports and Dance -lesson 3 powerpoint presentation
PDF
Manager Resume for R, CL & Applying Online.pdf
DOCX
How to Become a Criminal Profiler or Behavioural Analyst.docx
PDF
Villa Thesis-Final.pdf NNNNNNNNNNNNNNNNNNNNNNNNNNNNN
PPTX
arif og 2.pptx defence mechanism of gingiva
PPTX
Surgical thesis protocol formation ppt.pptx
PPTX
OCCULAR MANIFESTATIONS IN LEPROSY.pptx bbb
PPTX
PMP (Project Management Professional) course prepares individuals
PPTX
Untitled presentation voice baed jounral
PPTX
Autonomic_Nervous_SystemM_Drugs_PPT.pptx
PPTX
OnePlus 13R – ⚡ All-Rounder King Performance: Snapdragon 8 Gen 3 – same as iQ...
PPTX
Understanding Present Worth for a better future
PPTX
Cerebral_Palsy_Detailed_Presentation.pptx
PPTX
jinsha and arif-2.pptx blood factors and diseases
PDF
Josh Gao Strength to Strength Book Summary
PDF
HR Jobs in Jaipur: 2025 Trends, Banking Careers & Smart Hiring Tools
PPTX
engineeringAlumni Meet 2025 -Principal.pptx
PDF
esg-supply-chain-webinar-nov2018hkhkkh.pdf
313302 DBMS UNIT 1 PPT for diploma Computer Eng Unit 2
APPROACH TO DEVELOPMENTALlllllllllllllllll
Sports and Dance -lesson 3 powerpoint presentation
Manager Resume for R, CL & Applying Online.pdf
How to Become a Criminal Profiler or Behavioural Analyst.docx
Villa Thesis-Final.pdf NNNNNNNNNNNNNNNNNNNNNNNNNNNNN
arif og 2.pptx defence mechanism of gingiva
Surgical thesis protocol formation ppt.pptx
OCCULAR MANIFESTATIONS IN LEPROSY.pptx bbb
PMP (Project Management Professional) course prepares individuals
Untitled presentation voice baed jounral
Autonomic_Nervous_SystemM_Drugs_PPT.pptx
OnePlus 13R – ⚡ All-Rounder King Performance: Snapdragon 8 Gen 3 – same as iQ...
Understanding Present Worth for a better future
Cerebral_Palsy_Detailed_Presentation.pptx
jinsha and arif-2.pptx blood factors and diseases
Josh Gao Strength to Strength Book Summary
HR Jobs in Jaipur: 2025 Trends, Banking Careers & Smart Hiring Tools
engineeringAlumni Meet 2025 -Principal.pptx
esg-supply-chain-webinar-nov2018hkhkkh.pdf

Function Returns

  • 2. THE RETURN STATEMENT• Used to end execution of a function • Can be placed anywhere in a function • Statements that follow the return statement will not be executed • Can be used to prevent abnormal termination of program • A function can return a value or expression back to the statement that called the function. • You've already seen the randint function, which returns a value: x = random.random() • Also many math functions return a value: • exp(x,y); • sqrt(x); • sin(x);, etc. • In a value-returning function, the return statement can be used to return a value from function to the point of call. • assign it to a variable • send it to print • use it in an expression
  • 4. VARIABLE SCOPE: LOCAL AND GLOBAL • Variables defined inside a function are local to that function. They are hidden from the statements in other functions, which normally cannot access them. When the function terminates the values are released. • Because the variables defined in a function are hidden, other functions may have separate, distinct variables with the same name. • Question: What does the above statement mean?
  • 5. LOCAL VARIABLES • A function’s local variables exist only while the function is executing. This is known as the lifetime of a local variable. • When the function begins, its local variables and its parameter variables are created in memory, and when the function ends, the local variables and parameter variables are destroyed. • This means that any value stored in a local variable is lost between calls to the function in which the variable is declared.
  • 6. GLOBAL VARIABLES AND GLOBAL CONSTANTS • A global variable is any variable defined outside all the functions in a program. • The scope of a global variable is the portion of the program from the variable definition to the end. • This means that a global variable can be accessed by all functions that are defined after the global variable is defined. Bad Programming: The use of global variables Good Programming: Only creating global variables that are constant