Skip to main content

Things you must know before coding

wanna keen to learn to code but still confused about where to start ? Don't worry you are at the right blog, I'll be sharing from very basic concepts of coding. weather you have a technical background or not it doesn't matter just stay tuned with my blog, I can bet within a couple of days you will be able to write your own code and with weeks you will start thinking about logic how to solve any problem. Everything you need to know about programming  Programming is one of the top leading jobs in the current market, and it's growing every day. It's becoming more and more crucial to have a programmer in your company. And if you take into account that all the biggest companies in the world (Facebook, Twitter, Amazon etc.) are tied to programming, you'll understand the importance of it. That's why we'll try to teach you everything you need to know about programming.Basically, programming is defined as a process of developing and implementing various sets of i

 

Functions


 

Syntax

Functions are a construct to structure programs. They are known in most programming languages, sometimes also called subroutines or procedures. Functions are used to utilize code in more than one place in a program. The only way without functions to reuse code consists in copying the code.

A function in Python is defined by a def statement. The general syntax looks like this:

def function-name(Parameter list):
    statements, i.e. the function body
The parameter list consists of none or more parameters. Parameters are called arguments, if the function is called. The function body consists of indented statements. The function body gets executed every time the function is called.
Parameter can be mandatory or optional. The optional parameters (zero or more) must follow the mandatory parameters.

Function bodies can contain a return statement. It can be anywhere in the function body. This statement ends the execution of the function call and "returns" the result, i.e. the value of the expression following the return keyword, to the caller. If there is no return statement in the function code, the function ends, when the control flow reaches the end of the function body.
Example:
>>> def add(x, y):
...     """Return x plus y"""
...     return x + y
... 
>>> 
In the following interactive session, the function we previously defined will be called:
>>> add(4,5)
9
>>> add(8,3)
11
>>>

Example of a Function with Optional Parameters

>>> def add(x, y=5):
...     """Return x plus y, optional"""
...     return x + y
... 
>>> 
Calls to this function could look like this:
>>> add(4)
9
>>> add(8,3)
11
>>> 

Docstring

The first statement in the body of a function is usually a string, which can be accessed with function_name.__doc__
This statement is called Docstring.
Example:

>>> execfile("function1.py")
>>> add.__doc__
'Returns x plus y'
>>> add2.__doc__
'Returns x plus y, optional'
>>>

Keyword Parameters

Using keyword parameters is an alternative way to make function calls. The definition of the function doesn't change.
An example:

def sumsub(a, b, c=0, d=0):
    return a - b + c - d
Keyword parameters can only be those, which are not used as positional arguments.
>>> execfile("funktion1.py")
>>> sumsub(12,4)
8
>>> sumsub(12,4,27,23)
12
>>> sumsub(12,4,d=27,c=23)
4

Arbitrary Number of Parameters

There are many situations in programming, in which the exact number of necessary parameters cannot be determined a-priori. An arbitrary parameter number can be accomplished in Python with so-called tuple references. An asterisk "*" is used in front of the last parameter name to denote it as a tuple reference. This asterisk shouldn't be mistaken for the C syntax, where this notation is connected with pointers.
Example:

def arbitrary(x, y, *more):
    print "x=", x, ", y=", y 
    print "arbitrary: ", more
x and y are regular positional parameters in the previous function. *more is a tuple reference.
Example:
>>> execfile("funktion1.py")
>>> arbitrary(3,4)
x= 3 , x= 4
arbitrary:  ()
>>> arbitrary(3,4, "Hello World", 3 ,4)
x= 3 , x= 4
arbitrary:  ('Hello World', 3, 4)

Comments

Popular posts from this blog

Things you must know before coding

wanna keen to learn to code but still confused about where to start ? Don't worry you are at the right blog, I'll be sharing from very basic concepts of coding. weather you have a technical background or not it doesn't matter just stay tuned with my blog, I can bet within a couple of days you will be able to write your own code and with weeks you will start thinking about logic how to solve any problem. Everything you need to know about programming  Programming is one of the top leading jobs in the current market, and it's growing every day. It's becoming more and more crucial to have a programmer in your company. And if you take into account that all the biggest companies in the world (Facebook, Twitter, Amazon etc.) are tied to programming, you'll understand the importance of it. That's why we'll try to teach you everything you need to know about programming.Basically, programming is defined as a process of developing and implementing various sets of i