Function Basics

A place where you can post Python-related tutorials you made yourself, or links to tutorials made by others.

Function Basics

Postby metulburr » Fri Feb 22, 2013 7:47 am

Why use functions?
  • They maximize code reuse and minimize redunancy
      Package logic when using in more than one place and more than one time
      Reduce maintenance. For example, if you need to fix something, it gets fixed once in the function and not the 10+ times the function is called
  • Procedural decomposition
      Split systems into pieces that have well-defined roles



Function design concepts
  • coupling: use arguments for inputs and return for outputs
  • global variables are a poor way for functions to communicate
  • don't change mutable arguments unless the caller expects it, avoid specific function
  • each function should do ONE thing.



Coding functions
They are called in expressions, are passed values, and return results.

  • def
      def is executable code. Your function does not exist until Python reaches and runs def.
      def creates a function object and assigns it to a name
  • lambda
      lambda create an object but returns it as a result. It allows in-line function definitions. It is an expression not a statement, un-like def
  • return
      return sends a result object back to the caller. When a function is called, the caller stops until the function finishes its work and returns control to the caller. Functions that compute a value send it back to the caller with a return statement. The returned value becomes the the result of the function call.
      The return statement can show up anywhere in the function's body, it ends the function call and sends a result back to the caller
  • yield
      yield sends a result object back to the caller, but remembers where it left off. Functions known as generators, also use the yield statement to send back a value and suspend their state, to be resumed later, to produce a series of results over time
  • global
      global declares module-level variables that are to be assigned.
  • nonlocal
      nonlocal declares enclosing function variables that are to be assigned
  • Arguments
      Arguements are passed by assignment (object reference).


Function general format
Code: Select all
def add(arg1, arg2):
   value = arg1 + arg2
   return value

So this is a small basic example of a function. It does one thing, adds its arguments. Executing this code snippet will do nothing. This function needs to be called to execute. Let's dissect this definition a little bit more. The def is a keyword, which starts the creation of a function object and it assigns it the name add. This name is made by you. The arguments names in the header are assigned to the objects passed in parenthesis at the point of call. The two indented lines are the functions body. This gets executed every time the function is called.

The variable value is a local variable only known in the function call(). The last line in the body of the function, returns the value of the local variable to the caller.
Code: Select all
val = add(5,2)
print(val)

print(add(20,20))

This is the call of the function. The first calls the function and assigns the value to the variable val. Then is prints that variable. The second prints the result of the fiunction call directly.
So after making this call:
Code: Select all
val = add(5,2)

you are essentially doing:
Code: Select all
val = 7

because 7 was returned to the caller. So the section of code add(5,7) gets "replaced" with the value 7.




Functions without return statements
When the return statment is non-existant, the function exits when the control flow falls off the end of the function body. Every function returns something. If you do not have a return statement, the function returns the value None automatically.
Code: Select all
def add(arg1, arg2):
   value = arg1 + arg2
   print(value)
   
print(add(3,3))

This snippet of code will print the results:
Code: Select all
6
None

The 6 is printed because of the print inside the function's body. The None is printed because we are printing the functions return value. Since we did not return anything, it returns None.



def executes at runtime
def is an executable statement. Because of that, it can appear anywhere a statment can, even nested in other statements. An example would be:
Code: Select all
if test:
   def func():
      ...
else:
   def func():
      ...
      
func()

So based on the if condition, func() can be defined differently.

Because definition happens at runtime, there is nothing special about the function name. What is important is the object to which it refers:
Code: Select all
def func(func_name):
   print('in body of function {}'.format(func_name))

func('func')

othername = func
othername('othername')

Code: Select all
in body of function func
in body of function othername




Recursive functions
Functions that call themselves either directly or indirectly in order to loop is recursion. This is why we have loops. Use them. Many unexpected results in rescursion is due to the fact of the person not knowing how to use recursion.
Code: Select all
def recursive(string, num=0):
   print('{} {}'.format(string, num))
   recursive(string, num + 1)

recursive('test')


Code: Select all
test 0
test 1
test 2
test 3
test 4
test 5
test 6
test 7
test 8
test 9
test 10
test 11
test 12
test 13
test 14
test 15
test 16
test 17
test 18
test 19
test 20
test 21
test 22
test 23
test 24
test 25
test 26
test 27
test 28
test 29
test 30
test 31
test 32
test 33
test 34
test 35
test 36
test 37
test 38
test 39
test 40
test 41
test 42
test 43
test 44
test 45
test 46
test 47
test 48
test 49
test 50
test 51
test 52
test 53
test 54
test 55
test 56
test 57
test 58
test 59
test 60
test 61
test 62
test 63
test 64
test 65
test 66
test 67
test 68
test 69
test 70
test 71
test 72
test 73
test 74
test 75
test 76
test 77
test 78
test 79
test 80
test 81
test 82
test 83
test 84
test 85
test 86
test 87
test 88
test 89
test 90
test 91
test 92
test 93
test 94
test 95
test 96
test 97
test 98
test 99
test 100
test 101
test 102
test 103
test 104
test 105
test 106
test 107
test 108
test 109
test 110
test 111
test 112
test 113
test 114
test 115
test 116
test 117
test 118
test 119
test 120
test 121
test 122
test 123
test 124
test 125
test 126
test 127
test 128
test 129
test 130
test 131
test 132
test 133
test 134
test 135
test 136
test 137
test 138
test 139
test 140
test 141
test 142
test 143
test 144
test 145
test 146
test 147
test 148
test 149
test 150
test 151
test 152
test 153
test 154
test 155
test 156
test 157
test 158
test 159
test 160
test 161
test 162
test 163
test 164
test 165
test 166
test 167
test 168
test 169
test 170
test 171
test 172
test 173
test 174
test 175
test 176
test 177
test 178
test 179
test 180
test 181
test 182
test 183
test 184
test 185
test 186
test 187
test 188
test 189
test 190
test 191
test 192
test 193
test 194
test 195
test 196
test 197
test 198
test 199
test 200
test 201
test 202
test 203
test 204
test 205
test 206
test 207
test 208
test 209
test 210
test 211
test 212
test 213
test 214
test 215
test 216
test 217
test 218
test 219
test 220
test 221
test 222
test 223
test 224
test 225
test 226
test 227
test 228
test 229
test 230
test 231
test 232
test 233
test 234
test 235
test 236
test 237
test 238
test 239
test 240
test 241
test 242
test 243
test 244
test 245
test 246
test 247
test 248
test 249
test 250
test 251
test 252
test 253
test 254
test 255
test 256
test 257
test 258
test 259
test 260
test 261
test 262
test 263
test 264
test 265
test 266
test 267
test 268
test 269
test 270
test 271
test 272
test 273
test 274
test 275
test 276
test 277
test 278
test 279
test 280
test 281
test 282
test 283
test 284
test 285
test 286
test 287
test 288
test 289
test 290
test 291
test 292
test 293
test 294
test 295
test 296
test 297
test 298
test 299
test 300
test 301
test 302
test 303
test 304
test 305
test 306
test 307
test 308
test 309
test 310
test 311
test 312
test 313
test 314
test 315
test 316
test 317
test 318
test 319
test 320
test 321
test 322
test 323
test 324
test 325
test 326
test 327
test 328
test 329
test 330
test 331
test 332
test 333
test 334
test 335
test 336
test 337
test 338
test 339
test 340
test 341
test 342
test 343
test 344
test 345
test 346
test 347
test 348
test 349
test 350
test 351
test 352
test 353
test 354
test 355
test 356
test 357
test 358
test 359
test 360
test 361
test 362
test 363
test 364
test 365
test 366
test 367
test 368
test 369
test 370
test 371
test 372
test 373
test 374
test 375
test 376
test 377
test 378
test 379
test 380
test 381
test 382
test 383
test 384
test 385
test 386
test 387
test 388
test 389
test 390
test 391
test 392
test 393
test 394
test 395
test 396
test 397
test 398
test 399
test 400
test 401
test 402
test 403
test 404
test 405
test 406
test 407
test 408
test 409
test 410
test 411
test 412
test 413
test 414
test 415
test 416
test 417
test 418
test 419
test 420
test 421
test 422
test 423
test 424
test 425
test 426
test 427
test 428
test 429
test 430
test 431
test 432
test 433
test 434
test 435
test 436
test 437
test 438
test 439
test 440
test 441
test 442
test 443
test 444
test 445
test 446
test 447
test 448
test 449
test 450
test 451
test 452
test 453
test 454
test 455
test 456
test 457
test 458
test 459
test 460
test 461
test 462
test 463
test 464
test 465
test 466
test 467
test 468
test 469
test 470
test 471
test 472
test 473
test 474
test 475
test 476
test 477
test 478
test 479
test 480
test 481
test 482
test 483
test 484
test 485
test 486
test 487
test 488
test 489
test 490
test 491
test 492
test 493
test 494
test 495
test 496
test 497
test 498
test 499
test 500
test 501
test 502
test 503
test 504
test 505
test 506
test 507
test 508
test 509
test 510
test 511
test 512
test 513
test 514
test 515
test 516
test 517
test 518
test 519
test 520
test 521
test 522
test 523
test 524
test 525
test 526
test 527
test 528
test 529
test 530
test 531
test 532
test 533
test 534
test 535
test 536
test 537
test 538
test 539
test 540
test 541
test 542
test 543
test 544
test 545
test 546
test 547
test 548
test 549
test 550
test 551
test 552
test 553
test 554
test 555
test 556
test 557
test 558
test 559
test 560
test 561
test 562
test 563
test 564
test 565
test 566
test 567
test 568
test 569
test 570
test 571
test 572
test 573
test 574
test 575
test 576
test 577
test 578
test 579
test 580
test 581
test 582
test 583
test 584
test 585
test 586
test 587
test 588
test 589
test 590
test 591
test 592
test 593
test 594
test 595
test 596
test 597
test 598
test 599
test 600
test 601
test 602
test 603
test 604
test 605
test 606
test 607
test 608
test 609
test 610
test 611
test 612
test 613
test 614
test 615
test 616
test 617
test 618
test 619
test 620
test 621
test 622
test 623
test 624
test 625
test 626
test 627
test 628
test 629
test 630
test 631
test 632
test 633
test 634
test 635
test 636
test 637
test 638
test 639
test 640
test 641
test 642
test 643
test 644
test 645
test 646
test 647
test 648
test 649
test 650
test 651
test 652
test 653
test 654
test 655
test 656
test 657
test 658
test 659
test 660
test 661
test 662
test 663
test 664
test 665
test 666
test 667
test 668
test 669
test 670
test 671
test 672
test 673
test 674
test 675
test 676
test 677
test 678
test 679
test 680
test 681
test 682
test 683
test 684
test 685
test 686
test 687
test 688
test 689
test 690
test 691
test 692
test 693
test 694
test 695
test 696
test 697
test 698
test 699
test 700
test 701
test 702
test 703
test 704
test 705
test 706
test 707
test 708
test 709
test 710
test 711
test 712
test 713
test 714
test 715
test 716
test 717
test 718
test 719
test 720
test 721
test 722
test 723
test 724
test 725
test 726
test 727
test 728
test 729
test 730
test 731
test 732
test 733
test 734
test 735
test 736
test 737
test 738
test 739
test 740
test 741
test 742
test 743
test 744
test 745
test 746
test 747
test 748
test 749
test 750
test 751
test 752
test 753
test 754
test 755
test 756
test 757
test 758
test 759
test 760
test 761
test 762
test 763
test 764
test 765
test 766
test 767
test 768
test 769
test 770
test 771
test 772
test 773
test 774
test 775
test 776
test 777
test 778
test 779
test 780
test 781
test 782
test 783
test 784
test 785
test 786
test 787
test 788
test 789
test 790
test 791
test 792
test 793
test 794
test 795
test 796
test 797
test 798
test 799
test 800
test 801
test 802
test 803
test 804
test 805
test 806
test 807
test 808
test 809
test 810
test 811
test 812
test 813
test 814
test 815
test 816
test 817
test 818
test 819
test 820
test 821
test 822
test 823
test 824
test 825
test 826
test 827
test 828
test 829
test 830
test 831
test 832
test 833
test 834
test 835
test 836
test 837
test 838
test 839
test 840
test 841
test 842
test 843
test 844
test 845
test 846
test 847
test 848
test 849
test 850
test 851
test 852
test 853
test 854
test 855
test 856
test 857
test 858
test 859
test 860
test 861
test 862
test 863
test 864
test 865
test 866
test 867
test 868
test 869
test 870
test 871
test 872
test 873
test 874
test 875
test 876
test 877
test 878
test 879
test 880
test 881
test 882
test 883
test 884
test 885
test 886
test 887
test 888
test 889
test 890
test 891
test 892
test 893
test 894
test 895
test 896
test 897
test 898
test 899
test 900
test 901
test 902
test 903
test 904
test 905
test 906
test 907
test 908
test 909
test 910
test 911
test 912
test 913
test 914
test 915
test 916
test 917
test 918
test 919
test 920
test 921
test 922
test 923
test 924
test 925
test 926
test 927
test 928
test 929
test 930
test 931
test 932
test 933
test 934
test 935
test 936
test 937
test 938
test 939
test 940
test 941
test 942
test 943
test 944
test 945
test 946
test 947
test 948
test 949
test 950
test 951
test 952
test 953
test 954
test 955
test 956
test 957
test 958
test 959
test 960
test 961
test 962
test 963
test 964
test 965
test 966
test 967
test 968
test 969
test 970
test 971
test 972
test 973
test 974
test 975
test 976
test 977
test 978
test 979
test 980
test 981
test 982
test 983
test 984
test 985
test 986
test 987
test 988
test 989
test 990
test 991
test 992
test 993
test 994
test 995
test 996
Traceback (most recent call last):
  File "forum3.py", line 8, in <module>
    recursive('test')
  File "forum3.py", line 6, in recursive
    recursive(string, num + 1)
...
...
    print('{} {}'.format(string, num))
RuntimeError: maximum recursion depth exceeded while calling a Python object
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1417
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Function Basics

Postby micseydel » Fri Feb 22, 2013 8:06 am

I wouldn't mention yield here, since generators are more advanced. I also don't like the negative tone regarding recursion. It's an excellent tool, even though it's not something we use much in Python.

I think I've noticed this in other tutorials here (maybe even mine, it's hard to tell) but we sometimes say things that make sense to us, but aren't obvious to newbie programmers. For example,
global declares module-level variables that are to be assigned.

I don't believe we ever get into the nitty-gritty of what assignment is, although it's very important to a deep understanding of what is going on. It's also something that should be approached differently depending on the user's experience - someone who is a seasoned C programmer is going to be easier to explain it to than a non-programmer.

I'm not saying this post is terrible and we should take it down, but I think that we'll need to refine what we have so far, keeping in mind the assumptions we make.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1268
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Function Basics

Postby metulburr » Fri Feb 22, 2013 8:10 am

Change what you think needs done. I just took a shot at a different function thread than the one originally there. Or we can grab all the functions threads "good points" and make a new decent function tutorial thread, while leaving out each bad.

I think I've noticed this in other tutorials here (maybe even mine, it's hard to tell) but we sometimes say things that make sense to us, but aren't obvious to newbie programmers.

That is very possible. What is funny is i remember when i started, people would explain things, and i would be like "What???". Now i am the one on the other side of the fence.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1417
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Function Basics

Postby micseydel » Fri Feb 22, 2013 6:43 pm

It's tricky, a person just wanting to learn to code right away doesn't necessarily need to know a lot of details (like the difference between a statement and an expression) however there does come a point when knowing the terminology and all the very technical details becomes worthwhile. I'm not sure where to draw the line.

Also, I appreciate you giving permission to improve this thread, but I mostly wanted to say that it's just very difficult, if I had concrete specific changes I'd have pointed them out right away. I'll think about it though, and we can use replies to discuss the best way to go about things.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1268
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Function Basics

Postby metulburr » Fri Feb 22, 2013 7:11 pm

yeah i would rather have good tutorials written by all of us and edited by all of us to make one good thread, than a war of who wrote this tut or that tut.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1417
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY


Return to Tutorials

Who is online

Users browsing this forum: No registered users and 1 guest