Def function(help)


#1
Need help for def function

I need some help with syntax of the 'def' function.

I think that I understand the basic concept of the 'def' function, but I feel like I might need somethings cleared up. I just need a basic understanding of it, some help understanding why it's necessary in python, and some help understanding why, in this exercise (Learn Python: Functions/Parameters and Arguments), you need to 'define' power. I need someone to better explain it to me because it's a little bit confusing... I'd appreciate a little help! Thank you!

To: CodeCademy Community

#2

It’s not a function, it’s a statement that creates a function and assigns a name to it


#3

Since we’re on the topic. Can you explain to me the difference between ‘functions’ and ‘statements’, since I’ve clearly mixed the two of them up?


#4

Nothing in common. Functions are callable values. A statement is an instruction to do something which will have some side effect, there’s no result of a statement itself (for-statement, if-statement, function-statement…) In contrast, an expression evaluates to a value: 1 evaluates to 1, 1+1 evaluates to 2, a function call evaluates to the value that the function returns. Expressions have results, but typically no side-effects (but some do)
For example, 1+1 doesn’t cause anything to be printed (side effect) but it has a result of 2


#5

Also, couldn’t you just do something like, base = 37 and exponent = 4 instead of def power(base, exponent) and power(37, 4). Side-note: The ‘and’ isn’t meant to be any sort of code in these examples.


#6

You might actually find it difficult to come up with a trivial function that isn’t already better expressed better in some other way in python with all the built-in ways of expressing common actions


#7

Actually I’m not even sure what you mean by “something else”
The point of functions is to abstract away grimy details.
You would for example not want to do addition one digit at a time and carry the one yourself, you’d use an add function (or operator, same thing for the most part)
And, as mentioned, they’re values, which starts blurring the line between data and code


#8

Alright, so functions are practically words and stuff you can add values to, such as x = 4, if I’m not mistaken. And what you said in your previous comment responding to my question, is basically just that the def statement is kind of a shortcut? I’m kinda just guessing here based on what I picked up.


#9

No. That doesn’t really add up at all


#10

What? The last part? I knew I was probably wrong about the last part.


#11

Any of it. With a total lack of sense for what they are, wikipedia is a good place to start


#12

Hmmm… I mean I am searching it up as you’re talking…


#13

Oh ok nvm. I found some things describing functions better.


#14

A little bit at least.


#15

Sorry! I’m a little rusty and I’ve forgotten things from the beginning of the Python lesson on CodeCademy… I got confused because on the lesson I was on we were “creating our own functions” with define so I kinda lost the definitions of all this vocab! I’ve gone back through the lessons several times already as review but I guess it’s back again to the reviews again…


#16

If you focus on what you can do and don’t even try to remember how to write the corresponding code, you’ll have far less to memorize

You’d then decide what you want to do, and look up how those actions are done.

For example, numbers support addition, subtraction etc. Loops allow you to repeat things. Lists behave pretty much like a list you write on paper. Variables hold on to values for later use.


#17

RIght[quote=“ionatan, post:2, topic:321852, full:true”]
It’s not a function, it’s a statement that creates a function and assigns a name to it
[/quote]

Right because you needs to have more detail


#18

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.