WHAT ARE FUNCTIONS ?
Functions are named blocks of code designed to do one specific job. Functions allow you to write code once that can then be run whenever you need accomplish the same task. Functions can take in the information they you need , and return the information they generate. Using functions effectively
makes your programs write ,read,text and fix.
Defining a function
The first line of a function is its definition, marked by the keyword def. The name, of the functions is followed by a set of parentheses and a colon. A docstring in triple quotes, describes what the function does. The body of a function is indented one level.
To call a function, give the name of the function followed by a set of parantheses.
#making a function def greet_user(): """display a simple greeting.""" print ("hello") greet_user()
Passing information to a function
Information that's passed function is called an argument: information that's received by a function is called a parameter. Arguments are included in parentheses after the function's name, and parameters are listed in parentheses in the function's definition.
#passing a single argument def greet_user(username): """Display a simple greeting.""" print("hello, " + username + "!") greet_user('jesse') greet_user('diana') greet_user('brandon')
Positional and keyword arguments
The two main kinds of arguments are positional and keyword arguments. When you use positionl arguments Python matches the first arguments in the function call with the first parameter in the function call with the first parameter in the fumction defination and so forth.
with keyword argument you specify which parameter each argument should be assigned to in the function call. When you use keyword arguments, the order of the arguments doesn't matter.
#using positinial arguments def describe_pet(animal, name): """Display information about a pet.""" print("\nI have a " + animal + ".") print("its name is " + name + ".") describe_pet('hamster', 'harry') describe_pet('dog', 'willie') #using keywords arguments describe_pet(animal='hamster', name='harry') describe_pet(name='while', animal='dog')
Defaults values
You can provide a default value for a parameter. When function calls omit this argument the default value will be used. Parameters with default values must be listed after parameter without default values in the functions defination so possitional arguments can still work correctly.
#using a default value def descibe_pet(name, animal='dog'): """display information about a pet.""" print("\nI have a " + animal + ".") print ("its anme is " + name + ".") describe_pet(name, animal='dog') describe_pet('willie') #using none to make an argument optinal def describe_pet(animal, name=none): """display information about a pet.""" print("\nI have a " + animal + ".") if name: print("its name is " + name + ".") describe_pet('hamster', 'harry') describe_pet('snake')
Return values
#returning a single value def get_full_name(first, last): """return a neatly formatted full name.""" full_name = first + ' ' + last return full_name.title() musician=get_full_name('jimi', 'hendrix') print(musician) #returning a dictionary def build_person(first, last): """return a dictionary of information about a person. """ person = {'first': first, 'last': last} return person musician = build_person('jimi', 'hendrix') print(musician) #returning a dictionary with optional values def build_person(first, last, age=None): """return a dictionary of information about aperson. """ person = {'first': first, 'last': last} if age: person['age'] = age return person musician = build_person('jimi', 'hendrix', 27) print(musician) musican = build_person('janis', 'joplin') print(musican)
No comments:
Post a Comment