Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis+ 4
How I can learn and undesrtand deeply about Functions?
I am a new one in python and i just know greeting in it. I am able to use def to write some easy and simple codes but to be honest with myself, I couldnt undesrtand it deeply. could you plz allow me to understand it better and deep? when a code is long, i get confused and dunno how functions are related together! Thanks in advance for any help
14 Respostas
+ 15
Dear Shabnam
I've been in this situation 6 years ago when I was a CS student. I was messing around with language facilities blindly without any solid understanding of what's really going on behind the scene. But soon, through math courses (helped me to understand what a function is in its abstract form) and doing a lot of homework assignments and little projects by try and error ( of course, doing a lot of dumb mistakes!), became familiar with this REALLY SIMPLE concept. It's inevitable to skip the learning curve, but hopefully, we will see your improvement inch by inch through your determination and will power.
Good luck my friend. @~)~~~~
+ 15
Hi again Shabnam
Here is a youtube channel about the basic concepts of Python.
[https://www.youtube.com/watch?v=D48iCw3WWpI&list=PL82YdDfxhWsDJTq5f0Ae7M7yGcA26wevJ]
You can download the function related part from here.
[https://www.mediafire.com/file/5ai80z9s4qufx9w/Let%27s_Learn_Python_-_Basics_%236_of_8_-_Functions.mp4]
+ 14
Don't worry dear. You don't need to be great to start something, but you need to start to be great. Very soon you'll figure out how to walk and run in programming rather than crawling. ;)
+ 5
Ł
ŁŲøŁŲ±Ł
Ų§Ų² Ł
Ų³ŲŖŁŲÆŲ§ŲŖ ŁŁ
ŁŁ help ŁŲ³ŲŖŲ“.
ŲØŲ²Ų§Ų± Ų§ŪŁŲ¬ŁŲ±Ū ŲØŚÆŁ
Ų“Ų§ŪŲÆ Ų±Ų§ŲŲŖ ŲŖŲ± ŲØŲ§Ų“Ł...Ł
Ł Ł
Ų§ŁŪŲŖŁ Ł
ŪŚÆŁ
ŲØŁ Ś©ŲÆ Ł¾Ų§ŪŲŖŁŁ Ś©Ų§Ų± ŁŲÆŲ§Ų±Ł
.
Ł
Ł ŪŚ©Ų³Ų±Ū Ś©Ų§Ų± ŲÆŲ§Ų±Ł
Ś©Ł Ł
Ł
Ś©ŁŁ ŁŪ ŲŖŚ©Ų±Ų§Ų± ŲØŲ“Ł ... Ų§ŚÆŁ ŲØŲ®ŁŲ§Ł
Ų§ŪŁ Ų±Ł ŲµŲÆŲØŲ§Ų± ŲØŁŁŪŲ³Ł
Ų“ Ų§ŁŁŲ§ Ś©ŲÆŁŲ§Ł
Ų“ŁŁŲŗ Ł
ŪŲ“Ł ŲÆŁŁ
Ų§ Ś©ŲÆŁŲ§Ł
ŁŚÆŁŲÆŲ§Ų±ŪŲ“ Ų³Ų®ŲŖ Ł
ŪŲ“Ł....
Ł
ŪŲ§Ł ŲŖŲ§ŲØŲ¹ ŲŖŲ¹Ų±ŪŁ Ł
ŪŚ©ŁŁ
ŲŖŲ§ ŲÆŪŚÆŁ Ś©ŲÆ ŲŖŚ©Ų±Ų§Ų±Ū ŁŁŁŪŲ³Ł
Ł Ų§ŚÆŁ ŁŲ±Ų§Ų± ŲŖŲ§ŲØŲ¹. Ų®Ų±ŁŲ¬Ū Ł
Ł ŲŖŲŗŪŲ± ŲÆŲ§Ų“ŲŖŁ ŲØŲ§Ų“Ł ŪŁ Ų¬Ų§ Ų±Ł ŲÆŲ³ŲŖŚ©Ų§Ų±Ū Ś©ŁŁ
ŁŁ ŲµŲÆ Ų®Ų· Ś©ŲÆ Ų±Ł
Ł
Ų«ŁŲ§. Ł
Ł ŲØŲ§ŪŲÆ 100 ŲØŲ§Ų± ŲØŲ±Ų§Ū Ų§ŁŲÆŲ§Ų²Ł ŁŲ§Ū Ł
Ų®ŲŖŁŁ Ł
Ų³ŲŖŲ·ŪŁŁŲ§Ū Ł
Ų®ŲŖŁŁ Ł
Ų³Ų§ŲŲŖ ŲŲ³Ų§ŲØ Ś©ŁŁ
rec1=x1+y1
rec2=x2+y2
rec3=....
.
.
.
rec100=x100+y100
Ų¹Ų¬ŲØ Ų§ŁŲŖŲ¶Ų§ŲŪ Ł
Ł Ł
Ų³Ų§ŲŲŖ Ł
ŪŲ®ŁŲ§Ų³ŲŖŁ
ŲŲ³Ų§ŲØ Ś©ŁŁ
ŲØŁ Ų¬Ų§Ū Ų¶Ų±ŲØŲÆŲ± ŲØŲ¹ŁŲ§ŁŁ ŚÆŲ°Ų§Ų“ŲŖŁ
....ŲŲ§ŁŲ§ ŲØŲ§ŪŲÆ ŲØŲ±Ł
Ų§ŪŁ ŲµŲÆ Ų®Ų· Ś©ŲÆ Ų±Ł Ų§ŲµŁŲ§Ų Ś©ŁŁ
.
Ų§Ł
Ų§ Ų§ŚÆŁ ŲŖŲ§ŲØŲ¹ ŲØŁŲÆ Ł
Ų«Ł Ų²ŪŲ±
function RectSurface(int x,int y)
{
return x+y
}
rec1=RectSurface(x1,y1)
rec2=RectSurface(x2,y2)
rec3=RectSurface(x3,y3)
.
.
.
rec100=RectSurface(x100,y100)
ŲÆŲ±Ų³ŲŖŁ Ś©Ł ŲØŲ§Ų² ŲŖŲ§ŲØŲ¹ Ł
Ł ŲÆŲ§Ų±Ł Ų§Ų“ŲŖŲØŲ§ŁŲ§ Ų¬Ł
Ų¹ Ł
ŪŲ²ŁŁ ŁŁŪ Ų®ŁŲØ Ł
Ł Ų§ŁŲ§Ł ŲØŲ¬Ų§Ū ŲµŲÆ Ų®Ų· Ś©ŲÆ ŁŁŲ· ŁŁ
ŁŁ Ų¬Ł
Ų¹ ŲÆŲ§Ų®Ł ŲŖŲ§ŲØŲ¹ Ų±Ł ŲØŚ©ŁŁ
Ų¶Ų±ŲØ ŲÆŲ±Ų³ŲŖ Ł
ŪŲ“Ł ...ŪŲ¹ŁŪ ŁŁŲ· ŪŁ Ų¬Ų§ Ų§ŲµŁŲ§Ų Ų“ŲÆ ŁŁ Ł
Ų«Ł ŁŲØŁŪ ŲµŲÆ Ų¬Ų§
Ł
Ų«Ų§Ł ŪŚ©Ł
Ś©ŁŚŪŚ©Ł ŚŁŁ Ų§ŁŲ§Ł Ł
ŪŚÆŪ Ų®ŁŲØ ŲŖŲ§ŲØŲ¹ ŁŁŲ“ŲŖŁŁ ŪŁ ŚŁŲ§Ų± Ł¾ŁŲ¬ Ų®Ų· ŲØŪŲ“ŲŖŲ±Ł Ų§Ų² ŲØŲÆŁŁ ŲŖŲ§ŲØŲ¹ ŁŁŲ“ŲŖŁŁ
ŁŁ Ų§ŪŁŲ¬ŁŲ±Ū ŁŪŲ³ŲŖ ŲŖŲ§ŲØŲ¹ Ł
Ł Ś©Ų§Ų± ŪŁ Ų®Ų·Ū Ł
ŪŚ©ŁŁ Ų§ŚÆŁ ŲØŲ±Ų§Ū ŁŲ± Ł
ŲŖŲŗŪŲ± ŲØŲ§ŪŲÆ. ŪŁ Ś©ŲÆ ŲØŪŲ³ŲŖ Ų®Ų·Ū Ł
ŪŁŁŲ“ŲŖŁ
ŲŖŁ ŲŲ§ŁŲŖ ŲØŲÆŁŁ ŲŖŲ§ŲØŲ¹ ŲµŲÆ ŲŖŲ§ Ł
ŲŖŲŗŪŪŲ± ŲÆŲ§Ų±Ł
Ś©Ł xŲØŪŲ³ŲŖ Ł
ŪŲ“Ł 2000 Ų®Ų· Ś©ŲÆ ŁŁŪ ŲØŲ§ ŲŖŲ§ŲØŲ¹ Ł
ŪŲ“Ł ŪŁ ŲØŪŲ³ŲŖ Ų®Ų· ŲØŲ¹ŁŲ§ŁŁ 100 Ų®Ų· ŁŲ±Ų§Ų®ŁŲ§ŁŪ ŲŖŲ§ŲØŲ¹ Ś©Ł Ł
ŪŲ“Ł 120 Ų®Ų·...
static void main(ref int a, out int b)
{
return aĆb;
}
ŲØŁ Ų§Ų³ŲŖŲ§ŲŖŪŚ© Ł
ŪŚÆŁ Ų§access modifier
Ų§Ų³Ł
ŲŖŲ§ŲØŲ¹ Ł
Ł main ŁŲ³ŲŖ
Ł¾Ų§Ų±Ų§Ł
ŲŖŲ± ŁŲ±ŁŲÆŪ a,b ŁŲ³ŲŖ
Ł¾Ų§Ų±Ų§Ł
ŲŖŲ± Ų®Ų±ŁŲ¬Ū anonymous ŁŲ³ŲŖ ŁŁŪ return Ų§ŪŁ Ś©Ų§Ų± Ų±Ł Ł
ŪŚ©ŁŁ Ł Ų®Ų±ŁŲ¬Ū Ų±Ł Ł
ŪŲÆŁ
Ł
ŁŲÆŪŁŲ§ŪŲ± ref Ų§Ų² ŁŁŲ¹ refrence value type Ł
ŪŚ©ŁŲŖŲ“ Ł out Ų®Ų±ŁŲ¬Ū ŲØŲ§ ŲŖŲŗŪŪŲ± Ł
ŪŲÆŁ
Ų§ŪŁ Ś©ŲÆ ŲØŲ§ŁŲ§ Ų³ŪŁŲŖŚ©Ų³ Ų²ŲØŲ§Ł Ų³Ū Ų“Ų§Ų±Ł¾Ł ŁŁŪ ŁŁ
Ł Ų²ŲØŲ§ŁŁŲ§ Ų§ŪŁ Ų“Ś©ŁŪŁ Ł Ł
Ų§ŁŪŲŖŲ“Ų§Ł ŪŚ©ŪŁ
ŁŲ±Ų¬Ų§Ū ŲŲ±ŁŲ§Ł
Ł Ł
ŲŖŁŲ¬Ł ŁŲ“ŲÆŪ Ś©Ų§ŁŪŁ ŲŖŁ Ų§ŪŁŲŖŲ±ŁŲŖ ŲØŚÆŲ±ŲÆŪ ...Ų®ŁŲÆŲŖŁ ŲØŁ Ł¾Ų§ŪŲŖŁŁ Ł
ŲŲÆŁŲÆ ŁŚ©Ł ..Ł
Ų§ŁŪŲŖŁ ŲÆŲ±Ś© Ś©ŁŪ ŲÆŪŚÆŁ Ų²ŲØŲ§Ł ŁŲ§Ų³ŲŖ Ų§ŁŁ
ŪŲŖ ŁŲÆŲ§Ų±Ł ŁŲ§Ų³Ł ŁŁ
ŪŁŁ Ł
ŪŚÆŁ Ų§ŚÆŁ ŪŁ Ų²ŲØŲ§Ł ŪŲ§ŲÆ ŲØŚÆŪŲ±Ū ŲØŁŪŁ Ų²ŲØŲ§ŁŁŲ§ Ų±Ų§ŲŲŖ Ł
ŪŲ“Ł ŲØŲ±Ų§ŲŖ
ŲØŲØŲ®Ų“ŪŲÆ Ų²ŪŲ§ŲÆŪ ŁŁŲ“ŲŖŁ
Ł ŲŁŲµŁŁ Ų³Ų± ŲØŲ± Ų“ŲÆ
+ 4
ŲØŁŲŖŲ±ŪŁ Ų±Ų§Ł Ų®ŁŁŲÆŁ Ł
Ų³ŲŖŁŲÆŲ§ŲŖ Ų®ŁŲÆ Ų²ŲØŲ§ŁŁ .... ŲØŁ Ų³Ų§ŲÆŁ ŲŖŲ±ŪŁ Ł Ś©Ų§Ł
ŁŲŖŲ±ŪŁ Ų·Ų±ŪŁŁ Ł
Ł
Ś©ŁŁ ŪŲ§ŲÆ Ł
ŪŲÆŁ
Ų§ŲµŁŲ§ Ł
Ų§ŁŪŲŖ ŲŖŁ
Ų§Ł
Ų²ŲØŲ§Ł ŁŲ§Ū ŲØŲ±ŁŲ§Ł
Ł ŁŁŪŲ³Ū ŪŚ©ŪŁ Ł ŲŖŁŲ±ŪŲØŲ§ ŲŖŁ
Ų§Ł
Ś©ŁŁ
Ų§ŲŖ Ś©ŁŪŲÆŪ Ų“ŁŁ ŪŚ©ŪŁ Ł
ŚÆŲ± ŲÆŪŚÆŁ Ų¬Ų²Ų” ŁŪŚŚÆŪ Ų§ŁŁ Ų²ŲØŲ§Ł ŲØŲ§Ų“Ł Ś©Ł ŲŖŁ ŲØŁŪŁ ŁŲØŲ§Ų“Ł ŁŁŪ Ų®ŁŲØ Ų®ŁŲÆ ŁŁ
ŪŁ ŁŪŚŚÆŪ ŲØŁ Ų·Ų±ŁŪ ŲÆŲ± Ų²ŲØŲ§Ł ŁŲ§Ū ŲÆŪŚÆŁ ŁŲ§ŲØŁ Ł¾ŪŲ§ŲÆŁ Ų³Ų§Ų²Ū Ų®ŁŲ§ŁŲÆ ŲØŁŲÆ....
ŲŖŁŲ§ŲØŲ¹ ŁŁ
. Ų§Ų² Ų§ŪŁ Ł
ŁŲ¶ŁŲ¹ Ł
Ų³ŲŖŲ«ŁŲ§ ŁŪŲ³ŲŖŁ ... ŪŚ©Ł
ŲØŪŲ“ŲŖŲ± Ś©Ų§Ų± Ś©ŁŪ Ś©Ų§Ł
Ł ŲÆŲ³ŲŖŲŖ Ł
ŪŲ§ŲÆ
+ 3
A function in python is just a list of instructions(code) that is encapsulated under a name you can call as many times as you want.
This is useful because there are snippets of code or things you want to do many times in a program, so you would call the appropriate function to do the given task.
Sure, you could just rewrite your code to do the same thing, but there's a saying in computer science: Never copy-paste code. Because it duplicates bugs and edits you'll have to make. It sounds like common sense, but many programmers out there forget that.
If you're still confused, post back.
+ 3
dear @sapphire thank u so much for your answer. I appreciate it. U know I know about function a little bit but i cant understand how and when to call it. I also cant relate functions to each other. So if i cant it means I didnt understand it well unfortunately.
+ 3
Ų¢ŁŲ§Ū Ų³Ų¹ŪŲÆ ŚÆŲ±Ų§Ł
Ū Ų®ŪŁŪ ŲŖŲ“Ś©Ų± Ł
ŪŚ©ŁŁ
Ų§Ų² Ų“Ł
Ų§. Ł
ŁŲøŁŲ±ŲŖŁŁ Ų§Ų² Ł
Ų³ŲŖŁŲÆŲ§ŲŖ ŚŪ ŁŲ³ŲŖŲ Ł
Ł ŚŁŲÆ Ś©ŲŖŲ§ŲØ ŚÆŲ±ŁŲŖŁ
Ų±ŁŪ ŁŲØŲ³Ų§ŪŲŖ ŁŲ§Ū Ł
Ų®ŲŖŁŁ Ś©Ł
Ł ŲØŪŲ“ Ś©Ų§Ų± Ł
ŪŚ©ŁŁ
Ł Ų§ŲµŁŲ§ Ų±ŪŲ§Ų¶Ū Ł
Ł Ų®ŁŲØ Ś©Ł ŁŪŲ³ŲŖ ŲØŲÆ ŁŁ
ŁŲ³ŲŖ. ŁŁŪ Ł
Ł ŚÆŁŪŲ§ ŲÆŲ±ŁŁŁ
ŲØŁ Ś©ŲÆ Ų¹ŁŲ§ŁŁ ŲÆŲ§Ų±Ł. Ł
ŲŖŲ§Ų³ŁŲ§ŁŁ ŁŲ³Ł
ŲŖ ŁŲ§ŁŚ©Ų“Ł Ų±Ł ŁŲŖŁŁŲ³ŲŖŁ
ŲÆŲ±Ś© Ś©ŁŁ
Ł Ų§ŲµŁŲ§ ŁŁ
ŪŲÆŁŁŁ
ŚŲ±Ų§. Ł
Ų«ŁŲ§ ŁŁŲŖŪ ŲÆŁ ŲŖŲ§ ŁŲ§ŁŚ©Ų“Ł ŲØŲ“Ł Ł
Ł ŚÆŪŲ¬ Ł
ŪŲ“Ł
Ł Ś©Ų§Ų±ŲØŲ±ŲÆŲ“ Ś©ŁŲ§ ŲØŲ±Ų§Ł
ŚÆŪŲ¬ Ś©ŁŁŲÆŁ Ł
ŪŲ“Ł. Ł
Ł Ł
ŲŖŁŲ¬Ł Ų“ŲÆŁ
Ś©Ł Ł
Ł Ų§ŲµŁŲ§ Ł
ŲŖŁŲ¬Ł ŁŲ“ŲÆŁ
! Ł ŲŲ§ŁŲ§ ŲÆŁŲØŲ§Ł ŪŚ© ŲŖŲ¹Ų±ŪŁ ŪŲ§ ŁŲ± ŚŪŲ²Ū ŁŲ³ŲŖŁ
Ś©Ł Ų§ŪŁ Ų±Ł ŲØŲ±Ų§Ł
ŁŲ§Ų¶Ų ŲØŚ©ŁŁ. ŲØŲ§Ų²Ł
Ų®ŪŁŪ Ų§Ų² Ų“Ł
Ų§ ŲŖŲ“Ś©Ų± Ł
ŪŚ©ŁŁ
Ł Ų§Ų² ŲÆŲ§Ų“ŲŖŁ ŁŲ± ŁŲøŲ±Ū Ų§Ų² Ų¬Ų§ŁŲØ Ų“Ł
Ų§ Ų®ŁŲ“ŲŲ§Ł Ł
ŪŲ“Ł
. @sayid yarmohammadi
+ 3
Ų¬ŁŲ§ŲØ Ų¢ŁŲ§Ū Ų³Ų¹ŪŲÆ ŁŲ§ŁŲ¹Ų§ Ų§Ų² ŁŁŲŖŪ Ś©Ł ŲØŲ±Ų§Ł
ŚÆŲ°Ų§Ų“ŲŖŪŲÆ ŪŚ© ŲÆŁŪŲ§ ŲŖŲ“Ś©Ų± Ł
ŪŚ©ŁŁ
ŲØŪŁŁŲ§ŪŲŖ Ų³Ł¾Ų§Ų³ŚÆŲ°Ų§Ų±Ł
. ŁŲ·Ł Ś©Ų±ŲÆŪŲÆ. ŚŲ“Ł
Ś©Ų§Ų±Ū Ś©Ł Ł
ŪŁŲ±Ł
Ų§ŪŪŲÆ Ų§ŁŲ¬Ų§Ł
Ł
ŪŲÆŁ
. Ł ŲØŲ³ŪŲ§Ų± ŲŖŁŲ¶ŪŲ Ų¬Ų§ŁŲ§ŁŁ Ų§Ū ŲØŁŲÆ.
+ 3
Ł
ŁŁŁ ŲØŲ§Ų“ŪŲÆ
+ 2
Dear Babak, thank u sooo much. It made me feeling relax coz It seems I am on the way which is not just confusing for me and how one can improve little by little. coz my main major is biology and my knowlege in math is kinda much in low level,(i can say i have no knowledge in math but i feel i am good in coding or maybe its a motivation in me for codes ...), maybe the reason I have problem in undesrtanding basics and meanings is exactly here, Math!! Thank you so much for ur message which is like a hope gift card to me. I appreciate it
+ 2
Yes I just must keep going and dont leave the way. You are true and I agree with you however the first step is always difficult. Dear Babak, thank you for your beautiful reply.
+ 2
ŁŲ§ŁŲ§ Ų®ŁŲØŁ
+ 2
awww a big suprise to me dear Babak. thank you so much for your kindness. Im ganna check the links. again and again thanks a million for your great help