INTRODUCTION
TO FUNCTIONS
UNIT 1
FUNCTIONS
+In Python, a function is a block of code that performs a specific
task. Functions are reusable pieces of code that allow you to
break down a program into smaller, more manageable pieces.
They take inputs, perform some operations, and return a result.
FUNCTIONS
+Here's the syntax for defining a function in Python:
+def function_name(parameter1, parameter2, ...):
+ # function code goes here
+ return result
FUNCTIONS
+The def keyword is used to define a function.
+function_name is the name of the function. It should be a descriptive
name that explains what the function does.
+parameter1, parameter2, etc. are the input parameters to the
function. You can have any number of input parameters, or none at
all.
+The function code goes inside the indented block.
+The return keyword is used to return a result from the function.
+ If the function doesn't return anything, you can omit this keyword.
+Here's an example of a function that takes two numbers as
input and returns their sum:
+def add_numbers(num1, num2):
+ sum = num1 + num2
+ return sum
+The function can be called by passing in the required
arguments:
+result = add_numbers(2, 3)
+print(result)
+ # output: 5
USER-DEFINED FUNCTIONS
+User-defined functions are functions that are defined by the
programmer to perform a specific task in a Python program.
These functions can be created and called by the programmer
as needed, and can help to make code more readable,
reusable, and modular. Here's an example of a simple user-
defined function:
USER-DEFINED FUNCTIONS
+ def say_hello(name):
+ print("Hello, " + name + "!")
+In this example, the function say_hello takes one argument
name, and prints a greeting message using the value of that
argument.
+To call this function, you simply pass in the desired argument:
+say_hello("Alice")
+This would output: Hello, Alice!
+In addition to functions, Python also allows you to import
modules, which are pre-defined collections of functions and
variables that can be used to perform more complex tasks.
+For example, the math module provides functions for
performing mathematical operations such as square roots,
trigonometric functions, and logarithms.
+To use a module in your program,
+import statement:
+import math
+# Now the functions can be used from the math module
+print(math.sqrt(4))
+# Output: 2.0
+ specific functions or variables from a module using the from keyword:
+ from math import pi
+ # Now you can use the pi constant from the math module
+ print(pi)
+ # Output: 3.141592653589793
In summary, user-defined functions and modules are both powerful tools
that can be used to organize code, improve readability, and perform
complex tasks in Python.
ou can also import specific functions or variables from a module using the from keyword:
BUILT-IN FUNCTIONS
+Python provides a large number of built-in functions that can be
used to perform common tasks such as working with strings,
numbers, lists, dictionaries, and more. These functions are pre-
defined by Python and are available for use in any Python
program. Here are a few examples of commonly used built-in
functions and how to invoke them:
+
PARAMETERS
+Parameters are inputs that are passed into a function when it is
called. They allow you to pass data into a function so that the
function can perform some operation on that data. Here is an
example of a function that takes two parameters:
+def add_numbers(num1, num2):
+ sum = num1 + num2
+ return sum
+In this example, num1 and num2 are the two parameters that
are passed into the function add_numbers. The function then
adds the two numbers together and returns the result.
The scope of variables:
+ The scope of a variable is the part of the program where the variable is
accessible. In Python, variables can have different scopes depending on
where they are defined. Here are the four types of variable scopes in
Python:
1.Local scope: Variables that are defined inside a function have local scope
and can only be accessed from within that function.
2.Global scope: Variables that are defined outside of any function have
global scope and can be accessed from anywhere in the program.
3.Enclosing scope: Variables that are defined in an enclosing function have
enclosing scope and can be accessed from within any nested functions.
4.Built-in scope: Python has a set of built-in functions and variables that are
available in any scope.
Passing parameters:
+When you call a function in Python, you can pass in one or
more parameters to the function. Here is an example of a
function that takes two parameters:
+def say_hello(name, age):
+ print("Hello, " + name + "! You are " + str(age) + " years old.")
Void functions:
+A void function is a function that does not return any value. It is
simply used to perform some operation or side effect. Here is
an example of a void function:
+def print_hello():
+ print("Hello, world!")
+In this example, the function print_hello simply prints a
greeting message to the console
Function returning values:
A function can also return a value back to the caller. This can be
useful when you need to perform some operation on data and
then use the result of that operation in another part of your
program. Here is an example of a function that returns a value:
python
def add_numbers(num1, num2):
sum = num1 + num2
return sum
+In this example, the function add_numbers takes two
parameters and returns the sum of those parameters.

FUNCTIONINPYTHON.pptx

  • 1.
  • 2.
    FUNCTIONS +In Python, afunction is a block of code that performs a specific task. Functions are reusable pieces of code that allow you to break down a program into smaller, more manageable pieces. They take inputs, perform some operations, and return a result.
  • 3.
    FUNCTIONS +Here's the syntaxfor defining a function in Python: +def function_name(parameter1, parameter2, ...): + # function code goes here + return result
  • 4.
    FUNCTIONS +The def keywordis used to define a function. +function_name is the name of the function. It should be a descriptive name that explains what the function does. +parameter1, parameter2, etc. are the input parameters to the function. You can have any number of input parameters, or none at all. +The function code goes inside the indented block. +The return keyword is used to return a result from the function. + If the function doesn't return anything, you can omit this keyword.
  • 5.
    +Here's an exampleof a function that takes two numbers as input and returns their sum: +def add_numbers(num1, num2): + sum = num1 + num2 + return sum
  • 6.
    +The function canbe called by passing in the required arguments: +result = add_numbers(2, 3) +print(result) + # output: 5
  • 7.
    USER-DEFINED FUNCTIONS +User-defined functionsare functions that are defined by the programmer to perform a specific task in a Python program. These functions can be created and called by the programmer as needed, and can help to make code more readable, reusable, and modular. Here's an example of a simple user- defined function:
  • 8.
    USER-DEFINED FUNCTIONS + defsay_hello(name): + print("Hello, " + name + "!") +In this example, the function say_hello takes one argument name, and prints a greeting message using the value of that argument. +To call this function, you simply pass in the desired argument: +say_hello("Alice") +This would output: Hello, Alice!
  • 9.
    +In addition tofunctions, Python also allows you to import modules, which are pre-defined collections of functions and variables that can be used to perform more complex tasks. +For example, the math module provides functions for performing mathematical operations such as square roots, trigonometric functions, and logarithms.
  • 10.
    +To use amodule in your program, +import statement: +import math +# Now the functions can be used from the math module +print(math.sqrt(4)) +# Output: 2.0
  • 11.
    + specific functionsor variables from a module using the from keyword: + from math import pi + # Now you can use the pi constant from the math module + print(pi) + # Output: 3.141592653589793 In summary, user-defined functions and modules are both powerful tools that can be used to organize code, improve readability, and perform complex tasks in Python. ou can also import specific functions or variables from a module using the from keyword:
  • 12.
    BUILT-IN FUNCTIONS +Python providesa large number of built-in functions that can be used to perform common tasks such as working with strings, numbers, lists, dictionaries, and more. These functions are pre- defined by Python and are available for use in any Python program. Here are a few examples of commonly used built-in functions and how to invoke them: +
  • 14.
    PARAMETERS +Parameters are inputsthat are passed into a function when it is called. They allow you to pass data into a function so that the function can perform some operation on that data. Here is an example of a function that takes two parameters:
  • 15.
    +def add_numbers(num1, num2): +sum = num1 + num2 + return sum +In this example, num1 and num2 are the two parameters that are passed into the function add_numbers. The function then adds the two numbers together and returns the result.
  • 16.
    The scope ofvariables: + The scope of a variable is the part of the program where the variable is accessible. In Python, variables can have different scopes depending on where they are defined. Here are the four types of variable scopes in Python: 1.Local scope: Variables that are defined inside a function have local scope and can only be accessed from within that function. 2.Global scope: Variables that are defined outside of any function have global scope and can be accessed from anywhere in the program. 3.Enclosing scope: Variables that are defined in an enclosing function have enclosing scope and can be accessed from within any nested functions. 4.Built-in scope: Python has a set of built-in functions and variables that are available in any scope.
  • 17.
    Passing parameters: +When youcall a function in Python, you can pass in one or more parameters to the function. Here is an example of a function that takes two parameters: +def say_hello(name, age): + print("Hello, " + name + "! You are " + str(age) + " years old.")
  • 18.
    Void functions: +A voidfunction is a function that does not return any value. It is simply used to perform some operation or side effect. Here is an example of a void function: +def print_hello(): + print("Hello, world!") +In this example, the function print_hello simply prints a greeting message to the console
  • 19.
    Function returning values: Afunction can also return a value back to the caller. This can be useful when you need to perform some operation on data and then use the result of that operation in another part of your program. Here is an example of a function that returns a value: python def add_numbers(num1, num2): sum = num1 + num2 return sum +In this example, the function add_numbers takes two parameters and returns the sum of those parameters.