The document provides an introduction to functions in Python. It begins by explaining that a function is a block of code that runs when called. It then discusses how functions can take in parameters and return values. The document walks through examples of creating, calling, and writing functions. It also covers topics like default parameter values, arbitrary arguments, and using a main function.
The document discusses functions in programming. It defines functions as named blocks of code that perform tasks. Functions help break programs into smaller, reusable chunks. There are built-in functions included with languages like Python as well as user-defined functions. Functions make programs more organized and modular by allowing code to be reused and shared across the program. Parameters and arguments are used to pass information into functions.
Functions allow programmers to organize code into reusable blocks. A function is defined using the def keyword and can accept parameters. The body of a function contains a set of statements that run when the function is called. Functions can return values and allow code to be reused, reducing errors and improving readability. Parameters allow information to be passed into functions, while return values allow functions to provide results.
This document discusses writing your own functions in Python. It begins by explaining that functions allow you to make code reusable and simplify programs. Functions take arguments, perform operations, and can return values. The document then provides examples of defining, calling, and returning values from functions. It highlights important aspects of functions like arguments, return statements, and scope.
This document discusses various Python operators and control flow statements. It covers operators like arithmetic, comparison, bitwise, and logical operators. It also discusses precedence rules for operators. For control flow, it explains if, while, for statements along with break, continue, else blocks. It provides examples of functions, parameters, default arguments, keyword arguments, local and global variables, and return statements.
This document discusses Python functions, including built-in functions, defining custom functions, parameters, arguments, return values, and more. Some key points:
- Functions allow storing and reusing code through definition and invocation.
- Built-in functions like print() come with Python, while custom functions are defined using def and def keywords.
- Parameters receive arguments passed to functions, and allow code within a function to access values from a particular call.
- Functions can return values using the return statement. Functions that produce results are called "fruitful," while those that don't are "void."
- Functions help organize code into logical, reusable blocks and avoid repetition. They can also break complex
This lecture covers functions and modules in Python. It discusses built-in functions, writing user-defined functions, function parameters and return values, and scope. It also discusses modules as a way to organize Python code and introduces some of Python's standard library modules like math and random. The key points are:
1) Functions allow for code reuse and abstraction by encapsulating repeatable processes. Built-in functions perform common tasks while user-defined functions can be created for custom tasks.
2) Functions are defined using def and have parameters to pass in inputs and return values to produce outputs. Variables inside functions have local scope while global variables can be accessed everywhere.
3) Modules help structure Python programs and provide
The document discusses functions in programming. It defines functions as named blocks of code that perform tasks. Functions help break programs into smaller, reusable chunks. There are built-in functions included with languages like Python as well as user-defined functions. Functions make programs more organized and modular by allowing code to be reused and shared across the program. Parameters and arguments are used to pass information into functions.
Functions allow programmers to organize code into reusable blocks. A function is defined using the def keyword and can accept parameters. The body of a function contains a set of statements that run when the function is called. Functions can return values and allow code to be reused, reducing errors and improving readability. Parameters allow information to be passed into functions, while return values allow functions to provide results.
This document discusses writing your own functions in Python. It begins by explaining that functions allow you to make code reusable and simplify programs. Functions take arguments, perform operations, and can return values. The document then provides examples of defining, calling, and returning values from functions. It highlights important aspects of functions like arguments, return statements, and scope.
This document discusses various Python operators and control flow statements. It covers operators like arithmetic, comparison, bitwise, and logical operators. It also discusses precedence rules for operators. For control flow, it explains if, while, for statements along with break, continue, else blocks. It provides examples of functions, parameters, default arguments, keyword arguments, local and global variables, and return statements.
This document discusses Python functions, including built-in functions, defining custom functions, parameters, arguments, return values, and more. Some key points:
- Functions allow storing and reusing code through definition and invocation.
- Built-in functions like print() come with Python, while custom functions are defined using def and def keywords.
- Parameters receive arguments passed to functions, and allow code within a function to access values from a particular call.
- Functions can return values using the return statement. Functions that produce results are called "fruitful," while those that don't are "void."
- Functions help organize code into logical, reusable blocks and avoid repetition. They can also break complex
This lecture covers functions and modules in Python. It discusses built-in functions, writing user-defined functions, function parameters and return values, and scope. It also discusses modules as a way to organize Python code and introduces some of Python's standard library modules like math and random. The key points are:
1) Functions allow for code reuse and abstraction by encapsulating repeatable processes. Built-in functions perform common tasks while user-defined functions can be created for custom tasks.
2) Functions are defined using def and have parameters to pass in inputs and return values to produce outputs. Variables inside functions have local scope while global variables can be accessed everywhere.
3) Modules help structure Python programs and provide
Python is an interpreted, general-purpose, high-level programming language. It allows programmers to define functions for reusing code and scoping variables within functions. Key concepts covered include objects, expressions, conditionals, loops, modules, files, and recursion. Functions can call other functions, allowing for modular and reusable code.
Dive into Python Functions Fundamental Concepts.pdfSudhanshiBakre1
This document discusses Python functions, including how to define functions with parameters, return values, and docstrings. It covers calling functions and passing arguments, including positional and keyword arguments. The document also discusses built-in functions, user-defined functions, and when to use functions to improve code modularity, reusability, and simplify complex tasks. Tips are provided such as keeping functions short, using descriptive names, testing functions, and taking advantage of features like default arguments.
The document discusses functions in Python. It defines what functions are, how to define functions, pass arguments to functions, return values from functions, and scope of objects in functions. It covers built-in functions, when to write your own functions, and different types of arguments like required arguments, keyword arguments, default arguments, and variable-length arguments. It also discusses how functions communicate with their environment through parameters and arguments, and pass-by-reference vs pass-by-value in Python functions.
This document discusses functions in Python. It defines functions as collections of statements that perform specific tasks. There are three types of functions: built-in functions, module functions, and user-defined functions. Built-in functions are predefined in Python, module functions are contained in .py files, and user-defined functions are created by the user. The document provides examples of various types of functions and how they can be called and used.
Code Like Pythonista
Beautifully made PPT.
Ref. http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html
Image ref : https://pixabay.com/ko/ and https://morguefile.com/
licensed under a Creative Commons Attribution/Share-Alike (BY-SA) license.
Chapter Introduction to Modular Programming.pptAmanuelZewdie4
Modular programming involves breaking down a program into individual components (modules) that can be programmed and tested independently. Functions are used to implement modules in C++. Functions must be declared before use so the compiler knows their name, return type, and parameters. Functions are then defined by providing the body of code. Variables used within a function have local scope while variables declared outside have global scope. Functions can pass arguments either by value, where a copy is passed, or by reference, where the address is passed allowing the argument to be modified. Arrays and strings passed to functions are passed by reference as pointers.
Python programming - Functions and list and tuplesMalligaarjunanN
- A function is a block of reusable code that takes in parameters, performs an action, and returns a value. Functions provide modularity and code reusability.
- Functions in Python are defined using the def keyword followed by the function name and parameters in parentheses. The code block is indented and can return a value. Parameters can have default values.
- Functions can take positional arguments, keyword arguments, and variable length arguments. Parameters are passed by reference, so changes inside the function also affect the variables outside.
- Anonymous functions called lambdas are small single expression functions defined with the lambda keyword. They do not have a name and cannot contain multiple expressions or statements.
Functions allow for code reusability and modularization. A function is defined using the def keyword followed by the function name and parameters. Functions can take in arguments as inputs and return a value. Variables declared inside a function have local scope while those outside have global scope. Default parameter values can be specified in the function definition.
This document provides an overview of functions in C++. It defines what a function is, how to declare and define functions, how to call functions, and the differences between passing arguments by value versus by reference. A function is a block of code that performs a specific task. Functions are declared with a return type and parameter list, and defined with a body of code. Arguments can be passed into functions either by value, where the function receives a copy of the argument, or by reference, where any changes to the argument are reflected in the original variable. Well-designed programs use modular functions to organize code into reusable components.
This document discusses functions in Python. It defines what a function is and provides the basic syntax for defining a function using the def keyword. It also covers function parameters, including required, keyword, default, and variable-length arguments. The document explains how to call functions and discusses pass by reference vs pass by value. Additionally, it covers anonymous functions, function scope, and global vs local variables.
LESSON 4: INTRODUCING FUNCTIONS AND MODULAR DESIGN
Learn about Functions in Python. Advantages and disadvantages of functions. Introduction to Modular design. Local and Global Variables and their use. Passing parameters. What are arguments? Big questions: Evolution vs Intelligent design in light of functions (and modular design). A closer look at Robotics and advances in this field. Challenges and tasks including with solutions. Suggested research/HW and YouTube video recommendations. A note on Python’s built in functions.
This document discusses functions in Python. It covers defining functions, using arguments, importing functions from libraries, and some built-in functions. Key points include:
- Functions are reusable blocks of code that perform specific tasks. They reduce repetition and make code easier to debug.
- To define a function use def and give it a name and body, optionally including arguments in parentheses.
- Functions can be called by name and optionally pass arguments.
- Useful functions can be imported from libraries like math using import or from module import function.
- Python has many built-in functions like max(), min(), abs(), and type() that do not require importing.
- Functions are subprograms that perform specific tasks and can optionally return a value. Large programs are broken down into smaller functions.
- There are three types of functions: built-in functions, functions in modules, and user-defined functions. Built-in functions are predefined in Python. Functions in modules must be imported. User-defined functions are created by the programmer.
- To define a user-defined function, use the def keyword followed by the function name and parameters. The function body is indented and statements can optionally return a value. Functions are called by their name with arguments.
This document discusses programming languages and how computers execute code. It explains that high-level languages like Python, C++, and Java are translated into low-level machine language before the computer can run programs. It also discusses interpreters and compilers as tools that translate high-level code. The document then provides an introduction to the Python programming language, including basic Python vocabulary terms. It gives examples of Python code and explains concepts like variables, functions, operators, and data types.
Intro to Programming with JavaScript Seminar, Fall 2017 semester
Week 2: Function
Led by Jeongbae Oh, in conjunction with YCC (Yonsei Computer Club) @ Yonsei University
This seminar intends to introduce newcomers to programming using JavaScript, one of the most versatile languages of the modern world.
Python keywords are reserved words that have special meaning in the language. Keywords cannot be used as variable or function names. Identifiers are names given to variables, functions, classes, etc. and follow naming rules like not starting with a number. Functions allow code reuse by taking in parameters and returning values. Data types in Python include numbers, strings, lists, dictionaries and more.
हिंदी वर्णमाला पीपीटी, hindi alphabet PPT presentation, hindi varnamala PPT, Hindi Varnamala pdf, हिंदी स्वर, हिंदी व्यंजन, sikhiye hindi varnmala, dr. mulla adam ali, hindi language and literature, hindi alphabet with drawing, hindi alphabet pdf, hindi varnamala for childrens, hindi language, hindi varnamala practice for kids, https://www.drmullaadamali.com
Python is an interpreted, general-purpose, high-level programming language. It allows programmers to define functions for reusing code and scoping variables within functions. Key concepts covered include objects, expressions, conditionals, loops, modules, files, and recursion. Functions can call other functions, allowing for modular and reusable code.
Dive into Python Functions Fundamental Concepts.pdfSudhanshiBakre1
This document discusses Python functions, including how to define functions with parameters, return values, and docstrings. It covers calling functions and passing arguments, including positional and keyword arguments. The document also discusses built-in functions, user-defined functions, and when to use functions to improve code modularity, reusability, and simplify complex tasks. Tips are provided such as keeping functions short, using descriptive names, testing functions, and taking advantage of features like default arguments.
The document discusses functions in Python. It defines what functions are, how to define functions, pass arguments to functions, return values from functions, and scope of objects in functions. It covers built-in functions, when to write your own functions, and different types of arguments like required arguments, keyword arguments, default arguments, and variable-length arguments. It also discusses how functions communicate with their environment through parameters and arguments, and pass-by-reference vs pass-by-value in Python functions.
This document discusses functions in Python. It defines functions as collections of statements that perform specific tasks. There are three types of functions: built-in functions, module functions, and user-defined functions. Built-in functions are predefined in Python, module functions are contained in .py files, and user-defined functions are created by the user. The document provides examples of various types of functions and how they can be called and used.
Code Like Pythonista
Beautifully made PPT.
Ref. http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html
Image ref : https://pixabay.com/ko/ and https://morguefile.com/
licensed under a Creative Commons Attribution/Share-Alike (BY-SA) license.
Chapter Introduction to Modular Programming.pptAmanuelZewdie4
Modular programming involves breaking down a program into individual components (modules) that can be programmed and tested independently. Functions are used to implement modules in C++. Functions must be declared before use so the compiler knows their name, return type, and parameters. Functions are then defined by providing the body of code. Variables used within a function have local scope while variables declared outside have global scope. Functions can pass arguments either by value, where a copy is passed, or by reference, where the address is passed allowing the argument to be modified. Arrays and strings passed to functions are passed by reference as pointers.
Python programming - Functions and list and tuplesMalligaarjunanN
- A function is a block of reusable code that takes in parameters, performs an action, and returns a value. Functions provide modularity and code reusability.
- Functions in Python are defined using the def keyword followed by the function name and parameters in parentheses. The code block is indented and can return a value. Parameters can have default values.
- Functions can take positional arguments, keyword arguments, and variable length arguments. Parameters are passed by reference, so changes inside the function also affect the variables outside.
- Anonymous functions called lambdas are small single expression functions defined with the lambda keyword. They do not have a name and cannot contain multiple expressions or statements.
Functions allow for code reusability and modularization. A function is defined using the def keyword followed by the function name and parameters. Functions can take in arguments as inputs and return a value. Variables declared inside a function have local scope while those outside have global scope. Default parameter values can be specified in the function definition.
This document provides an overview of functions in C++. It defines what a function is, how to declare and define functions, how to call functions, and the differences between passing arguments by value versus by reference. A function is a block of code that performs a specific task. Functions are declared with a return type and parameter list, and defined with a body of code. Arguments can be passed into functions either by value, where the function receives a copy of the argument, or by reference, where any changes to the argument are reflected in the original variable. Well-designed programs use modular functions to organize code into reusable components.
This document discusses functions in Python. It defines what a function is and provides the basic syntax for defining a function using the def keyword. It also covers function parameters, including required, keyword, default, and variable-length arguments. The document explains how to call functions and discusses pass by reference vs pass by value. Additionally, it covers anonymous functions, function scope, and global vs local variables.
LESSON 4: INTRODUCING FUNCTIONS AND MODULAR DESIGN
Learn about Functions in Python. Advantages and disadvantages of functions. Introduction to Modular design. Local and Global Variables and their use. Passing parameters. What are arguments? Big questions: Evolution vs Intelligent design in light of functions (and modular design). A closer look at Robotics and advances in this field. Challenges and tasks including with solutions. Suggested research/HW and YouTube video recommendations. A note on Python’s built in functions.
This document discusses functions in Python. It covers defining functions, using arguments, importing functions from libraries, and some built-in functions. Key points include:
- Functions are reusable blocks of code that perform specific tasks. They reduce repetition and make code easier to debug.
- To define a function use def and give it a name and body, optionally including arguments in parentheses.
- Functions can be called by name and optionally pass arguments.
- Useful functions can be imported from libraries like math using import or from module import function.
- Python has many built-in functions like max(), min(), abs(), and type() that do not require importing.
- Functions are subprograms that perform specific tasks and can optionally return a value. Large programs are broken down into smaller functions.
- There are three types of functions: built-in functions, functions in modules, and user-defined functions. Built-in functions are predefined in Python. Functions in modules must be imported. User-defined functions are created by the programmer.
- To define a user-defined function, use the def keyword followed by the function name and parameters. The function body is indented and statements can optionally return a value. Functions are called by their name with arguments.
This document discusses programming languages and how computers execute code. It explains that high-level languages like Python, C++, and Java are translated into low-level machine language before the computer can run programs. It also discusses interpreters and compilers as tools that translate high-level code. The document then provides an introduction to the Python programming language, including basic Python vocabulary terms. It gives examples of Python code and explains concepts like variables, functions, operators, and data types.
Intro to Programming with JavaScript Seminar, Fall 2017 semester
Week 2: Function
Led by Jeongbae Oh, in conjunction with YCC (Yonsei Computer Club) @ Yonsei University
This seminar intends to introduce newcomers to programming using JavaScript, one of the most versatile languages of the modern world.
Python keywords are reserved words that have special meaning in the language. Keywords cannot be used as variable or function names. Identifiers are names given to variables, functions, classes, etc. and follow naming rules like not starting with a number. Functions allow code reuse by taking in parameters and returning values. Data types in Python include numbers, strings, lists, dictionaries and more.
हिंदी वर्णमाला पीपीटी, hindi alphabet PPT presentation, hindi varnamala PPT, Hindi Varnamala pdf, हिंदी स्वर, हिंदी व्यंजन, sikhiye hindi varnmala, dr. mulla adam ali, hindi language and literature, hindi alphabet with drawing, hindi alphabet pdf, hindi varnamala for childrens, hindi language, hindi varnamala practice for kids, https://www.drmullaadamali.com
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Dr. Vinod Kumar Kanvaria
Exploiting Artificial Intelligence for Empowering Researchers and Faculty,
International FDP on Fundamentals of Research in Social Sciences
at Integral University, Lucknow, 06.06.2024
By Dr. Vinod Kumar Kanvaria
This slide is special for master students (MIBS & MIFB) in UUM. Also useful for readers who are interested in the topic of contemporary Islamic banking.
How to Setup Warehouse & Location in Odoo 17 InventoryCeline George
In this slide, we'll explore how to set up warehouses and locations in Odoo 17 Inventory. This will help us manage our stock effectively, track inventory levels, and streamline warehouse operations.
Main Java[All of the Base Concepts}.docxadhitya5119
This is part 1 of my Java Learning Journey. This Contains Custom methods, classes, constructors, packages, multithreading , try- catch block, finally block and more.
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPRAHUL
This Dissertation explores the particular circumstances of Mirzapur, a region located in the
core of India. Mirzapur, with its varied terrains and abundant biodiversity, offers an optimal
environment for investigating the changes in vegetation cover dynamics. Our study utilizes
advanced technologies such as GIS (Geographic Information Systems) and Remote sensing to
analyze the transformations that have taken place over the course of a decade.
The complex relationship between human activities and the environment has been the focus
of extensive research and worry. As the global community grapples with swift urbanization,
population expansion, and economic progress, the effects on natural ecosystems are becoming
more evident. A crucial element of this impact is the alteration of vegetation cover, which plays a
significant role in maintaining the ecological equilibrium of our planet.Land serves as the foundation for all human activities and provides the necessary materials for
these activities. As the most crucial natural resource, its utilization by humans results in different
'Land uses,' which are determined by both human activities and the physical characteristics of the
land.
The utilization of land is impacted by human needs and environmental factors. In countries
like India, rapid population growth and the emphasis on extensive resource exploitation can lead
to significant land degradation, adversely affecting the region's land cover.
Therefore, human intervention has significantly influenced land use patterns over many
centuries, evolving its structure over time and space. In the present era, these changes have
accelerated due to factors such as agriculture and urbanization. Information regarding land use and
cover is essential for various planning and management tasks related to the Earth's surface,
providing crucial environmental data for scientific, resource management, policy purposes, and
diverse human activities.
Accurate understanding of land use and cover is imperative for the development planning
of any area. Consequently, a wide range of professionals, including earth system scientists, land
and water managers, and urban planners, are interested in obtaining data on land use and cover
changes, conversion trends, and other related patterns. The spatial dimensions of land use and
cover support policymakers and scientists in making well-informed decisions, as alterations in
these patterns indicate shifts in economic and social conditions. Monitoring such changes with the
help of Advanced technologies like Remote Sensing and Geographic Information Systems is
crucial for coordinated efforts across different administrative levels. Advanced technologies like
Remote Sensing and Geographic Information Systems
9
Changes in vegetation cover refer to variations in the distribution, composition, and overall
structure of plant communities across different temporal and spatial scales. These changes can
occur natural.
A review of the growth of the Israel Genealogy Research Association Database Collection for the last 12 months. Our collection is now passed the 3 million mark and still growing. See which archives have contributed the most. See the different types of records we have, and which years have had records added. You can also see what we have for the future.
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...PECB
Denis is a dynamic and results-driven Chief Information Officer (CIO) with a distinguished career spanning information systems analysis and technical project management. With a proven track record of spearheading the design and delivery of cutting-edge Information Management solutions, he has consistently elevated business operations, streamlined reporting functions, and maximized process efficiency.
Certified as an ISO/IEC 27001: Information Security Management Systems (ISMS) Lead Implementer, Data Protection Officer, and Cyber Risks Analyst, Denis brings a heightened focus on data security, privacy, and cyber resilience to every endeavor.
His expertise extends across a diverse spectrum of reporting, database, and web development applications, underpinned by an exceptional grasp of data storage and virtualization technologies. His proficiency in application testing, database administration, and data cleansing ensures seamless execution of complex projects.
What sets Denis apart is his comprehensive understanding of Business and Systems Analysis technologies, honed through involvement in all phases of the Software Development Lifecycle (SDLC). From meticulous requirements gathering to precise analysis, innovative design, rigorous development, thorough testing, and successful implementation, he has consistently delivered exceptional results.
Throughout his career, he has taken on multifaceted roles, from leading technical project management teams to owning solutions that drive operational excellence. His conscientious and proactive approach is unwavering, whether he is working independently or collaboratively within a team. His ability to connect with colleagues on a personal level underscores his commitment to fostering a harmonious and productive workplace environment.
Date: May 29, 2024
Tags: Information Security, ISO/IEC 27001, ISO/IEC 42001, Artificial Intelligence, GDPR
-------------------------------------------------------------------------------
Find out more about ISO training and certification services
Training: ISO/IEC 27001 Information Security Management System - EN | PECB
ISO/IEC 42001 Artificial Intelligence Management System - EN | PECB
General Data Protection Regulation (GDPR) - Training Courses - EN | PECB
Webinars: https://pecb.com/webinars
Article: https://pecb.com/article
-------------------------------------------------------------------------------
For more information about PECB:
Website: https://pecb.com/
LinkedIn: https://www.linkedin.com/company/pecb/
Facebook: https://www.facebook.com/PECBInternational/
Slideshare: http://www.slideshare.net/PECBCERTIFICATION
2. HOW ARE YOU FEELINGS SO FAR?
• You just completed your first test, and are roughly 1/4th to 1/3rd through the semester. Congratulations,
how are you feeling? Not feeling on solid footing is normal. I once asked the professor towards the end
of the semester “When did it click for you?” and she replied at the end of her second semester in CSC.
Meaning, keep going. If you don’t have command, don’t feel bad. It’s really hard, just keep practing
and learning, and it will come.
• This powerpoint will be about functions. It is a very basic introduction to get you comfortable with the
idea, and give you comfortability with the topic as you tackle the reading, quiz, and labs. As always,
reach out to me, or the professor, with questions about the topic.
3. FUNCTIONS
• A function is a block of code which only runs when it is called.
• You “call” variables all the time when you reference them later on in your code. A function is similarly called, but it’s a block
of code rather than a single value.
• You can pass data, known as parameters, into a function.
• print() is a built in function of python. You call it by referencing it and passing your own data through it. print(“I am passing
data through the print function”)
• A function can return data as a result. In the print function’s case, it’s returns the data you see on screen. There’s many
types of functions that serves many different roles.
• Lets jus think about what we know about functions in math. You put something into a function, and you get an output. In
python we can design our own functions. They’re extremely useful, and like variables can be called upon effortlessly.
4. CREATING A FUNCTION
• In Python a function is defined using the def keyword:
• For example:
• def my_function():
print(“This is my function")
• def is like if for decisions or while for loops, it is the header. Notice how it also ends with a colon :
• In our example we name our function my_function. Functions, like variables, have the exact same
naming requirements.
• Just like if/else, or loops, INDENT AFTER THE HEADER. The statements below your function header
belong to that function and only that function. Unless you call the function by referencing it, the code
will not execute.
5. CALLING FUNCTIONS
• To call a function, use the function name followed by parenthesis:
• For example,
• def my_function():
• print(“This is my function")
my_function()
As you can see, it does not execute unless we call it. This is incredibly useful. If you have complex
calculations in your code, or a giant block of code, it can be called with just the name of a function. This
saves space, time, and speed. Again, its like a variable in which its values can be easily called, but It can
store a lot more values.
6. PARAMETER VARIABLES
• An parameter variable is any information that can be passed through a function.
• Paramter variables are specified after the function name, inside the parentheses. You can add as many
parameter variables as you want, just separate them with a comma.
• The following example has a function with one paramater (fname). When the function is called, we pass
along a first name, which is used inside the function to print the full name:
7. • def my_function(fname):
• print(fname + " Peacock")
• my_function(“Colson")
• my_function(“Zachary")
• my_function(“Brittany")
Again, the argument is fname(first name). Then we pass along our values after for that argument. Then
the print function takes what we passed through for fname, and concatenates that with the string
“peacock”.
8. # OF PARAMETER VARIABLES
• By default, a function must be called with the correct number of arguments. Meaning that if your function expects 2 arguments, you have to
call the function with 2 arguments, not more, and not less. We saw in the second slide, there does not have to be an argument. And
theoretically, there can be an unlimited amount. It just has to match up.
• Example
• This function expects 3 arguments, and gets 3arguments:
• def my_function(fname, lname, eyeColor):
• print(fname + " " + lname+” “+eyeColor)
• my_function(“Caleb", “Peacock“, “has blue eyes”)
• If you try to call the function with 1 or 2 arguments, you will get an error.
9. ARBITRARY ARGUMENTS
• If you do not know how many arguments that will be passed into your function, add a * before the parameter name in the
function definition.
• This way the function will receive a tuple of arguments, and can access the items accordingly:
def my_function(*sibling):
print("The youngest sibling is " + sibling[3])
my_function(“Brittany", “Zachary", “Colson“, “Caleb”)
Delete the star(*) from the argument “sibling”. What happens? Why?
When not sure how many arguments are going to be passed through your function, * is a good bet.
10. DEFAULT PARAMETER VALUE
• The following example shows how to use a default parameter value.
• If we call the function without argument, it uses the default value:
• For example,
• def my_function(country = "Norway"):
• print("I am from " + country)
• my_function("Sweden")
• my_function("India")
• my_function()
• my_function("Brazil")
As you can see, we can pass whatever values we want through the argument or parameter variable. It’s only expecting one value, and we can put
whatever country we want. If we leave empty (), it defaults to the default value to the parameter, “Norway”.
11. RETURN VALUES
• Arithmetic!!! Let’s get into it. I think this is the really cool part of functions and why they are so useful. Let’s say we want to calculate the trajectory of a rocket, but
multiple times with different launching angles, fuel capacity, weight of the rocket, etc. These are all parameter variables we could pass through the function of
determining a rockets trajectory. Now, I am not a rocket scientist and do not know how to calculate that, but if I were, I could easily write the equation once at the
beginning of my code, and then just call that equation using a function passing different variables such as angle and weight and I will get an answer immediately.
• To let a function return a value, use the return statement:
• def my_function(x):
• return 5 * x
• print(my_function(3))
• print(my_function(5))
• print(my_function(9))
Output? You will be using return A LOT, so memorize it! It returns whatever you pass through the arguments. So it’s returning 5* 3(what you passed through the x
argument).
12. LETS WRITE A FUNCTION TOGETHER
• Lets write a function. Keep it simple. Lets just calculate the sum of two integers. How would we do
that?
• We need the start the function with a key word def, followed by the function name, parameters(if any),
and then a colon.
• def calculate_sum(x,y):
• We start off with def, name our function to something that can tell a general user what the function
does, and then we name our two parameter variables, x and y
13. WRITING A FUNCTION TOGETHER
• def calculate_sum(x,y):
Now what? Well we need to write the code for the function. How do you calculate sum? Lets take the
two parameter variables, x and y, and incorporate them to calculate our sum.
def calculate_sum(x,y):
return (x+y)
Now, our function is complete!!! We just need to call our function. How do we do that?
14. AVERAGE FUNCTION
• How would we write a function that takes 3 values, and finds the average of the values?
Create the function definition starting off with def
Def average_function(x,y,z):
We named our function something easily recognizable, we created our 3 parameter variables x,y,z, and
ended with a :
15. AVERAGE FUNCTION
• def average_function(x,y,z):
Next we have to write our code! What would be the equation for finding the average of 3 numbers?
Return that equation back to the function.
Lets take a look at it in eclipse.
16. MAIN FUNCTION
• When defining and using functions in Python, it is good programming practice to place all statements
into functions, and to specify one function as the starting point
• • Any legal name can be used for the starting point, but we chose ‘main’ since it is the required function
name used by other common languages
• • Of course, we must have one statement in the program that calls the main function
17. MAIN FUNCTION
• def main():
• answer=average_function(2,5,8)
• print("The average is %.1f" % answer)
• def average_function(x,y,z):
• return (x+y+z)
Without the main function this would not execute because I have not yet defined the average function.
The main function allows the program to keep running until it defines the average function, thus executing
the block of code at the top.
18. Q&A
• Any questions about this chapter, powerpoint, previous lessons, or anything coding related, please ask!
If I do not know, or if one of us is not satisfied with our answer, I will consult with the professor and get
back to you.
• Remember, please give yourself multiple,multiple hours to do the zylabs. You need time to work on
them, you need time to work through being stuck for hours as well. Remember, we are learning, and
they are meant to be hard.
• You may be tempted to cheat, but if you are actually taking this class as your major, just think about
your future and what cheating on a lab will do for your career. Nothing. At. All. The labs are hard,
Computer Science is really hard. It gets easier. Find solace in the fact everyone is struggling with you,
and learning with you, and that while struggling now, 6 months from now you will look at some of these
labs and think they’re a piece of cake.