Last Updated:

Python Function Arguments

Python Function Arguments

Today we will consider one of the most important parts of not only the Python programming language, but also programming in principle. Of course, we are talking about functions; about how they are created, what they are needed for, how to use them and not only. The topic is really very important, so you need to study it thoroughly.

def (function/method)

A special piece of code that can be accessed repeatedly is called a function or method. In fact, the function and method are the same, only the names differ, but this is the topic of future lessons. So, certain values are passed to the function, which are called arguments (they may not be passed), it processes everything and returns the result (it may not return).

Functions and their arguments

Arguments are those values that are passed to a function. They may or may not be mandatory. A small note: optional arguments must be the most recent. Let's still look at examples.

Recipe for creating a function in python

The recipe for creating a function in a python is quite simple:

  1. First you need to specify the keyword def.
  2. Next, enter the names of the function, in parentheses the arguments that it will take and put a colon.
  3. All the code that will follow is called the function body.

Creating Functions

An example of creating and using a function:

defsum(a, b):
return a + b

To call it in the code, you need to specify the name and write the arguments in parentheses.

print(sum(1,2)) => 3

Sample code with an optional argument:

defmultiply(a,b,c = 4):
return a * b * c

C is an optional argument. If we don't pass it in, the default value (in this case, 4) will be substituted. And the function call is exactly the same.

Basic built-in functions in Python

 

Python developers care about us and therefore have created an impressive number of ready-made functions. We will consider only a part of them.

print() is the most popular and common function that outputs a value to the console.
len() — returns the number of items that are stored in a dictionary, tuple, or list.
str(), int(), bool() — they respectively lead the object to a string, integer, or Boolean type. About int() it is still worth saying that if you pass a string - there will be an error, and if a number with a floating point, then only the whole part will remain, and the rest will be discarded.
min(), max() — return the minimum and maximum element that is passed to them.
sum() — returns the sum of the elements.
type() — displays the type of the object.

Function with a variable number of arguments

If you want to create a function that will take a different number of arguments, and this is very often useful, then you need to put the * sign before its name. It turns out that we create a list inside the function, just write it in a more convenient way. Let's look at an example:

def f(*args):
for i inargs:
print(i * 2)
f(1,2,3) => 2,4,6

The argument is usually called args, although this is not necessary. Next, in the loop, just sort through each element and multiply it by 2.

In addition to lists, we can transfer dictionaries. To do this, you just need to register two stars, and everything else remains the same.

Return value of the function (return)

 

We have already come across the word return, which returns some value. But there are functions that return nothing. They can perform some other actions, for example, write text to a file, read some values and much more.

Anonymous function: lambda

Lambda functions are, as a rule, short functions for which it makes no sense to add a real one. They are written in a single line and the syntax is as follows:

>>lam = lambda a, b: a + b
>>>lam(1, 2)
3
>>> (lambda a, b: a + b)(1, 2)
3

Empty function (stub)

In Python, you can create empty functions that do nothing at all, and are usually used when the programmer does not want to fill it with code yet, but does not want to forget to do it later, so he creates it.

deftest():
pass

For such purposes, a "stub" pass is used.

Recursive functions

This is a technique that is widely used not only in programming, but in ComputerScience as a whole. What is it? Recursion is when a function calls itself. The easiest way to show this is by the example of finding the Fibonaci number.

deffunc(x):
if x == 0:
return 1
returnfunc(x-1) * x

Here's what it looks like. But it is worth using this method only in rare cases, because firstly it is not optimal, and secondly recursion is limited. If you endlessly try to call it, then an emergency exit from the program will occur.

Scope

 

One of the main points of any language is the scope. Specifically in the python there are 2 areas: local and global. Global are those that are created inside the module and are available everywhere, and local ones are those that are created in the body of the function and are available only for it. Outside of a function, you cannot call a local variable.

Python Proficiency Test

 

Well, in conclusion, I recommend taking a test and answering questions to further consolidate the topic.

  1. What is a function and how is it created?
  2. deff(a, b x = 2): return a + b print(f(2,5)). What will be the result? (a) 6 (b) 7 (c) Error (d) 9
  3. What keyword do I need to specify to put a stub for the function?
  4. deffunc(f): if f == 0: return 1 returnfunc(f+1) * f print(f(2)). What will happen in this case? (a) 3 (b) Error (c) 6 (d) 4
  5. What are scopes and what are they for?
  6. What does lambda syntax look like?
  7. Choose the superfluous. a) print() b) multiply() c) int() d) type()

Today was a very voluminous topic that you definitely need to know. So right now, after reading it, go and practice. Create a simple function and try to work with it. Practice is our everything! Good luck!