12 – Functions in C Programming Language

In this chapter, we will learn Functions.

Function

A statement which perform a task is called a Function. Every C program has at least one function which is main(). The most trivial programs can define additional functions.

C standard library provides numerous built-in functions which your program can call. For example, strcat() to concatenate two strings, memcpy() to copy one memory location to another location, & so on.

Defining Function

The general form of function definition in C programming language is:

In C programming, a function definition consist of a function header & function body. Below all the parts are defined.

  • return type – The return type is the data type of the value the function returns. Some functions perform operations without returning a value. In this case, void is the return_type keyword.
  • Function Name – This is the actual name of the function.
  • Parameter – Parameter is like a placeholder. When function invoked, you pass a value to the parameter. This value is refer to as actual parameter. The parameter list refers to the type, order & number of the parameter of a function. Parameters are optional.
  • Function Body – The function body contains statements which define what the function does.

Example:

The source code for a function called max() is given below. This function takes two parameters num1 & num2 & returns the maximum value between the two.

Function Declaration

Function Declaration tells the compiler about function name & how to call the function. The actual body of the function can be defined separately:

Parts

Function declaration has following parts:

The function declaration for the above defined function max() is as follows:

In function declaration, parameter names are not important, only their type is required, So the given below is also a valid declaration:

If you define a function in one source file & you call that function in another file then function declaration will be required.

Calling a Function

While creating a function in C, you give a definition of what a function has to do. To use a function, you have to call the function to perform the defined task.

When a program calls a function, the program controls are transferred to the called function. A called function performs a defined task & when its return statement is executed or when its function -ending closing brace is reached, it returns the program control back to the main program.

To call a function, you simply need to pass the required parameters along with the function name & if the function returns a value, then you can store the return value.
For Example:

we have kept max() along with the main() & compiled the source code. While returning the final executable, it would produce the following result:

Function arguments

If a function is to use arguments, it must declare variables which accept the values of the arguments. These variables are called the formal parameters of the function.

Formal parameters behave like other variable inside the function &

Sr.NoCall TypeDescription
1Call by valueThis method copies the actual value of an argument into formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument.
2Call by referenceThis method copies the address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. This means that changes made to the parameter affect the argument.

By default, C uses call by value to pass argument. In general, it means the code within a function cannot alter the arguments used to call the function.

Subscribe Our Channels:

Click Here for more Articles:

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.