Skip main navigation

Functional Arguments

Consider the following example of a function: def say_hi(): return 'Hello and welcome to my program'print(say_hi()) So far, the function has used empty parentheses, but these parentheses serve an important purpose in many functions. The parentheses are used for passing arguments into a function. Arguments are basically values that you want to insert into a function. Functional arguments are a long topic, so be sure to work through it methodically and carefully.

Consider the following example of a function:

def say_hi():
 return 'Hello and welcome to my program'
print(say_hi())

 

So far, the function has used empty parentheses, but these parentheses serve an important purpose in many functions. The parentheses are used for passing arguments into a function. Arguments are basically values that you want to insert into a function. Functional arguments are a long topic, so be sure to work through it methodically and carefully.

 

If you would like your custom say_hi() function to address a specific person by name, you could supply their name in an argument. Try this code example:

 

def say_hi(name):
 return f'Hello and welcome to my program, {name}!'
print(say_hi('Sarah'))

 

Output:

 

Hello and welcome to my program, Sarah!

F-String Interpolation

In this case, you included an argument called name. Similarly to variable names, you can name arguments almost anything you want. Inside the function, you might notice something weird being returned. Don’t worry – that’s just a regular string. The function uses something called f-string interpolation. This has the same result as concatenation.

 

Replace the line in the example with this code and compare the output:

 

return 'Hello and welcome to my program. ' + name + '!'

 

F-string interpolation is simpler and more efficient when you need to print longer strings:

 

return f'Hello and welcome to my program, {name}!'

 

To use this approach, all you need to do is type the letter f in front of your string value. If you want to include any variables or other values in the string, simply refer to that value inside curly brackets. The example includes the name argument: {name}.

 

When you declare a function that uses arguments, you must provide all the arguments upon calling that function. For example:

 

print(say_hi('Sarah'))

 

As you can see, you provided a value for the name argument, and that value is ‘Sarah’.

 

Positional and Keyword Arguments

 

Each function can have a number of positional arguments and a number of keyword arguments. You would typically use keyword arguments to specify default values or optional arguments.

 

The main rule with functional arguments is that keyword arguments must follow positional arguments (if any). You can specify keyword arguments in any order, so you need only remember the keyword arguments’ names and not their positions.

 

As an example, this function adds two numbers together and provides the option of adding one or two additional numbers to the total as well. Try these code snippets to see how keyword arguments work:

 

def add_optional_bonus(num1,num2,opt1=0,opt2=0):
	return (num1+num2+opt1+opt2)

print(add_optional_bonus(5,6))
11
print(add_optional_bonus(5,6,opt2=5))
16
print(add_optional_bonus(5,6,opt2=6))
17
print(add_optional_bonus(5,6,opt2=6,opt1=5))
22

 

As you can see, num1 and num2 are positional arguments, whereas opt1 and opt2 are keyword arguments. If you put the keyword arguments before the positional arguments, it will result in an error, as in this code:

 

add_optional_bonus (opt2=6, opt1=5, 5,6)

 

Output:

 

File "<stdin>", line 1 
SyntaxError: positional argument follows keyword argument

 

Returning Multiple Values

 

One of the useful features that differentiates Python from Java and C++ is that you can return multiple values from a function. Try this code example:

 

def next_three_values(start_num):
 a = start_num+1
 b = start_num+2
 c = start_num+3 
 return a, b, c

x, y, z = next_three_values(5)
print(x,y,z) 

 

Output:

 

6 7 8

 

In this example, the function returns three values (a, b, c) to three variables (x, y, z) that are declared when calling the function.

This article is from the free online

Introduction to Programming with Python

Created by
FutureLearn - Learning For Life

Reach your personal and professional goals

Unlock access to hundreds of expert online courses and degrees from top universities and educators to gain accredited qualifications and professional CV-building certificates.

Join over 18 million learners to launch, switch or build upon your career, all at your own pace, across a wide range of topic areas.

Start Learning now