
Creating Functions in Programming
Learn about the importance of creating functions in programming for reusability, error reduction, and efficiency. Explore the elements of a function, including function definition, parameters, and examples. Discover how functions help in writing cleaner and more organized code.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
Writing Functions It is possible (and common) to create functions when writing a program Why? Reusability Sharing Reduces errors Write and debug the function once Avoid redundancy Professor John Carelli, Kutztown University
Function Definition Creating a function involves 4 basic parts def keyword This tells Python that what follows is a function definition The function name All functions have a name parameters This is the list of variables that accept information passed to the function from the argument list body The statements that do the work in the function Source: Pearson Education, Inc Professor John Carelli, Kutztown University
Elements of a Function def fname (par1, par2, ): statements [return statement] Functions include: def followed by the function name (required) A list of parameters (optional) As needed, internal statements (indented) If output is desired, a return statement (optional) Professor John Carelli, Kutztown University
Function Parameters def fname (par1, par2, ): statements [return statement] Parameters is a list of variables that accept information from the calling routine (via the arguments) When the function is called, the values of the arguments are passed (assigned) to the parameters in the function in the order given The parameters are then used in the function statements to perform computations Professor John Carelli, Kutztown University
Function Parameters def fname (par1, par2, ): statements [return statement] The return statement of a function serves as its output It can be a variable or an expression The resulting value is passed back to the calling routine Ex: input() function returns what the user typed (as a string) Note: print() function has no return Professor John Carelli, Kutztown University
Function Examples def add(x, y): sum= x + y return sum def hello(name): print( Hello ,name); def getName(): name= input( Enter name ) return name Professor John Carelli, Kutztown University
Function Invocation Functions must be defined before they are called Python issues an error if an undefined function is called Functions are called by: Invoking the function name Providing a input list of arguments The arguments are assigned in the order provided to the defined function parameters z= add(a,b) After this statement is executed, z will contain the sum of a and b (using add() as declared previously) Professor John Carelli, Kutztown University
Local Variables and Scope All variables created in a function are local to the function They don t exist outside the function Including both parameters (in the input list) And variables declared in internal function statements They have local scope Any modifications to a local variable do not affect variables in the calling routine Even if they have the same name! All variables created in the main function (the top level) can be seen and used in functions They have global scope They are global variables Professor John Carelli, Kutztown University
Procedural Programming A programming approach wherein a problem is broken down into sub tasks. Functions are written to address each subtask The overall problem is compartmentalized Each function handles a smaller and simpler task Functions are then called in the main program to solve the overall problem Usually involves writing a top-level main function Professor John Carelli, Kutztown University
main() A top-level function that serves as the entry point for the program Program execution begins in the main() function Functions addressing sub-tasks are called, as needed, from main() General Approach: Define the main() function first in the file Followed by other functions, defined as needed, that are called in main After all functions are defined, call main() Professor John Carelli, Kutztown University
Using __name__ __name__ is a global Python variable __name__ is set to the name of the currently running function If a file contains the following if statement and the file is invoked in Python, __name__ is set to the value __main__ result is that main() gets called if __name__ == '__main__ : main() Professor John Carelli, Kutztown University