🌐
W3Schools
w3schools.com › python › python_functions.asp
Python Functions
A function helps avoiding code repetition. In Python, a function is defined using the def keyword, followed by a function name and parentheses:
🌐
Python documentation
docs.python.org › 3 › library › functions.html
Built-in Functions — Python 3.14.3 documentation
3 weeks ago - If the prompt argument is present, it is written to standard output without a trailing newline. The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that. When EOF is read, EOFError is raised. Example: >>> s = input('--> ') --> Monty Python's Flying Circus >>> s "Monty Python's Flying Circus"
🌐
Programiz
programiz.com › python-programming › function
Python Functions (With Examples)
... 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: ... Dividing a complex problem into smaller chunks makes our program easy to understand and reuse.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-functions
Python Functions - GeeksforGeeks
We can define a function in Python, using the def keyword. A function might take input in the form of parameters.
Published   February 14, 2026
🌐
CodeHS
codehs.com › tutorial › ryan › basic-functions-in-python
Tutorial: Basic Functions in Python | CodeHS
Click on one of our programs below to get started coding in the sandbox! View All · Python Tutorial · python · By Calvin Studebaker · High School · javascript · By Ryan Hart · High School · Coding LMS · Online IDE · CodeHS Pro · Computer Science Curriculum ·
🌐
Stanford CS
cs.stanford.edu › people › nick › py › python-function.html
Python Functions
For today, it's sufficient that a function can take a parameter, and the parameter value is "passed in" as part of the call simply by including the desired value within the parenthesis. So for example to call the paint_window() function to paint the window blue might look like the this. ... The syntax 'blue' is a Python string, which is the way to express a bit of text like this.
🌐
Real Python
realpython.com › defining-your-own-python-function
Defining Your Own Python Function – Real Python
June 11, 2025 - Note: To learn more about built-in functions, check out Python’s Built-in Functions: A Complete Exploration. Similarly, the built-in len() function takes a data collection as an argument and returns its length: ... In this example, the list of numbers has ten values, so len() returns 10.
🌐
Tutorialspoint
tutorialspoint.com › python › python_functions.htm
Python - Functions
As variable in Python is a label or reference to the object in the memory, both the variables used as actual argument as well as formal arguments really refer to the same object in the memory. We can verify this fact by checking the id() of the passed variable before and after passing. In the following example, we are checking the id() of a variable. def testfunction(arg): print ("ID inside the function:", id(arg)) var = "Hello" print ("ID before passing:", id(var)) testfunction(var)
🌐
Simplilearn
simplilearn.com › home › resources › software development › your ultimate python tutorial for beginners › functions in python | definition, types and examples
Learn Functions in Python: Definition, Types, and Examples
June 9, 2025 - Learn about functions in Python: how they run when called, pass parameters, and return data. This guide helps you understand essential Python function concepts.
Find elsewhere
🌐
Learn Python
learnpython.org › en › Functions
Functions - Learn Python - Free Interactive Python Tutorial
Simply write the function's name followed by (), placing any required arguments within the brackets. For example, lets call the functions written above (in the previous example):
🌐
DataCamp
datacamp.com › tutorial › functions-python-tutorial
Python Functions: How to Call & Write Functions | DataCamp
November 22, 2024 - An appropriate Docstring for your hello() function is ‘Prints “Hello World”’. def hello(): """Prints "Hello World". Returns: None """ print("Hello World") return · Note that docstrings can be more prolonged than the one that is given here as an example. If you’d like to study docstrings in more detail, you best check out some Github repositories of Python libraries such as scikit-learn or pandas, where you’ll find plenty of examples!
🌐
PYnative
pynative.com › home › python › python functions
Python Functions [Complete Guide] – PYnative
January 26, 2025 - # function def even_odd(n): # check numne ris even or odd if n % 2 == 0: print('Even number') else: print('Odd Number') # calling function by its name even_odd(19) # Output Odd NumberCode language: Python (python) Run · You can take advantage of the built-in module and use the functions defined in it. For example, Python has a random module that is used for generating random numbers and data.
🌐
Codecademy
codecademy.com › learn › flask-introduction-to-python › modules › learn-python3-functions › cheatsheet
Introduction to Python: Functions Cheatsheet | Codecademy
For example, the function definition defines parameters for a character, a setting, and a skill, which are used as inputs to write the first sentence of a book. def write_a_book(character, setting, special_skill): ... Python functions can have ...
🌐
Tutorial Teacher
tutorialsteacher.com › python › python-user-defined-function
Python Functions (With Examples)
The following function takes three arguments. ... def greet(name1, name2, name3): print ('Hello ', name1, ' , ', name2 , ', and ', name3) greet('Steve', 'Bill', 'Yash') # calling function with string argument ...
Top answer
1 of 4
24

what is the purpose of having the arg1, arg2 in the parenthesis next to it?

In this case, arg1 and arg2 are called arguments. Arguments allow functions to receive inputs it's expected to use in order to perform a task. The inputs are provided by the callers.

For example, in school math, you may've already seen things like z = f(x, y) where a function named f is defined as f(x, y) = x + y. This is the same concept in a programming language.

It also allows you do write more generic, flexible, and reusable code. For example, you don't have to write many different versions of a function to accomplish the same task with slightly different results, avoiding situations like add2(x, y) = x + y and add3(x, y, z) = x + y + z, and so on. You can simply do something like:

def sum(values):  # values is of type 'list'
    result = 0
    for value in values:
        result += value
    return result

And call it like this:

total = sum([1, 2, 3, 4, 5, 6, 7]) # a list of any length with numbers

Or like this:

total = sum([1, 2])

How many arguments a function needs will depend on what it needs to do and other factors.

Update

What confuses me is the print_two_again("Steve","testing") , what is this called and its purpose?

The line print_two_again("Steve","testing") is an invocation of the function (i.e. a function call). This causes the program to 'jump' into the body of the function named print_two_again and start executing the code in it.

The ("Steve","testing") part are the arguments being sent to the function as inputs. These are positional arguments, which basically means that they get "mapped" to the names arg1 and arg2 based on the order in which you've provided them when invoking the function.

For example, consider the function f(x, y) = x - y. If this function is called as z = f(3, 4) then the argument by the name of x will receive the value 3 and y will be 4, to return -1. If you reverse the arguments in the call, then you'd have x=4 and y=3 and it'd return 1 instead. The same is true of the arguments in the function you've provided.

This means that the order of the arguments in a function call is important.

The Python language, like many others, already has a set of built-in functionality. The function named print is an example of this. You can get a lot of information using the pydoc command (pydoc3 if you use Python3, which I'd recommend). For example, the command pydoc3 print produces the following documentation:

Help on built-in function print in module builtins:

print(...) print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)

Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file:  a file-like object (stream); defaults to the current sys.stdout.
sep:   string inserted between values, default a space.
end:   string appended after the last value, default a newline.
flush: whether to forcibly flush the stream.

Note that this is documentation for Python3. Python2 documentation will be slightly different.

There's a direct correlation between your understanding of functions, as seen in your math courses in school, and functions as seen in a programming language. This is because math is part of the underlying foundation of computer science and programming languages, among others (e.g. analysis of algorithms).

2 of 4
7

what is the purpose of having the arg1, arg2 in the parenthesis next to it?

arg1 and arg2 are the names of the inputs and from there the function can use those inputs. In your case what your function does is to print them. Other functions can do other things with these arguments. But let's go step by step.

def print_two_again(arg1, arg2):
    print "arg1: %r, arg2: %r" % (arg1, arg2) 

In the first two lines starting with def, you define a function. It does something. In your case prints the two arguments it takes.

print_two_again("Steve","Testing")

On the third line what you actually do is to call that function. When you call that function you tell the function to pass "Steve" and "Testing" arguments to the function definition.

Above line is literally called a function call. Let's say you have a program and you want it to print two words. You need to define how you want it to be done. This is called function definition, where you define how things work. That's OK, but not enough. You would want to make it happen. So what you do is to execute that function. This is called a function call.

print_two_again("First","Call")
print_two_again("Second","Call")

In the above lines, what we did is to call the previously defined function two times but with different arguments.

Now let's look at the second line, which probably confuses you.

print "arg1: %r, arg2: %r" % (arg1, arg2) 

print is a built-in function in Python. What above line does here is to pass arg1 and arg2 arguments and print them with the format of "arg1: %r, arg2: %r"

🌐
Medium
jessica-miles.medium.com › writing-functions-in-python-a-beginners-guide-ed9182db959b
Writing Functions in Python— A Beginner’s Guide | by Jessica Miles | Medium
November 1, 2021 - It’s conventional in Python to use all lower-case letters for function names, and underscores to separate words. Add an empty multiline string using a set of triple quotes ("""Example"""), as a placeholder for your docstring.
🌐
freeCodeCamp
freecodecamp.org › news › functions-in-python-a-beginners-guide
Functions in Python – Explained with Code Examples
July 28, 2021 - In any programming language, functions facilitate code reusability. In simple terms, when you want to do something repeatedly, you can define that something as a function and call that function whenever you need to. In this tutorial, we shall learn ...
🌐
WsCube Tech
wscubetech.com › resources › python › functions
Functions in Python: Types, Advantages, Examples
February 10, 2026 - Learn about Python functions, their types, advantages, and examples in this tutorial. Understand how to use functions in Python to write efficient, reusable code.
🌐
Scientech Easy
scientecheasy.com › home › blog › functions in python | types, examples
Functions in Python | Types, Examples - Scientech Easy
November 25, 2022 - We do not need to import required module for them. Python comes with a variety of built-in functions. Some of the commonly used built-in functions are as: 1. abs(value): This function returns the absolute value of a given numeric value. Let’s take an example of it.