Skip to main content
Question

Can user defined functions return a Number?

  • September 20, 2024
  • 2 replies
  • 33 views

For a user defined function as follows, can it be used to return a number or does it only return a Bool value?

 

threshold : Number = 100;
hasManySubInterfaces(iface) : Bool = length(iface.subinterfaces) > threshold;

 

Say something like the following, is that possible?

 

threshold : Number = 100;
hasManySubInterfaces(iface) : Number= length(iface.subinterfaces) + threshold;

2 replies

Andreas
Employee
  • Employee
  • September 20, 2024

Hi @pjptsujit ,

Yes, functions can return any type of data.

Your example function should work. For example, you can use it in a query like this:

threshold : Number = 100;

hasManySubInterfaces(iface) : Number = length(iface.subinterfaces) + threshold;

foreach d in network.devices
foreach i in d.interfaces
select { myNumber: hasManySubInterfaces(i) }

 


Forum|alt.badge.img+2
  • Employee
  • September 20, 2024

You also don’t necessarily need to specify the return type. 

 

threshold : Number = 1;

greaterThanThreshold(iface) = length(iface.subinterfaces) > threshold;
plusThreshold(iface) = length(iface.subinterfaces) + threshold;

foreach device in network.devices
foreach interface in device.interfaces
select {
length: length(interface.subinterfaces),
greaterThan: greaterThanThreshold(interface),
plusThreshold: plusThreshold(interface)
}