Python - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Python Functions

Python Functions

shape Description

A function is a piece of composed, reusable code that is utilized to play out a solitary, related activity. Functions give better seclusion to an application and a high level of code reusing. The following is the syntax. [c]def functionname( parameters ): "function_docstring" function_suite return [expression][/c]

Defining Function

shape Description

The following are the some rules need to be performed to define a function. The following is the syntax. [c]def <function_name>([parameters]): </function_name> [/c] The following is an example. [c]def sum(a,b):[/c]

Invoking Function

shape Description

Characterizing a function gives a name, indicates the parameters that should be incorporated into the function, structures the squares of code. The following is the syntax. [c]<function_name>(parameters) </function_name>[/c] The following is an example. [c]sum(x,y)[/c] Where sum is the name of the function and x,y are the arguments which are passed in the function definition. The following brief examle to understand the concept. [c]def printme( str ): "This prints a passed string into this function" print str return; # Now you can call printme function printme("Hello guys") printme("welcome to splessons")[/c] Now compile the code result will be as follows. [c] Hello guys welcome to splessons [/c]

Function Arguments

shape Description

In python functions can be called by using following ways.

Required arguments

At the point when the function call articulation must match the number and request of contentions as characterized in the capacity definition it is Positional Argument coordinating. The following is an example. [c]def printme( str ): "This prints a passed string into this function" print str return; printme()[/c] Now compile the code result will be as follows. [c]Traceback (most recent call last): File "python1.py", line 6, in <module> printme() TypeError: printme() takes exactly 1 argument (0 given)[/c]

Keyword arguments

Utilizing the Keyword Argument, the contention go in capacity call is coordinated with capacity definition on the premise of the name of the parameter. The following is an example. [c]def printme( str ): "This prints a passed string into this function" print str return; # Now you can call printme function printme( str = "Splessons")[/c] Now compile the code result will be as follows. [c]Splessons[/c]

Default Arguments

Default Argument is the contention which gives the default qualities to the parameters go in the capacity definition, in the event that esteem is not gave in the capacity call. The following is an example. [c]def printinfo( name, age = 35 ): "This prints a passed info into this function" print "Name: ", name print "Age ", age return; # Now you can call printinfo function printinfo( age=38, name="sachin" ) printinfo( name="sachin" )[/c] Now compile the code result will be as follows. [c]Name: sac Age 38 Name: sac Age 35[/c]

Summary

shape Key Points

  • The functions which are not bond to any name called as Anonymous Function.
  • The lamda is the keyword to create anonymous function.
  • The def keyword will not be used to create anonymous.