What does it mean to have a function in code?
Computer Programming - Functions
A part is a block of organized, reusable code that is used to perform a single, related action. Functions provide improve modularity for your application and a loftier degree of lawmaking reusing. You have already seen diverse functions like printf() and main(). These are called built-in functions provided past the language itself, but we can write our own functions as well and this tutorial volition teach you how to write and utilize those functions in C programming language.
Proficient thing nearly functions is that they are famous with several names. Dissimilar programming languages name them differently, for example, functions, methods, sub-routines, procedures, etc. If you come up across any such terminology, then just imagine about the same concept, which we are going to discuss in this tutorial.
Allow's start with a plan where we will define two arrays of numbers and then from each array, nosotros volition find the biggest number. Given below are the steps to detect out the maximum number from a given gear up of numbers −
1. Get a list of numbers L1, 502, Lthree....50N two. Assume Fifty1 is the largest, Set up max = Li 3. Take next number Li from the listing and do the following 4. If max is less than Li five. Set max = Fiftyi half-dozen. If Li is last number from the list so 7. Impress value stored in max and come out 8. Else prepeat aforementioned procedure starting from stride three
Allow'due south translate the above program in C programming language −
#include <stdio.h> int main() { int set1[5] = {x, twenty, thirty, 40, 50}; int set2[5] = {101, 201, 301, 401, 501}; int i, max; /* Process start prepare of numbers available in set1[] */ max = set1[0]; i = 1; while( i < v ) { if( max < set1[i] ) { max = set1[i]; } i = i + i; } printf("Max in first set = %d\due north", max ); /* At present procedure second set of numbers available in set2[] */ max = set2[0]; i = 1; while( i < v ) { if( max < set2[i] ) { max = set2[i]; } i = i + 1; } printf("Max in 2d set = %d\n", max ); } When the above lawmaking is compiled and executed, it produces the following result −
Max in outset set = fifty Max in second set = 501
If y'all are clear about the above example, and then it will become easy to sympathize why we need a function. In the above instance, at that place are only two sets of numbers, set1 and set2, merely consider a state of affairs where we have 10 or more similar sets of numbers to detect 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 big with repeated lawmaking. To handle such situation, we write our functions where we attempt to proceed the source code which will be used again and again in our programming.
Now, let's run into 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 grade of a role definition in C programming language is every bit follows −
return_type function_name( parameter list ) { trunk of the function return [expression]; } A part definition in C programming consists of a function header and a function body. Here are all the parts of a function −
-
Return Type − A function may return a value. The return_type is the data blazon 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.
-
Role Name − This is the actual proper name of the function. The function name and the parameter listing together institute the office signature.
-
Parameter List − A parameter is like a placeholder. When a office is invoked, yous pass a value as a parameter. This value is referred to equally 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 office may contain no parameters.
-
Office Body − The part body contains a collection of statements that defines what the office does.
Calling a Office
While creating a C role, y'all give a definition of what the function has to do. To employ a function, you will have to call that function to perform a divers task.
Now, let'due south write the to a higher place case with the assist of a function −
#include <stdio.h> int getMax( int fix[] ) { int i, max; max = prepare[0]; i = 1; while( i < 5 ) { if( max < set[i] ) { max = prepare[i]; } i = i + 1; } return max; } principal() { int set1[5] = {x, 20, 30, 40, 50}; int set2[v] = {101, 201, 301, 401, 501}; int max; /* Process first set of numbers available in set1[] */ max = getMax(set1); printf("Max in first set = %d\n", max ); /* Now process 2nd gear up of numbers available in set2[] */ max = getMax(set2); printf("Max in 2nd ready = %d\north", max ); } When the to a higher place code is compiled and executed, it produces the following consequence −
Max in kickoff prepare = 50 Max in 2d fix = 501
Functions in Java
If you are clear about functions in C programming, then it is easy to understand them in Java also. Java programming names them as methods, but the rest of the concepts remain more than or less same.
Following is the equivalent program written in Java. You can try to execute it to see the output −
public class DemoJava { public static void main(Cord []args) { int[] set1 = {10, 20, 30, 40, l}; int[] set2 = {101, 201, 301, 401, 501}; int max; /* Process offset set of numbers available in set1[] */ max = getMax(set1); System.out.format("Max in first set = %d\n", max ); /* Now procedure second gear up of numbers available in set2[] */ max = getMax(set2); System.out.format("Max in 2nd set = %d\n", max ); } public static int getMax( int gear up[] ) { int i, max; max = set[0]; i = 1; while( i < 5 ) { if( max < set[i] ) { max = gear up[i]; } i = i + 1; } return max; } } When the above program is executed, it produces the post-obit consequence −
Max in first set = fifty Max in second gear up = 501
Functions in Python
Once once again, if yous know the concept of functions in C and Coffee programming, then Python is not much different. Given below is the basic syntax of defining a function in Python −
def function_name( parameter list ): body of the function return [expression]
Using this syntax of part in Python, the above example can be written as follows −
def getMax( prepare ): max = set up[0] i = i while( i < v ): if( max < set[i] ): max = ready[i] i = i + one return max set1 = [10, xx, 30, 40, l] set2 = [101, 201, 301, 401, 501] # Process first fix of numbers available in set1[] max = getMax(set1) print "Max in showtime prepare = ", max # Now process second fix of numbers bachelor in set2[] max = getMax(set2) impress "Max in 2d gear up = ", max
When the to a higher place lawmaking is executed, it produces the following result −
Max in kickoff set = 50 Max in 2nd set = 501
Useful Video Courses
Video
Video
Video
Video
Video
Video
Source: https://www.tutorialspoint.com/computer_programming/computer_programming_functions.htm
0 Response to "What does it mean to have a function in code?"
Post a Comment