Updated April 18, 2023
Introduction to Kotlin optional parameter
Kotlin optional parameter is defined as the function which takes as the value it may be of any data type which is at the end of the argument list after any required parameters from the user if it’s needed the caller provides the argument for any of the succession of optional parameters and it must provide the arguments for all the preceding optional parameters whereas the number of parameters increases the method overloading can be quickly spiral out the control and lead with the more difficult for identifying the parameters support for various offers in the kotlin language.
Syntax
In kotlin language has many default concepts, methods, classes, and other behaviours like variables, method overloading and overriding to utilise the kotlin application. Generally, the kotlin function is called with the arguments passed and those arguments are used as the parameters.
fun funname(parameter1, parameter2)
{
--some codes—
}
fun main()
{
funname() // no value is passed in this method so the parameter is the optional one
}
The above codes are the basic syntax for utilising the functions with the different sets of arguments. And it depends on the requirement the function is called and utilised it on the main method.
How optional parameter work in Kotlin?
The optional parameter is used in various functions like that we can provide the default values to parameters in the function definition. If the function is called as the arguments passed then those arguments are used as the parameters with the function is called without passing the arguments and also the default arguments are used in the kotlin methods. Mainly the optional parameters are defined at the end of the parameter list after any required parameters from the kotlin method. If we can start by calling without any parameters then the template or greetings like errors and exceptions will be printed with both default and customized functions in the kotlin main method. The kotlin functions and constructors are defined with the default arguments and allowing calls to them to skip the arguments that have the default value and similar with the more concise way using the default parameters this is as much as simple and it has more sounds. Mainly kotlin supports the default arguments in the function declarations it specified the default value for the function parameters and it is used to corresponding to the argument that is omitted from the function call. The default functions cannot have specified the default values for the parameters specified in the base class functions and also it supports the customized views and parameters.
Example #1
Code:
fun firstdemo(strp1: String="Footprinting,HTTrack,Shodan,google hacking database,whois,Banner Grabbing,the Harvester[Pentest tool]", strp2: String="Footprinting Countermeasures,network scanning[nmap, Hping],ping command" , strids: Int=31) {
println("strp1 of the firstdemo is: $strp1")
println("strp2 of the firstdemo is: $strp2")
println("Your str ids are the firstdemo is: $strids")
}
fun demo2(strp3: String="wire sniffing,Rainbow Table,distributed network")
{
println("Medusa-password cracking tool")
println("privilege escalation,Malware Keyloggers")
println("Malware Keyloggers,Malware Rootkits[sniffers,backdoor,log,ddos]")
println("Malware Trojans,Virus[Self-replicating,host needed,data alteration/corruption]")
println("virus lifecycle:design,replication,launch,elimination,Malware Type of viruses")
println("Bootsector virus,malware worms,sniffing[monitor,captures,wiretap],Injecting packets")
println("ARP,MACflooding,DNS,ARC poisoning,sniffing attacks,sniffing tools")
}
fun demo4()
{
println("January is the Trojan month")
println("February is the malware month")
println("March is the worm month")
println("April is the Bootsector virus month")
println("May is the sniffing attacks month")
println("June is the Keyloggers month")
println("July is the Malware Rootkits month")
println("August is the Virus month")
println("September is the Session Hijacking month")
println("October is the DDOS month")
println("November is the ARP month")
println("December is the P2P month")
}
fun main(args: Array<String>) {
println("Welcome To My Domain its the first example that related to the kotlin optional parameters")
val str3 = "nmap,netdiscover,nslookup,dig,hping3,enumeration[NetBios][snmp,dns,smtp,ldap,smb][smbclient,netbios,dns]"
val str4 = "vulnerabilities-weakness,designflaws,vulnerability phases,tools,phases-pre,assessment,post vulnerability,Types of password attacks and defense,Bruteforce ,keylogger,hash injection,guessing"
val id3 = 45
firstdemo()
demo2("detection,incorporation")
demo4()
println("bandwidth attack-ddos attack, botnet floods with ICMP")
println("synflood attack -3 way handshake not completed, listen queue is 75 seconds")
println("ICMP Flood attack[ICMP Packets,Reflection networks],P2P,dereil,pandora,loic,hoic,doshttp,andosid -ddos tools")
println("Session Hijacking[TCP session],seesion sniffing,xss,mitm,network level session hijacking - hijacking protocol,IDS and Firewall Evasion Technique")
println("Fragmentaion,IP address spoofing,IP address spoofing,Source routing,Proxy servers,Encryption,Flooding,SSH tunneling,Encryption,WAF Detection with WAFW00F")
println("Gaining Remote Access,Web server Attack methodology,Information gathering(Recon),Google Dorking,Web Server Footprinting,Website Mirroring,Vulnerability Scanner")
println("Web Application Hacking Methodlogy,whois,Sql Injection attack,Error-based[UNION,Tautology,System Stored Procedure, End of Line Comment, Illegally/Logically Incorrect Query]")
println("Blind[Time delay, Boolean Exploitation],Commmand Injection Attack,Wireless,802.11 standard,Wireless Attacks,Evil Twin,Jamming")
println("Honeyspot,misconfiguration,Ad-Hoc connection,OWASP Top 10 for Mobile,Improper platform usage,insecure data storage,code tampering,Mobile Attacks,Bluesmacking,Bluebugging")
println("SMishing,Bluejacking,Bluesnarfing,Bluesniffing,BluePrinting,IOT hacking,IOT architecture,IOT Attacks,OT Concepts[Operational Technology]")
println("controlling robots,ICS,DCS,Cloud Environments,servers accessed via internet,software and databases,scalability,cost savings")
println("Cloud Threats,Geo-resilience,data centers,Cryptography,hash algorithm,Cryptography attacks,PTES,recon,system hacking,IOT and OT,web attacks,cryptography, cloud,mobile")
}
Sample Output:
In the above example, we used three different methods like firstdemo(), demo2() and demo4() to utilise the methods with the main function. Firstdemo() method we can pass the different arguments with both string parameters the variables like strp1, strp2 are two different string datatypes with values. The third parameter is strides as the integer type and we can pass the values. Here we printed the pentesting concepts in the main method.
Example #2
Code:
fun secondMethod(ch: Char = '=', len: Int = 17) {
for (i in 1..len) {
print(ch)
}
}
fun main(args: Array<String>) {
println("Welcome To My Domain its the second example that related to the kotlin optional parameters:")
secondMethod()
println("\n\n'+' is used as the first argument.")
println("Your first parameter is passed:")
secondMethod('+')
println("\n\n'+' is used as the first argument.")
println("41 is used as a second argument.")
println("The output arguments values are passed:")
secondMethod('@', 41)
}
Sample Output:
In the above example we used the same as the first example here I used a single method like secondMethod() with the same parameters like character type and integer type. In the method, we used for loop to iterate the values here the string character is iterated and also the length is measured. In the main method, we can call the method and print the values.
Conclusion
In kotlin language, we used to define and call the functions in both default and customized types. For these, we can use to call both defaults and named arguments for how to define and call the functions with a variable number of arguments to cover all the kotlin types of functions.
Recommended Articles
This is a guide to Kotlin Optional parameter. Here we discuss the introduction, syntax, and working of an optional parameter in Kotlin along with different examples and code implementation. You may also have a look at the following articles to learn more –