![]() |
[Basic] Functions - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: General (https://python-forum.io/forum-1.html) +--- Forum: Tutorials (https://python-forum.io/forum-4.html) +---- Forum: Fundamentals (https://python-forum.io/forum-40.html) +---- Thread: [Basic] Functions (/thread-328.html) |
Functions - metulburr - Oct-05-2016 Why use functions?
Function design concepts
Coding functions They are called in expressions, are passed values, and return results.
Function general format def add(arg1, arg2): value = arg1 + arg2 return valueSo 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. 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: val = add(5,2)you are essentially doing: val = 7because 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. def add(arg1, arg2): value = arg1 + arg2 print(value) print(add(3,3))This snippet of code will print the results: 6 NoneThe 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: 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: def func(func_name): print('in body of function {}'.format(func_name)) func('func') othername = func othername('othername') in body of function func in body of function othernameRecursive functions Functions that call themselves either directly or indirectly in order to loop is recursion. def recursive(string, num=0): print('{} {}'.format(string, num)) recursive(string, num + 1) recursive('test') test 0 test 1 test 2 test 3 test 4 test 5 test 6 test 7 ... 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 |