+ 41

What exactly is the point of assigning a function to a variable?

1st Mar 2016, 2:18 AM
Manta Ryugami
Manta Ryugami - avatar
15 Respostas
+ 80
Assigning a function to a variable is a very powerful technique in functional programming. You could have an algorithm which works with "a function" but if you are using a variable to call that function, you have the flexibility of assigning a different function at runtime. It is something similar to polymorphism.
23rd May 2016, 10:30 AM
James Flanders
+ 40
Another advantage is maintainability of the code. For example if function is called at multiple places and at later point of time if you want to change the name of the function then you have to change the name at all places. If function was assigned with variable then you have to change the name at only one place. I.e where ever assignment has done.
7th Mar 2018, 6:23 PM
Santhosh Jain
Santhosh Jain - avatar
+ 21
@Aditya, take a look on this example given in the lessons: def do_twice(func, x, y): return func(func(x, y), func(x, y)) By assigning a function to a variable - in this case a reference - you can create a general function which is able to perform a task depending on what function you pass as argument. It is a good tool for a DRY code, since you'll be able to reusing some functions, just like "do_twice" from above example, that otherwise you would need to do many times.
31st Dec 2016, 10:17 PM
Daniel Pompermayer
Daniel Pompermayer - avatar
+ 4
just take a simple example : *Every person has 4 apple and 5 banana. *total no. of persons is a user input (i am hardcoding it as 10 for simplicity) totalPerson = 10; def calculateApple(p): return p*4; def calculateBanana(p): return p*5; allApple = calculateapple(TotalPerson); allBanana = calculateBanana(TotalPerson); totalFruits = allApple + allBanana; So in this example, I have assigned calculateApple() return value to allApple variable. The function will use the value of total persons to calculate total no. of apples and then it will return the result. That returned value will be stored in allApple variable.
24th Jan 2017, 2:09 AM
Arman Avasthi
Arman Avasthi - avatar
+ 4
Es un poco complicado de entender sería mejor en una PC e ir paso a paso con el intérprete y ver cómo se va ejercitando para poder entender mejor
8th Apr 2021, 3:22 PM
Rodolfo López Aquino
Rodolfo López Aquino - avatar
+ 3
Assigning a function to a variable is so important because to reduce code redundancy, faster to program than to create or declare variable again and again . To reduce code
19th Mar 2021, 7:59 AM
Nar Bdr Kharka
Nar Bdr Kharka - avatar
+ 2
By assigning the function to a variable helps us to be organize and makes an easy access to the function again and again, we dont need to write down the same line of codes everytime we need it.
8th Jun 2021, 5:57 AM
Nimrod Trance
Nimrod Trance - avatar
+ 1
Пишите на русском пожалуйста
9th Jul 2021, 8:02 AM
david danilov
david danilov - avatar
+ 1
It is very important to avoid duplication of codes. If you assign a function to a particular variable, you can call it again elsewhere instead of rewriting the whole function.
6th Sep 2021, 9:42 PM
Tony Amusan
Tony Amusan - avatar
0
@James Can you please elaborate ?
19th Nov 2016, 3:35 PM
Aditya Vardhan Singh
Aditya Vardhan Singh - avatar
- 1
Assigning a function to a variable is a very powerful technique in functional programming. You could have an algorithm which works with "a function" but if you are using a variable to call that function, you have the flexibility of assigning a different function at runtime. It is something similar to polymorphism.
8th May 2021, 3:26 PM
Hemant Gupta
Hemant Gupta - avatar
- 1
It's useful for closures
17th Sep 2021, 9:22 AM
Michael McLean
Michael McLean - avatar
- 2
the type of variable like a function so we can assign function to var
29th Mar 2021, 4:44 PM
Youssef Labied
Youssef Labied - avatar
- 3
Assigning a function to a variable is a very powerful technique in functional programming. You could have an algorithm which works with "a function" but if you are using a variable to call that function, you have the flexibility of assigning a different function at runtime. It is something similar to polymorphism.
24th Jul 2021, 4:57 AM
Vaibhav Nehe
Vaibhav Nehe - avatar
- 5
Another advantage is maintainability of the code. For example if function is called at multiple places and at later point of time if you want to change the name of the function then you have to change the name at all places. If function was assigned with variable then you have to change the name at only one place. I.e where ever assignment has dpne.
7th Mar 2018, 6:22 PM
Santhosh Jain
Santhosh Jain - avatar