Functions in C, Python, & C++

Let's start with C. Please be remembered that most of the posts are posted for my purpose as I don't get much time. So, you will not be able to get the full picture of each topic from the posts though some might sound informative and coherent.

I always believe the power of the function is when it is only called. 


Function Definitions, Invocation, & Prototype in C 

Definition

<data type> function-name (<data-type> argument1, <data-type> argument2)

{                                                              

....                                                           

Blah...Blah...                                          

.....                                                          

}                                                             

Calling/Invocation

<data-type> variable = function-name(argument1, argument2)

Prototype

<data type> function-name (<data-type> argument1, <data-type> argument2)

Function Definitions in Python

Definition

def  function-name (argument1, argument2) :

      .....                                          

      Blah...Blah...                          

      .....                                          

Invocation

function-name(argument1, argument2)

Arbitrary Arguments 

If the number of arguments is unknown, we can add * before the argument and pass to the function in Python.

Definition

def  function-name (*argument) :

      .....                                          

      Blah...Blah...                          

      .....                                          

Invocation

function-name("argument1", "argument2", "argument3")

Keyword Arguments

Arguments can be passed as key = value syntax. In this case, the order doesn't matter. 

Definition

def  function-name (argument1, arguement2, argument3) :

      .....                                          

      Blah...Blah...                          

      .....                                          

Invocation

function-name(argument1 = "text1", argument2 = "text2", argument3 = "text3") 

Arbitrary Keyword Arguments

If we are not sure of how many keyword arguments to be passed into the function, we can add ** before the argument in the function definition. 

Definition

def  function-name (**argument) :

      .....                                          

      Blah...Blah...                          

      .....                                          

Invocation

function-name(argument1 = "text1", argument2 = "text2", argument3 = "text3") 

Default Parameter Value

If the function is called without an argument, it uses the default value. 

Definition

def  function-name (argument = "Shirley") :

      .....                                          

      Blah...Blah...                          

      .....                                          

Invocation

function-name()

Passing a list as an argument

If the function uses list as an argument, it will treated as list in the function definition.

Definition

def  function-name (names) :

      for x in names:                       

      print(x)                                  

      .....                                          

Invocation

names = ["Shirley", "Sweety", "Jessy"]
function-name(names) 

Return value

If the function uses list as an argument, it will treated as list in the function definition.

Definition

def  function-name (names) :

     return 5 * x                     

Invocation

print(function-name(names)

The pass statement

Function definition can't be empty, so if your intent to have empty function, don't forget to have pass statement

Definition

def  function-name () :

     pass                

Recursive Function

To be updated........

Definition

def  function-name () :

              

C++ will be updated when I learn... 


Reference 

w3schools


Comments