File tree Expand file tree Collapse file tree 1 file changed +47
-0
lines changed
Expand file tree Collapse file tree 1 file changed +47
-0
lines changed Original file line number Diff line number Diff line change 1+ # Diving into functions
2+
3+ """ 1. A function is defined by the keyword def. The syntax of function is:
4+ def function_name( parameters ):
5+ executable statements
6+ return
7+
8+ 2. A function is called by simply writing the function name followed by parebthesis. Syntax:
9+ function_name (passed_arguments)
10+ """
11+
12+
13+ # A Basic Function Example
14+
15+ # A simple function catching a string and printing it
16+ def printing (name ):
17+ print ("The name is " ,name ,"\t Codex" ,name )
18+ printing ("ITER" )
19+
20+
21+ # A simple add function that takes two variables x and y and returns the summation of x and y
22+ def add (x ,y ):
23+ return (x + y )
24+ ans = add (2 ,3 )
25+ print (" 2 + 3 =" ,ans )
26+
27+
28+ # A function that can catch unknown number of arguments and store it in form of a list
29+ def many_args (* args ):
30+ for i in args :
31+ print (i )
32+ many_args (1 ,2 ,"codex" ,40.76 )
33+
34+
35+
36+ # function with default value
37+ def example (name = "codex" ):
38+ print (name )
39+ example ("ITER" ) # here the value of name will be changed to ITER
40+ example () # here as no value is passed the default value will be the output ,i.e, codex
41+
42+
43+ # function with keyword argument
44+ def example2 ( name , year ):
45+ print ("Name is: " ,name )
46+ print ("Year is: " ,year )
47+ example2 ( year = 2017 ,name = "CODEX" ) # here the arguments are passed out of order but still it will catch perfectly as keywords are used
You can’t perform that action at this time.
0 commit comments