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 thatfunctionsin 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:
Pythondefmsg(name):returnf"Hello,{name}!"# Assigning the function to a variablef=msg# Calling the function using the variableprint(f("Anurag"))
Explanation:
- function greet is assigned to the variable f.
- f is then used to call the function, demonstrating that functions can be treated like any other variable.
Passing Functions as Arguments
Functions can be passed as arguments to other functions, enablinghigher-order functions.
Example:
Pythondefmsg(name):returnf"Hello,{name}!"deffun1(fun2,name):returnfun2(name)# Passing the greet function as an argumentprint(fun1(msg,"Bob"))
Explanation:
- The fun1 function takes another function fun2 and a name as arguments.
- The msg function is passed to fun1, which then calls greet with the given name.
Returning Functions from Other Functions
A function can return another function, allowing for the creation of function factories.
Example:
Pythondeffun1(msg):deffun2():returnf"Message:{msg}"returnfun2# Getting the inner functionfunc=fun1("Hello, World!")print(func())
OutputMessage: Hello, World!
Explanation:
- The fun1 defines an fun2 and returns it.
- func stores the returned fun2, which can then be called later.
Storing Functions in Data Structures
Functions can be stored in data structures likelists ordictionaries.
Example:
Pythondefadd(x,y):returnx+ydefsubtract(x,y):returnx-y# Storing functions in a dictionaryd={"add":add,"subtract":subtract}# Calling functions from the dictionaryprint(d["add"](5,3))print(d["subtract"](5,3))
Explanation:
- Functions add and subtract are stored in a dictionary operations.
- Functions are accessed and called from the dictionary using their respective keys.
Suggested Quiz
8 Questions
Which of the following best describes a first-class function in programming languages?
A function that can only be called from within its own scope.
A function that can be assigned to variables, passed as arguments, and returned from other functions like any other object.
A function that can only be defined at the top level of a module.
A function that can only be executed in a specific context.
In the context of first-class functions, what is a higher-order function?
A function that can only return primitive data types.
A function that can take other functions as arguments or return them as results.
A function that is defined within another function but cannot be returned.
A function that executes in a separate thread.
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?
Functions can be stored in data structures
Functions can be assigned to variables
Functions can return other functions
Functions can only return primitive types
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?
They can be assigned to variables.
They can be passed as arguments to other functions.
They can only be defined once in a program.
They can be returned from other 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?
Functions are static objects
Functions are constants and cannot be reassigned
Functions are first-class citizens and can be stored, passed, and reassigned
Functions must be declared global to be used
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/81/8< Previous Next >