A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python/first-class-functions-python/ below:

First Class functions in Python

First Class functions in Python

Last Updated : 04 Jan, 2025

First-class function is a concept where functions are treated as first-class citizens. By treating functions as first-class citizens, Python allows you to write more abstract, reusable, and modular code. This means that functions in such languages are treated like any other variable. They can be passed as arguments to other functions, returned as values from other functions, assigned to variables and stored in data structures.

Characteristics of First-Class Functions Assigning Functions to Variables

We can assign a function to a variable and use the variable to call the function.

Example:

Python
def msg(name):
    return f"Hello, {name}!"

# Assigning the function to a variable
f = msg

# Calling the function using the variable
print(f("Anurag"))  

Explanation:

Passing Functions as Arguments

Functions can be passed as arguments to other functions, enabling higher-order functions.

Example:

Python
def msg(name):
    return f"Hello, {name}!"

def fun1(fun2, name):
    return fun2(name)

# Passing the greet function as an argument
print(fun1(msg, "Bob"))  

Explanation:

Returning Functions from Other Functions

A function can return another function, allowing for the creation of function factories.

Example:

Python
def fun1(msg):
    def fun2():
        return f"Message: {msg}"
    return fun2

# Getting the inner function
func = fun1("Hello, World!")
print(func()) 

Output
Message: Hello, World!

Explanation:

Storing Functions in Data Structures

Functions can be stored in data structures like lists or dictionaries.

Example:

Python
def add(x, y):
    return x + y

def subtract(x, y):
    return x - y

# Storing functions in a dictionary
d = {
    "add": add,
    "subtract": subtract
}

# Calling functions from the dictionary
print(d["add"](5, 3))       
print(d["subtract"](5, 3)) 

Explanation:

Suggested Quiz

8 Questions

Which of the following best describes a first-class function in programming languages?

In the context of first-class functions, what is a higher-order function?

What will be the output of the following code?

def greet(name):

return f"Hello, {name}"

say_hello = greet

print(say_hello("Geek"))

Explanation:

Functions can be assigned to variables. Here, say_hello becomes another reference to greet.

Which of the following is not a property of first-class functions?

Explanation:

First-class functions can return any type, including other functions—not limited to primitives.

What will be the output of this code?

def outer():

def inner():

return "Inner function"

return inner

func = outer()

print(func())

Explanation:

outer() returns the inner function, and func() calls it, returning its string.

Which of the following is NOT a characteristic of first-class functions?

What is the output of the following code?

def make_multiplier(n):

def multiplier(x):

return x * n

return multiplier

double = make_multiplier(2)

print(double(5))

Explanation:

make_multiplier(2) returns a function that multiplies its argument by 2. So, double(5) returns 10.

How does Python treat functions with respect to variables?

Explanation:

Being first-class citizens means functions can be assigned, passed, and returned like other objects.

Quiz Completed Successfully

Your Score :   2/8

Accuracy :  0%

Login to View Explanation

1/8 1/8 < Previous Next >

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4