0% found this document useful (0 votes)
45 views3 pages

Chapter - 3 - Working With Functions

The document discusses functions in Python. It defines functions as pieces of code that carry out specific tasks and may or may not take inputs or return outputs. There are three types of functions: built-in, user-defined, and anonymous (lambda). Functions are defined using the def keyword followed by the function name and parameters. Parameters refer to the names used in the function definition while arguments refer to the actual inputs passed. Functions may return values using the return statement. Composition involves combining functions so the output of one is the input of the next. Variables inside a function have local scope while those outside have global scope.

Uploaded by

Shaurya Garg
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)
45 views3 pages

Chapter - 3 - Working With Functions

The document discusses functions in Python. It defines functions as pieces of code that carry out specific tasks and may or may not take inputs or return outputs. There are three types of functions: built-in, user-defined, and anonymous (lambda). Functions are defined using the def keyword followed by the function name and parameters. Parameters refer to the names used in the function definition while arguments refer to the actual inputs passed. Functions may return values using the return statement. Composition involves combining functions so the output of one is the input of the next. Variables inside a function have local scope while those outside have global scope.

Uploaded by

Shaurya Garg
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/ 3

CHAPTER – 3 – WORKING WITH FUNCTIONS

1. Introduction – That means that a function is a piece of code written to carry out a specified task. To carry out that

specific task, the function might or might not need multiple inputs. When the task is carried out, the function can or

cannot return one or more values.

There are three types of functions in Python:

 Built-in functions - such as min() to get the minimum value, print() to print an object to the terminal.

 User-Defined Functions - which are functions that users create to help them out.

 Anonymous functions - which are also called lambda functions because they are not declared with the

standard def keyword.

Functions vs Methods - A method refers to a function which is part of a class. You access it with an instance or

object of the class. A function doesn’t have this restriction: it just refers to a standalone function. This means that all

methods are functions, but not all functions are methods.

2. Defining Function is Python – The four steps to defining a function in Python are the following:

a. Use the keyword def to declare the function and follow this up with the function name.

b. Add parameters to the function: they should be within the parentheses of the function. End your line with a

colon.

c. Add statements that the functions should execute.

d. End your function with a return statement if the function should output something. Without the return

statement, your function will return an object none.

3. Parameters vs Argument – Parameters are the names used when defining a function or a method, and into which

arguments will be mapped. In other words, arguments are the things which are supplied to any function or method

call, while the function or method code refers to the arguments by their parameter names.

4. Returning values from functions – Refer to below given example of User defined function:

Note that as you’re printing something in your hello() functions, you don’t really need to return it.
However, if you want to continue to work with the result of your function and try out some operations on it, you will

need to use the return statement to actually return a value, such as a String, an integer.

Consider the following scenario, where hello() returns a String "hello", while the function hello_noreturn() returns

None, For Example –

Note that hello_noreturn() function does not have return statement it will return an object with value None.

Tip functions immediately exit when they come across a return statement.

5. Compositions – Function composition is the way of combining two or more functions in such a way that the output

of one function becomes the input of the second function and so on.

For Example -

First the add() function is called on input 5. The add() adds 2 to the input and the output which is 7, is given as the

input to multiply() which multiplies it by 2 and the output is 14.


6. Scope of Variables – In general, variables that are defined inside a function body have a local scope, and those

defined outside have a global scope. That means that local variables are defined within a function block and can only

be accessed inside that function, while global variables can be obtained by all functions that might be in your script.

7. Python Simple Function Examples –

a. Function without return statement and default parameter – The following example shows how to use a

default parameter value. If we call the function without argument, it uses the default value:

Output –

b. Function with return statement –

Output –

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy