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

comp-Lecture 8 - Functions

The document provides an overview of functions in programming, including definitions, calling methods, parameters, and return statements. It explains concepts such as pseudocode, variable scope, and different types of arguments (positional, keyword, arbitrary, and default). Key points emphasize the importance of understanding function structure and variable visibility in programming.

Uploaded by

danieldenis6060
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

comp-Lecture 8 - Functions

The document provides an overview of functions in programming, including definitions, calling methods, parameters, and return statements. It explains concepts such as pseudocode, variable scope, and different types of arguments (positional, keyword, arbitrary, and default). Key points emphasize the importance of understanding function structure and variable visibility in programming.

Uploaded by

danieldenis6060
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

COMP-122

INTRODUCTION TO COMPUTER
PROGRAMMING

Lecture 8: Functions
OUTLINE
• Pseudocode
• Function Definition
• Function Call
• Parameters/Arguments
• Return statement
• Variable scope
Pseudocode
• Pseudocode, as the name suggests, is a false code or a representation of code which can be
understood by even a layman with some school level programming knowledge.
• Pseudocode is simply an implementation of an algorithm in the form of annotations and
informative text written in plain English. It has no syntax like any of the programming language
and thus can’t be compiled or interpreted by the computer.
• Algorithm is an organized logical sequence of the actions or the approach towards a particular
problem. A programmer implements an algorithm to solve a problem.
• The main goal of a pseudocode is to explain what exactly each line of a program should do,
hence making the code construction phase easier for the programmer.
• Pseudocode Example to calculate the area of a rectangle:
Functions
• A function is a named block of code that is designed to do one specific job.
• When you want to perform a particular task that you have defined in a function,
you call the name of the function responsible for it.
• If you need to perform that task multiple times throughout your program, you do
not need to type all the code for the same task again and again; you just call the
function dedicated to handling that task, and the call tells Python to run the code
inside the function.
• It is defined using the def keyword. The function name follows all variable
naming rules as previously covered
• Syntax:
Function Calling
• The function call must always come after the function
definition.
• To call a function, use the function name followed by
parenthesis:
Parameters/Arguments
• Information (any data type) can be passed into functions as arguments.
• Arguments are specified after the function name, inside the parentheses. You
can add as many arguments as you want, just separate them with a comma.
• The terms parameter and argument can be used for the same thing:
information that are passed into a function.
• NB:A parameter is the variable listed inside the parentheses in the function
definition while an argument is the value that is sent to the function when it is
called.
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.
Keyword Arguments
• A keyword argument is a name-value pair that you pass to a function.
• You directly associate the name and the value within the argument, so when
you pass the argument to the function, there’s no confusion.
• Keyword arguments free you from having to worry about correctly ordering
your arguments in the function call, and they clarify the role of each value in
the function call.
• Rewriting previous example using keyword arguments:
Arbitrary Arguments
• By default, a function must be called with the correct number of arguments.
• Meaning that if your function expects 2 arguments, you must call the function
with 2 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:
Keyword Arbitrary Arguments
• If you do not know how many keyword arguments that will be passed into your
function, add two asterisk: ** before the parameter name in the function
definition.
• This way the function will receive a dictionary of arguments, and can access
the items accordingly:
Default Arguments
• When writing a function, you can define a default value for each parameter.
• If an argument for a parameter is provided in the function call, Python uses
the argument value.
• If not, it uses the parameter’s default value. So, when you define a default
value for a parameter, you can exclude the corresponding argument you’d
usually write in the function call.
Points to note
• Default arguments should follow non-default arguments.
• Keyword arguments should follow positional arguments
• All the keyword arguments passed must match one of the arguments
accepted by the function and their order is not important.
• No argument should receive a value more than once.
• Default arguments are optional arguments.
The Return Statement
• A function doesn’t always have to display its output directly.
• Instead, it can process some data and then return a value or set of values of
any data type.
• The value the function returns is called a return value.
• The return statement takes a value from inside a function and sends it back to
the line that called the function.
Variable Scope
• Scope of a variable is the portion of a program where the variable is
recognized.
• Parameters and variables defined inside a function are not visible from outside
the function. Hence, they have a local scope.
Variable Scope Contd
• On the other hand, variables outside of the function are visible from inside.
They have a global scope.
• We can read these values from inside the function but cannot change (write)
them.
Variable Scope Contd
• In order to modify the value of variables outside the function, they must be
declared as global variables using the keyword global

You might also like