0% found this document useful (0 votes)
8 views

Functions Notes

This document is the summary of the Python language, specifically focusing on Functions.

Uploaded by

asongembaza06
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
8 views

Functions Notes

This document is the summary of the Python language, specifically focusing on Functions.

Uploaded by

asongembaza06
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 5
Cheatsheets / Learn Python 3 Functions Function Parameters Sometimes functions require input to provide data for their code. This input is defined using parameters. Parameters are variables that are defined in the function definition. They are assigned the values which ‘were passed as arguments when the function was called, elsewhere in the code. For example, the function definition defines parameters for a character, a setting, and a skil, which are used as, inputs to write the first sentence of a book. Multiple Parameters Python functions can have multiple parameters. Just as ‘you wouldn't go to school without both a backpack and a pencil case, functions may also need more than one Input to carry out their operations. To define a function with muttiple parameters, parameter names are placed one after another, separated by commas, within the parentheses of the function definition, Functions Some tasks need to be performed multiple times within « program. Rather than rewrite the same code in ‘multiple places, a function may be defined using the def keyword. Function definitions may include parameters, providing data input to the function. Functions may return a value using the return keyword followed by the value to return. [codelcademy weite_ak practicing her ready_for_school : i ‘eull' and t ("I'm read chool!" 4 Define a function m onQ) with rameter x Y 1 # Invoke the function in 2 # ourput: 3 Function Indentation Python uses indentation to identify blocks of code. Code within the same block should be indented at the same level. A Python funetion is one type of code block. ll code under @ function declaration should be indented to identity it as part of the function. There can be additional indentation within a funetion to handle other statements such as for and if solong as the lines are not indented less than the fist line of the function code. Calling Functions Python uses simple syntax to use, invoke, or calla preexisting function. A function can be called by writing the name of it, followed by parentheses. For example, the code provided would call the doHomework() method. Function Arguments Parameters in python are variables — placeholders for ‘the actual values the function needs. When the function is called, the corguments. For example, the arguments passed into the function .sales() are the “The Farmer's Market”, “toothpast and “$1” which correspond to the parameters values are passed in as grocery_store , item_on_sale , and cost [codelcademy # Indentation is used to identify co cks jef testfunction hs # This code is part o de the testf indentation bec. block # but still part of he function part of testfu jef sal Function Keyword Arguments Python functions can be defined with named arguments which may have default values provided. ‘When funetion arguments are passed using their 1s keyword arguments. The rhames, they are referres Use of keyword arguments when calling a function allows the arguments to be passed in any order — not just the order that they were defined in the function. If ‘the function is invoked without a value for a specific argument, the default value will be used. Returning Multiple Values Python functions are able to return multiple values Using one return statement. All values that should be returned are listed after the retum keyword and are separated by commas. Inthe example the function square_point() returns x squared, y_squated and z_squared The Scope of Variables In Python, a variable defined inside a function is called local variable. It eannot be used outside of the scope of the function, and attempting to do so without defining the variable outside of the function will cause Inthe example, the variable @ is defined both inside and outside of the function. When the function f1() is implemented, a is printed as 2 because itis locally defined to be so. However, when printing @ outside of the function, a is printed as 5 because it is Implemented outside of the scope of the function. # Retu # will print 5 # will print 2 [codelcademy Returning Value from Function AA return keyword is used to return a value from a Python function. The value returned from a function ccan be assigned to @ variable which can then be used in the program, Inthe example, the function check_leap_year returns a string which indicates ifthe passed parameter Is aleap year oF not. Global Variables ‘variable that is defined outside of a function is called global variable. It can be accessed inside the body of a funetion, Inthe example, the variable @ is @ global variable because it is defined outside of the function prints_a Itis therefore accessible to prints_a., which will print the value of Parameters as Local Variables Function parameters behave identically to a function's local variables. They are initialized with the values passed into the function when it was called Like tocal variables, parameters cannot be referenced {rom outside the scope of the function. Inthe example, the parameter value is defined as. part of the definition of my_funetion , and therefore can only be accessed within my_function [Attempting to print the contents of value from outside the function causes an error. [codelcademy check_leap_year ) i a= 0 2018 Print 08 Share v

You might also like