Computer Programming-Functions
Computer Programming-Functions
A function is a block of organized, reusable code that is used to perform a single, related
action. Functions provide better modularity for your application and a high degree of code
reusing. You have already seen various functions like printf() and main(). These are called
built-in functions provided by the language itself, but we can write our own functions as well
and this tutorial will teach you how to write and use those functions in C programming
language.
Good thing about functions is that they are famous with several names. Different
programming languages name them differently, for example, functions, methods, sub-
routines, procedures, etc. If you come across any such terminology, then just imagine about
the same concept, which we are going to discuss in this tutorial.
Let's start with a program where we will define two arrays of numbers and then from each
array, we will find the biggest number. Given below are the steps to find out the maximum
number from a given set of numbers −
#include <stdio.h>
main() {
When the above code is compiled and executed, it produces the following result −
If you are clear about the above example, then it will become easy to understand why we
need a function. In the above example, there are only two sets of numbers, set1 and set2, but
consider a situation where we have 10 or more similar sets of numbers to find out the
maximum numbers from each set. In such a situation, we will have to repeat, processing 10
or more times and ultimately, the program will become too large with repeated code. To
handle such situation, we write our functions where we try to keep the source code which will
be used again and again in our programming.
Now, let's see how to define a function in C programming language and then in the
subsequent sections, we will explain how to use them.
Defining a Function
The general form of a function definition in C programming language is as follows −
return [expression];
}
Return Type − A function may return a value. The return_type is the data type of
the value the function returns. Some functions perform the desired operations without
returning a value. In this case, the return_type is the keyword void.
Function Name − This is the actual name of the function. The function name and the
parameter list together constitute the function signature.
Parameter List − A parameter is like a placeholder. When a function is invoked, you
pass a value as a parameter. This value is referred to as the actual parameter or
argument. The parameter list refers to the type, order, and number of the parameters
of a function. Parameters are optional; that is, a function may contain no parameters.
Function Body − The function body contains a collection of statements that defines
what the function does.
Calling a Function
While creating a C function, you give a definition of what the function has to do. To use a
function, you will have to call that function to perform a defined task.
Now, let's write the above example with the help of a function −
#include <stdio.h>
int i, max;
max = set[0];
i = 1;
while( i < 5 ) {
max = set[i];
}
i = i + 1;
}
return max;
}
main() {
When the above code is compiled and executed, it produces the following result −
Functions in Java
If you are clear about functions in C programming, then it is easy to understand them in Java
as well. Java programming names them as methods, but the rest of the concepts remain more
or less same.
Following is the equivalent program written in Java. You can try to execute it to see the
output −
int i, max;
max = set[0];
i = 1;
while( i < 5 ) {
return max;
}
}
Functions in Python
Once again, if you know the concept of functions in C and Java programming, then Python is
not much different. Given below is the basic syntax of defining a function in Python −
return [expression]
Using this syntax of function in Python, the above example can be written as follows −