functions in c with examples

 what are functions in c

 Now that you simply should have learned about variables, loops, and conditional statements it's time to find out about functions. you ought to have a thought of their uses as we've already used them and defined one within the guise of main. Getchar is another example of a function. generally , functions are blocks of code that perform variety of pre-defined commands to accomplish something productive. you'll either use the built-in library functions otherwise you can create your own functions.


                                  


Functions that a programmer writes will generally require a prototype. a bit like a blueprint, the prototype gives basic structural information: it tells the compiler what the function will return, what the function are going to be called, also as what arguments the function are often passed. once I say that the function returns a worth , I mean that the function are often utilized in an equivalent manner as a variable would be. for instance , a variable are often set adequate to a function that returns a worth between zero and 4 .

For example:
1
2
3
 
#include /* Include rand() */
 
int a = rand(); /* rand may be a standard function that each one compilers have */
Do not think that 'a' will change randomly , it'll be set to the worth returned when the function is named , but it'll not change again.

The general format for a prototype is simple:
1
 
return-type function_name ( arg_type arg1, ..., arg_type argN );
arg_type just means the sort for every argument -- as an example , an int, a float, or a char. It's precisely the same thing as what you'd put if you were declaring a variable.

There are often quite one argument passed to a function or none in the least (where the parentheses are empty), and it doesn't need to return a worth . Functions that don't return values have a return sort of void. Let's check out a function prototype:
1
 
int mult ( int x, int y );
This prototype specifies that the function mult will accept two arguments, both integers, which it'll return an integer. don't forget the trailing semi-colon. Without it, the compiler will probably think that you simply try to write down the particular definition of the function.

When the programmer actually defines the function, it'll begin with the prototype, minus the semi-colon. Then there should be a block (surrounded by curly braces) with the code that the function is to execute, even as you'd write it for the most function. Any of the arguments passed to the function are often used as if they were declared within the block. Finally, end it all with a cherry and a closing brace. Okay, maybe not a cherry.

Let's check out an example program:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 
#include
 
int mult ( int x, int y );
 
int main()
{
 int x;
 int y;
 
 printf( "Please input two numbers to be multiplied: " );
 scanf( "%d", &x );
 scanf( "%d", &y );
 printf( "The product of your two numbers is %d\n", mult( x, y ) );
 getchar();
}
 
int mult (int x, int y)
{
 return x * y;
}
This program begins with the sole necessary include file. Next is that the prototype of the function. Notice that it's the ultimate semi-colon! the most function returns an integer, which you ought to always need to conform to the quality . you ought to not have trouble understanding the input and output functions if you've followed the previous tutorials.

Notice how printf actually takes the worth of what appears to be the mult function. what's really happening is printf is accepting the worth returned by mult, not mult itself. The result would be an equivalent as if we had use this print instead
1
 
printf( "The product of your two numbers is %d\n", x * y );
The mult function is really defined below main. Because its prototype is above main, the compiler still recognizes it as being declared, then the compiler won't give a mistake about mult being undeclared. As long because the prototype is present, a function are often used albeit there's no definition. However, the code can't be run without a definition albeit it'll compile.

Prototypes are declarations of the function, but they're only necessary to alert the compiler about the existence of a function if we do not want to travel ahead and fully define the function. If mult were defined before it's used, we could do away with the prototype--the definition basically acts as a prototype also .

Return is that the keyword wont to force the function to return a worth . Note that it's possible to possess a function that returns no value. If a function returns void, the return statement is valid, but as long as it doesn't have an expression. In other words, for a function that returns void, the statement "return;" is legal, but usually redundant. (It are often wont to exit the function before the top of the function.)

The most important functional (pun semi-intended) question is why can we need a function? Functions have many uses. for instance , a programmer may have a block of code that he has repeated forty times throughout the program. A function to execute that code would save an excellent deal of space, and it might also make the program more readable. Also, having just one copy of the code makes it easier to form changes. Would you rather make forty little changes scattered all throughout a potentially large program, or one change to the function body? So would I.

Another reason for functions is to interrupt down a posh program into logical parts. for instance , take a menu program that runs complex code when a menu choice is chosen . The program would probably best be served by making functions for every of the particular menu choices, then breaking down the complex tasks into smaller, more manageable tasks, which might be in their own functions. during this way, a program are often designed that creates sense when read. And features a structure that's easier to know quickly. The worst programs usually only have the specified function, main, and fill it with pages of jumbled code.If you any have doubt,suggestion regarding this post or website then feel free to share with us.

 
If you have learned something new from this post then share this post with your family and
friends.
 
Happy Learning :)😎  

 


Previous

Post a Comment

Previous Post Next Post