What is the use of User-made functions? wouldn't it be easier to define a var? Or do I have functions mixed up? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What is the use of User-made functions? wouldn't it be easier to define a var? Or do I have functions mixed up?

16th Aug 2019, 9:55 AM
Alec Deranian
Alec Deranian - avatar
7 Answers
+ 3
here is an example with a function: def calc(val1, val2, op): print('operator is ',type(op)) if op == '+': return val1 + val2 if op == '-': return val1 - val2 res = calc(3, 5, '+') print(res) The function 'calc()' can take 3 arguments: val1 and val2 - both should be numeric values. The last var is op which gets a string variable like '+'. so a function can get arguments as int, float, string, list ... the function can store these recived objects and can use them for processing. when function will be terminated the variables are deleted. same happens to variables of the main programm. if you mean 'store' in terms of using values for a future use they has to be stored in file and can then be read and used again.
16th Aug 2019, 12:07 PM
Lothar
Lothar - avatar
0
Your question is unclear
16th Aug 2019, 9:58 AM
Aymane Boukrouh
Aymane Boukrouh - avatar
0
can you explain a function?
16th Aug 2019, 10:00 AM
Alec Deranian
Alec Deranian - avatar
0
A function is a piece of code which you can reuse over and over, a variable stores a value.
16th Aug 2019, 10:17 AM
Paul Grasser
Paul Grasser - avatar
0
so the value can be a string?
16th Aug 2019, 10:22 AM
Alec Deranian
Alec Deranian - avatar
0
Of a variable yes, not of a function.
16th Aug 2019, 10:23 AM
Paul Grasser
Paul Grasser - avatar
0
so a function can't store a string?
16th Aug 2019, 10:25 AM
Alec Deranian
Alec Deranian - avatar