Defining Functions

Passing Parameters

For each in parameter of a Slice operation, the Python mapping produces a corresponding parameter for the method in the skeleton. Additionally, every operation has a trailing parameter of type Ice.Current. For instance, the name operation of the Node interface does not have any parameters. However, the name method in a Python servant has a current parameter. In other words, this article will tell you everything you need to know about passing parameters.

passing parameters

Parameter passing on the server side follows the rules for the client side. Further, an operation returning multiple values returns them in a tuple comprising of a non-void return value, if any, followed by the out parameters in the sequence of declaration. Moreover, an operation returning only one value basically returns the value itself.

Further, an operation returns numerous values when it declares multiple out parameters, or when it declares a non-void return type and no less than one out parameter. Thus, the following interface which passes string parameters in all possible directions will make it clearer:

Slice

                    

interface Example {

string op1(string sin);

void op2(string sin, out string sout);

string op3(string sin, out string sout);

};

 

The generated skeleton class for this interface looks like below:

Python

                    

class Example(Ice.Object):

def __init__(self):

# ...

#

# Operation signatures.

#

# def op1(self, sin, current=None):

# def op2(self, sin, current=None):

# def op3(self, sin, current=None):

Browse more Topics Under Defining Functions

Default Parameters

If a parameter specified in Python function definition has the form <name>=<value>, then <value> will become a default value for that parameter. Similarly, we call these parameters defined this way as default or optional parameters.  For instance:

                    

class Example(Ice.Object):

def __init__(self):

# ...

#

# Operation signatures.

#

# def op1(self, sin, current=None):

# def op2(self, sin, current=None):

# def op3(self, sin, current=None):

Keyword-Only Arguments

A Python function in version 3.x can be defined so that it takes keyword-only arguments. Further, these refer to function arguments that must be specified by keyword. For instance, if you wish to write a Python functions which takes a variable number of string arguments, concentrates them together separated by a dot (.) and prints them to the console. Consequently, you need something like this for starting:

                    

>>>

>>> def concat(*args):

...     print(f'-> {".".join(args)}')

...

>>> concat('a', 'b', 'c')

-> a.b.c

>>> concat('foo', 'bar', 'baz', 'qux')

-> foo.bar.baz.qux

Certainly, the output prefix is hard-coded to the string '-> '. Thus, now if you wish to modify the function for accepting this as an argument as well, can the user specify something else? To clarify, given below is one possibility:

>>>

>>> def concat(prefix, *args):

...     print(f'{prefix}{".".join(args)}')

...

>>> concat('//', 'a', 'b', 'c')

//a.b.c

>>> concat('... ', 'foo', 'bar', 'baz', 'qux')

... foo.bar.baz.qux

Above all, please remember some undesirable things about this solution that the prefix string is lumped together with the strings to be concatenated. Thus, by looking at the function call, it does not become clear that the first argument is treated differently from the rest. In conclusion, the prefix is not optional, it has to be included always. Therefore, bringing passing parameters to an end.

FAQ on Passing Parameters

Question 1: What are the default arguments in python?

Answer 1: Default arguments in Python functions refer to those arguments that take default values. Moreover, it is if no explicit values pass to these arguments from the function call.

Question 2: What are the parameters and arguments in python?

Answer 2: We use the terms of parameter and argument for the same thing. Moreover, it is information that passes into a function. In other words, a parameter is a variable that lists inside the parentheses in the function definition. On the other hand, an argument is the value sent to the function when it is called.

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

Your email address will not be published. Required fields are marked *

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.