0
Please help stuck on functions how do they work?and how can I use it in coding
6 Answers
+ 3
Functions are blocks of code that you can "call" to execute:
//declaring the function
function funcName(){
//code
return "some functions return values, like this string";
}
//calling the function and showing the return in the console
console.log(funcName());
//console.log() is like a function!
+ 1
Cristian Gabriel Mazzulla Where are the code lines inside functions stored?
+ 1
Quoi Runtime I don't know the technical details. As I understand it, functions are just pieces of code, stored like any other piece of code, but with an address that you "go to" when you want to execute it.
The truth is that I'm not sure, I only know that you put the name of the function() and it worksđ
Oh but I'm sure NO, 'a=67' is not an attribute and 'a' may not be defined until the function is called.
0
Quoi Runtime ? I think I don't understand the questionđ
0
Cristian Gabriel Mazzulla In Python, functions are objects. So where exactly is the code block inside the function stored? Here's what I mean:
def func(foo):
a = 67
print(a % 9)
'func' is an object, so am I safe to assume that it holds the 'a = 67...' code data in an attribute of itself? Where is the code inside stored?
0
Cristian Gabriel Mazzulla Suppose this function:-
# meaningless garbage tbh lol
def foo():
print("Hello")
print(78)
True | False
So I'm assuming something like:-
foo.__code =
"""print(\"Hello\")
print(78)
True | False"""
And exec(foo.__code)