0% found this document useful (0 votes)
10 views14 pages

Chapter 3 Python Function

Chapter 4 discusses Python functions, which are blocks of code designed to perform specific tasks, and categorizes them into user-defined and standard library functions. It explains how to create and call functions, the difference between parameters and arguments, and the use of the return statement. Additionally, it highlights the availability of built-in library functions in Python that can be used directly in programs.

Uploaded by

Erdey Syoum
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views14 pages

Chapter 3 Python Function

Chapter 4 discusses Python functions, which are blocks of code designed to perform specific tasks, and categorizes them into user-defined and standard library functions. It explains how to create and call functions, the difference between parameters and arguments, and the use of the return statement. Additionally, it highlights the availability of built-in library functions in Python that can be used directly in programs.

Uploaded by

Erdey Syoum
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Chapter 4

Python Functions
Functions in Python
• A function is a block of code that performs a
specific task.
• Suppose we need to create a program to make a
circle and color it.
• We can create two functions to solve this problem:
1. function to create a circle
2. function to color the shape
• Dividing a complex problem into smaller chunks
makes our program easy to understand and reuse.
User Defined Function Vs Standard Library
Functions
• In Python, functions are divided into two categories: user-defined
functions and standard library functions. These two differ in several
ways:
User-Defined Functions
• Functions we create ourselves.
• They're like our custom tools, designed for specific tasks we have in
mind.
Standard Library Functions
• Think of these as Python's pre-packaged gifts.
• They come built-in with Python, ready to use.
• These functions cover a wide range of common tasks such as
mathematics, file operations, working with strings, etc.
Create a Function
• Let's create our first function.

Note: When writing a function,


pay attention to indentation,
which are the spaces at the start
of a code line.

Different parts of the


function greet () :
Calling a Function
• In the above example, we have declared a function
named greet().

• If we run the above code, we


won't get an output. Because
creating a function doesn't mean
we are executing the code inside
Python Function it.
Call • It means the code is there for us
to use if we want to.
• To use this function, we need to
call the function.
Example: Python • Here's how the control of the
Function Call program flows:

Here,
• When the function greet() is called, the
program's control transfers to the function
definition.
• All the code inside the function is executed.
• The control of the program jumps to the
next statement after the function call.
Python Function Arguments
• Arguments are inputs given to the function.

• Here, we passed 'John' as an


argument to the greet() function.

• We can pass different arguments


in each call, making the function
re-usable and dynamic.

• Call the function with a different


argument.
E.g. greet("David")
Example: Function to Add Two Numbers
created a function named
add_numbers() with
arguments: num1 and num2.

Python Function with Arguments


Parameters and Arguments
Parameters
• Parameters are the variables listed inside the parentheses in the
function definition.
• They act like placeholders for the data the function can
accept when we call them.
• Think of parameters as the blueprint that outlines what kind of
information the function expects to receive.

• print_age() function takes ‘’age’’


as its input. However, at this stage,
the actual value is not specified.
• The age parameter is just a
placeholder waiting for a specific
value to be provided when the
function is called.
Arguments
• Arguments are the actual values that we pass to
the function when we call it.
• Arguments replace the parameters when the function
executes.

• Here, during the function call, the


argument 25 is passed to the
function.
The return Statement
• We return a value from the function using the return
statement.
• We have created a function
named find_square().
• The function accepts a number
and returns the square of the
number.

• Note: The return statement also


denotes that the function has
ended.
• Any code after return is not
executed.
Function Argument with Default Values
• In Python, we can provide default values to function
arguments.
• We use the ‘’= ‘’ operator to provide default values.
• Example,

Hence, these values are used instead of the default


values.
Python Library Functions
• Python provides some built-in functions that can be directly
used in our program.
• We don't need to create the function, we just need to call them.
Some Python library functions are:
- print() - prints the string inside the quotation marks
- sqrt() - returns the square root of a number
-pow() - returns the power of a number
• These library functions are defined inside the module. And to
use them, we must include the module inside our program.

• Example, sqrt() is defined inside the math module.


End of CH-4!

You might also like