C is one of the most popular and powerful programming languages. Did you know that C code can be edited and run by a C++ IDE and compiler? The C language has many pre-defined variables, functions, and libraries – and while this makes it very powerful it can also be a steep learning curve for new programmers. Using a fast and reliable C or C++ compiler for Windows is very important for beginners and professionals since it helps C/C++ developers in remembering which language features exist, how to use them, and even detect errors when we get them wrong which helps beat that barrier to learning and improving.
Table of Contents
How to write a function declaration in C programming?
In C and C++, functions can be declared before the usage part or after the usage part. A function can be declared by function name and parenthesis ( ) and { }. If the function has a return type (int, float, …) this should be declared before the function name, in other words this return type is the type of the function. If function has parameters, these can be added between (
and )
separated with comas. Then all the calculations and operations of the function should inside {
and }
, this is the body of the function. If the function has return type, this type of variable should be returned at the end of the body of the function. Note that function name can not be a name used in C/C++. Here is a general function declaration.
Syntax for a general function declaration:
1 2 3 4 5 |
<return type> <function_name>( <parameter1 type> <parameter1>, <parameter2 type> <parameter2>, ... ) { // function operations here return <variable returned>; } |
Here is an example for a general function declaration:
1 2 3 4 5 |
int myf( int x, int y) { int z = x+y; return z; } |
here we declare a myf() function which returns integer value and has 2 integer parameters x, y. Inside this function we just create and calculate a int z variable, then we used value of this variable as a return value. Or you can directly return value of calculation as below,
1 2 3 4 |
int myf( int x, int y) { return (x+y); } |
this function is faster than before, because function doesn’t spend time to declare z variable. In other words, every command line is important inside a function. Optimize it as much as possible.
How to use a function in C programming?
Syntax for a general function usage:
1 |
<variable> = <function_name>( <parameter1>, <parameter2>, .... ); |
here is an example that how we use a function.
1 |
int a = myf(3, 5); |
Here we create a new int
variable a
to obtain return value and we used 3 and 5 as parameters of this function. Instead of a variable you can directly print out this return value, or you can use this function where int
parameters are needed.
An example of how to write a function in C programming?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include <stdio.h> int myf( int x, int y) { return (x,y); } int main() { int a = myf(3, 5); getchar(); return 0; } |
How to write a simple function declaration in C programming?
If function has no return type, and no parameters, it is a simple function and you don’t need to use return type. Generally, in most C compilers, you can start with function name or you can use void
term before its name, to declare that this function has no return type.
Syntax for a simple function declaration:
1 2 3 4 |
<function_name>() { } |
or generally we use void as below,
1 2 3 4 |
void <function_name>() { } |
1 2 3 4 |
myf() { printf("Hello LearnCPlusPlus.org\n"); } |
Here is a void function declaration example,
How to use a simple function in C programming?
In usage, we use function name and its parameters, let’s see syntax and usage;
Syntax for a general function usage:
1 |
<variable> = <function_name>( <parameter1>, <parameter2>, .... ); |
here is an example that how we use a function.
1 |
myf(); |
An example of how to write a simple function in C programming?
Here is a simple function example in C programming,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <iostream> myf() { printf("LearnCPlusPlus.org\n"); } int main() { myf(); getchar(); return 0; } |
What is a void function in C programming?
In C and C++ programing, the void term means “no value is returned”. In math, a function returns a value, i.e. y = f (x); Here f(x) is a function that works with variable x and y is the output of this function. Note that, in English, void term means “completely empty” or “not valid or legally binding”.
In programming, generally a function returns a value, but some functions do not need to return a value. These functions can carry out all sorts of operations and run many other functions inside but when it is over it should not have to return a value. We call these functions “void functions”. To define a void function, we should start with the keyword void
which represents this function has no possible return value.
How to write a void function declaration in C programming?
For example, we want to create some code that promotes the LearnCPlusPlus site on the screen we might decide we don’t really need a return value from this function. In C programming, you don’t need to define as void, for example this is a void function in C language,
Syntax for a void function declaration:
1 2 3 4 5 |
<return type> <function_name>( <parameter1 type> <parameter1>, <parameter2 type> <parameter2>, ... ) { // function operations here return <variable returned>; } |
Here is an example for a void function declaration with parameters:
1 2 3 4 |
void myf( int x, int y) { printf(("x:%d y:&d", x, y); } |
For the compatibility, we highly recommend you to use void term for all C and C++ function definitions.
How to use a void function in C programming?
In usage, as same above, we use function name and its parameters, let’s see syntax and usage;
Syntax for a void function usage:
1 |
<function_name>( <parameter1>, <parameter2>, .... ); |
We can easily use this void function as below,
here is an example that how we use a function.
1 |
myf(3,5); |
An example of how to write a void function in C programming
At the end of void
functions, you don’t need to (and shouldn’t) use the return
command as there is no return value. Even without the return statement, control will return to the caller automatically at the end of the function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <iostream> void advert() { std::cout << "LearnCPlusPlus.org\n"; } int main() { advert(); getchar(); return 0; } |
Can void functions in C and C++ have parameters?
Yes, void
functions may have parameters just like any regular function. For example, this analyze()
function is a void
function that has parameters.
1 2 3 4 |
void analyze(int n, float x, float y, TBitmap *bmp) { } |
How to write function overloading in C programming?
Function Overloading is used to operate with the same function by defining it with different variable types. By using Function Overloading, a function can be used with the same name in different parameter types and multiple variations. In this post we explain how you can do function overloading.
How to write a recursive function in C programming?
The C++ and C programming languages support recursion. Recursion can be a very useful technique in software development.
Recursion means the process of repeating things in itself. In C and C++, if you create a function to call itself, it is called a recursive function, recursive procedure, recursive call, or recursive method. Recursive functions allows you to call the same function from within the function itself to reprocess data with different steps of calculations. Here is how you can create and use recursive functions.
C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.