Functions

¡Supera tus tareas y exámenes ahora con Quizwiz!

Docstring

A comment in triple quotes that describes what the function does.

Return Values

A function does not always have to display its output directly. Instead, it can process some data and then return a value to the calling line. The value the function returns is called a return value, which can be a number, string, list, etc. When you call a function that returns a value, you must provide a variable to store the return value. Syntax: return value Example: def get_formatted_name(first_name, last_name): -->"""Return a full name, neatly formatted.""" -->full_name = first_name + ' ' + last_name -->return full_name.title() my_name = get_formatted_name('kate', 'safian') print(my_name)

Function

A named block of code that accepts instructions and returns information to perform a specific job. You can call a function to perform a task that you have defined within the function. Functions are useful because they can be called multiple times, which saves time from having to write the code again and again.

Parameters

A piece of information the function accepts in order to do its job. The parameter name is a temporary variable.

Avoiding argument errors

Always be sure to call a function with the exact number of arguments it can accept. If your function definition contains three parameters, pass three arguments when you make a function call. DO NOT pass less or more since unmatched arguments result in errors.

Defining a function

Define a function name and the parameters (instructions/ information) the function accepts to do its job. The body of the function is where the actual job of the function happens. Syntax: def functionName(parameters): -->Body of function Example: def greet_user(username): -->"""Display a simple greeting.""" -->print("Hello, " + username.title() + "!")

Arguments

Information that is passed from a function call to a function in order to perform the function's task/job. Arguments are either numbers or strings (use quotes!).

Passing an arbitrary number of arguments

Sometimes you won't know ahead of time how many arguments a function needs to accept. To accept an arbitrary number of arguments from the calling line, put an asterisk * in the function definition in front of the parameters. Example: def make_pizza(*toppings): -->"""Print the list of toppings that have been requested.""" -->print(toppings) make_pizza('mushrooms', 'green peppers', 'extra cheese')

Calling a function

Specify the functionName you would like to call, along with the arguments (information) that match up with the parameters in the function definition. Syntax: functionName(arguments) Example: greet_user('wine lover')

Positional arguments

When you call a function, Python must match each argument in the function call with a parameter in the function definition. The simplest way to do this is based on the order of the arguments provided. Values matched up this way are called positional arguments. Be careful of the order because your call must match up with your function definition! Example: def describe_pet(animal_type, pet_name): -->"""Display information about a pet.""" -->print(f"My{animal_type}'s name is {pet_name.title()}.") describe_pet('dog', 'maybell')

Passing a list

You can also pass a list as an argument to a function. When you pass a list to a function, the function gets direct access to the contents of the list. Example: def greet_users(names): -->"""Print a simple greeting to each user in the list.""" -->for name in names: ----->msg= f"Hello, {name.title()} !" ----->print(msg) usernames = ['hannah', 'ty', 'margot'] greet_users(usernames)


Conjuntos de estudio relacionados

Chapter 59: Assessment and Management of Problems Related to Male Reproductive Processes NCLEX

View Set

CH 11 Bio 141 intro to nervous tissue

View Set

Chapter 44: Disorders of the Skeletal System: Metabolic and Rheumatic Disorders-Patho http://thepoint.lww.com/Book/Show LEVEL 3

View Set

Chapter 12 Building and Managing teams

View Set

level 9 chapter 2 single and dual agency

View Set