C Functions

In this lesson, you will learn about functions in the C programming language and their usages, along with examples to better understand the topic.

What are functions in C?

C functions are comparable to those of other languages of programming. A function is a piece of software in the form of a parameter that takes one more input, performs some processing, and returns a value. There are built-in functions, but C also provides you the choice to develop your features.

Types of function

There are two types of functions in C:

  • Standard Library Function
  • User-Defined Function

Standard library Function

In C programming, the standard library functions available are called built-in functions. A feature is a block of code contained in itself that performs a particular job. C has an enormous collection of internal or built-in functions that you can call directly to execute a particular job within your C program, such as printf(), a standard library function available in stdio.h header. You can include the header file at the program’s start by using #include <stdio.h>.

User-defined Function

C also enables you to define your functions and the built-in functions. It is a way of creating reusable code packages that accomplish particular duties and can be retained and maintained independently as the main program. Here are some functional benefits

Why do we use a user-defined function?

Functions reduce code repetition within a program— Function enables you to extract frequently used code blocks into a single element. Now you can conduct the same job without copying and pasting the same block of code repeatedly by calling this function wherever you want within your script.

Functions make the code much simpler to keep. Since it can be used multiple times, any modifications produced within a function will automatically be enforced at all locations without touching multiple files. Functions make the elimination of mistakes easier — When the program is split into functions, if any mistake occurs, you understand precisely what the mistake causes and where to find it. Thus, it becomes much easier to fix mistakes.

You can use functions across other apps — Because a function is separated from the remainder of the script, the same function can be easily reused in other apps simply by including the php file containing those functions.

You will see how readily you can define your own function in C in the following section.

Create a Function in C

Creating functions bla bla

Syntax of function

//Definition of Function
void NameofFunction ()
{
//code will be processed in parenthesis
}

int main ()
{
// Calling Function in main()
  NameofFunction ();
}

The user-defined function declaration starts with the data type of function, followed by the function name to be created, followed by parentheses, i.e.(), and lastly, places the code of your function between curly brackets{}. This is a simple instance of a user-defined feature displaying my first program syntax:

Function Declaration in C

A Function Declaration is entirely the declaration of a function that determines the function’s name, parameters, and return type, and it doesn’t include a functional body. A function Declaration provides details to the compiler that the function uses in the program later.

Syntax of function Declaration

returnType NameofFunc ();
returnType NameofFunc (type1 arg, type2 arg, ...);

In the below example, the function’s name is userDefineFunc(), and the return type of this function is void. It can be int, float, or any other data types. No arguments of type int are passed to the function. However, the function can have a number of arguments. The function Declaration is not needed if the user-defined function is defined before the main() function.

Calling a Function

Passing the program’s control to the user-defined function by calling it in the main() function.

Syntax of calling a function in C

NameofFunc();
NameofFunc(arg, arg);

In the above example, we call the function using userDefineFunc(); statement in the main() function.

Function definition

Function definition holds the piece of code to execute a task. In our example, printing some text ‘My first Progarm’ and returning it.

Syntax of the function definition

datatype NameofFunc (datatype arg, datatype arg, ...)
{
  //body of the function
}

When you call a function, the control flow of the program goes inside that function. The compiler begins executing the codes in the body of a function and then exits at the end of the execution, back to where the function was called.

Example:

// program displays the text of my first program
#include <stdio.h>
//fucntion declarartion
void userDefineFunc ();
void main ()
{
  printf (" ==Program displays some text==\n");
  //calling function
  userDefineFunc ();
  return 0;
}

//fucntion definition
void userDefineFunc ()
{
  printf ("My First Program");
}

Output

==Program displays some text==
My First Program
C Recursion

In the next lesson, you will learn about an important terminology in functions: recursion!