CC++Introduction to C++Learn C++

How To Write A Function In C Programming

How To Write A Function In C Programming

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.

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:

Here is an example for a general function declaration:

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,

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:

here is an example that how we use a function.

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?

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:

or generally we use void as below,

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:

here is an example that how we use a function.

An example of how to write a simple function in C programming?

Here is a simple function example in C programming,

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:

Here is an example for a void function declaration with parameters:

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:

We can easily use this void function as below,

here is an example that how we use a function.

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.

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.

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 functionrecursive procedurerecursive 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.

How To Write A Function In C Programming the C++ Builder Logo

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.

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Introduction to C++Learn C++

Learn Copy Constructors in C++ Classes

C++C++11C++14C++17Introduction to C++Learn C++Syntax

Learn How To Use Types Of Destructors In C++?

C++C++11C++14Learn C++Syntax

How To Convert u32string To A wstring In C++

C++C++11C++14C++17C++20Introduction to C++Learn C++

How To Learn The Move Constructors In Modern C++?