Defining and calling functions in C# vs Python

Similar to how we store and label the data that we need several times throughout our program, using variables and variable names; we can also label a block of code by putting it into a function. We can then call the block of code simply by the function name, instead of rewriting the same code over and over again, every time we need it.

Suppose we are writing a program, in which we often need to compare two integers and determine which one is greater than the other. Lets assume we have two integers stored in variables num1 and  num2 and that the integers cannot be equal. We can then write a simple if-else statement to compare the integers.

C# code snippet

Python code snippet

So far, so good. But what if we have to make such comparison of two integers at ten different places in our program? We can either copy and paste the above code ten times, which will result in an unnecessarily long and messy code, or we can create a function that takes two integers as input and provides the greater integer in return.

Function definition in Python

The general structure of a function definition in Python is depicted in the image below. Python functions are defined using the  def keyword followed by the function name. The inputs (called input parameters) are separated by commas and appear inside parenthesis after the function name.


Python function definition in general

Given the general structure, we can now place our code snippet to determine the greater out of two integers inside a function.

Here getMax() is the name we chose for our function and first_num and second_num are names of our input parameters. The function simply compares the two input parameters and updates and returns the variable  greater (called output parameter) using the  return statement. Note that the function body in Python must be indented similar to the body of a loop or an if-statement.

Function definition in C#

The general structure of a function definition in C# can be seen in the image below. C# requires the data-types for the input and output parameters in the function definition.


C# function definition in general

Since the input and output parameters are all integers, we must declare their data-types using the  int keyword. If a function does not have an output parameter, the keyword  void is used before the function name.

The keyword  static will be discussed in a future lesson.

Calling a function

Let us now write the complete program with the getMax() function definition and function calls.

C# code


The function is simply called by its name with the inputs to the function (called function arguments) within parenthesis. The difference between function arguments and function parameters should be re-emphasized here. Function arguments are the inputs to the function as provided in the function call while function parameters are the input and output variables named in the function definition for use internally within the function. So when the getMax() function is called with arguments  num1 and  num2, the parameter  first_num gets the value of argument  num1 and the parameter  second_num gets the value of argument  num2.

Python code

As our function returns a parameter of type  int, we must have an integer variable (the variables greater1, greater2 and greater3) on the left hand side of the statement when the function is called. If the function did not return any parameter, it would be called as:

The Main() function in C#

Now that we have understood how functions are defined, we can easily see that in C#, the statement  static void Main() , followed by the curly braces is nothing more than the definition of a function named ‘Main’ with the return type void. Our Test  class now has two functions, the Main() function which is mandatory in any C# program and our function getMax(). The Main() function calls the getMax() function three times. The order in which we define these functions does not matter. The program execution begins with the Main() function and the getMax() function can be called by Main() as long as they are in the same class.

On the contrary, Python programs do not need a main function. The execution of code in Python is from top to bottom. Therefore, any function call must be after (or below) the function definition.

Returning multiple values using tuples

Suppose we want to write a function that takes two integers as input and returns their sum and their product. We can return multiple values from a function by combining them into a new data type called tuple. 

C# code

A tuple is declared in C# using the keyword  Tuple with angled brackets <int, int> containing the data types that constitute the tuple, separated by commas. Since the return type of this function is a tuple made of two integers, we must write  Tuple <int, int> before the function name as well. The tuple returned from the addMultiply() function is stored in the Main() function in another tuple called  result. We can then access each item in the tuple using the dot . operator.

Python, as always, does not require explicit declaration of data types. A tuple can be initialized simply by assigning comma separated variables to a new variable.

Python code

The items of a tuple are accessed in Python using brackets and the item’s index.

What we learnt

  1. If a block of code is required several times in a program, it should be put into a function.
  2. In Python, a function definition must precede any calls to that function.
  3. Functions can have input and output parameters. The calling function sets values of input parameters using arguments in the function call. The output parameter is returned using  return statement.
  4. Functions can return multiple values using tuples.

< Print multiplication table using while loop              Classes >



You may also like: