Why Functions? - Problem Solving with Python (2024)

Why Functions?

Functions are an essential part of most programming languages. Functions are reusable pieces of code that can be called using a function's name. Functions can be called anywhere in a Python program, including calling functions within other functions.

Functions provide a couple of benefits:

Every function has a function name. The function name is used when the function is called in a program. Calling a function means running a function.

Functions can receive input from the program. The input provided to a function is called input arguments or just arguments. Arguments are the code passed to a function as input.

Functions can produce output. We say a function returns output to the program. The output of a function can be assigned to a variable for use in a program.

Below is an example calling Python's pow() a function:

In[1]:

out = pow(3,2)
In the function call above, the function name is pow. pow is the power function. The pow function raises a number to a power. The input arguments are the numbers 3 and 2. The function output is assigned to the variable out. In this example, the function returns the value 9 (3 raised to the 2 power, 3^2 = 9).
Why Functions? - Problem Solving with Python (2024)
Top Articles
Latest Posts
Article information

Author: Margart Wisoky

Last Updated:

Views: 5888

Rating: 4.8 / 5 (58 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Margart Wisoky

Birthday: 1993-05-13

Address: 2113 Abernathy Knoll, New Tamerafurt, CT 66893-2169

Phone: +25815234346805

Job: Central Developer

Hobby: Machining, Pottery, Rafting, Cosplaying, Jogging, Taekwondo, Scouting

Introduction: My name is Margart Wisoky, I am a gorgeous, shiny, successful, beautiful, adventurous, excited, pleasant person who loves writing and wants to share my knowledge and understanding with you.