L06P
L06P
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)
• 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
• Different functions may have local variables with the same name
Each function does not see the other function’s local variables, so no
confusion