Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

Function in C

We refer to a function as a group of various statements that perform a task together. Any C
program that we use has one function at least, that is main(). Then the programs can define
multiple additional functions in a code.

Table of Contents

• Use Of The Function In C


• Advantages Of Using Functions In C Programming
• Defining A Function In C
o Function Syntax
o Example
• Types Of Functions
• Return Value
• Function Calling – Different Aspects
• Declaration Of Function
• Calling Of A Function
• Function Arguments
• Library Functions In C
• Practice Problems On Function In C
• FAQs

Use of the Function in C


One can easily divide a C program code into several separate functions. How we divide our
available code according to different available functions is totally up to us. But the division
should occur logically in such a way that every function is capable of performing a specific
task.

The declaration of function informs the compiler about the name of the function, parameters,
and return type. In other words, the function definition helps in providing the actual body of
any function that we want in a program/ code.

We can also refer to functions as a sub-routine, a method, or a procedure in a program. The


standard library in C language provides its users with various built-in functions that the
concerned programs can call. For example, we can use the strcat() function for concentrating
two strings, copy the location of one memory to another location of memory using the
memcpy() function, and many more.
Advantages of Using Functions in C Programming
The functions in the C programming language offer the following advantages to us:

• When we make use of the functions, then we can easily avoid the rewriting of the
same code/ logic time and again multiple times in any program.
• The calling of C functions may appear as many numbers of times as we want in any
program. And we can do so from any place in the program that is given to us.
• We can perform the tracking of a large C program pretty easily if we divide it into
various functions.
• One of the primary achievements of the C functions is reusability.
• However, remember that the function calling always acts as an overhead in the case of
a C program.

Defining a Function in C
The function definition, in general, holds this form in the C programming language:

return_type name_of_function( parameter list ) {

function body

The function definition in the C programming language contains a function body and
a function header. Let us take a look at all the parts that a function consists of:

• Function Name − It denotes the actual name of any function that we are using in the
given code. The name and parameter of a function constitute a function signature in
the code together.
• Function Body − It consists of a collection of all the statements. These provide a
definition of what the function will do in the code.
• Parameters − It is just like a placeholder. Whenever we invoke a function, we pass a
value to the available parameter. We refer to this value as an argument or an actual
parameter. We refer to the parameter list as the number, order, and type of the
function in the code. The parameters may be optional. It means that any function in a
code may consist of no parameters at all.
• Return Type − We may get a value in return for a function. The term return type
refers to the data type of the returns that we get from the functions (function returns).
Some of the functions are also capable of performing any desired operation without
getting a value in return for it. In any such case, the keyword used for the return_type
will be void.

Syntax of Function Aspects of Function


return_type name_of_function (argument list) {function body;} Function definition

name_of_function (argument_list) Function call

return_type name_of_function (argument list); Function declaration

Function Syntax
Here is the syntax that we use to create a function in the C programming language:

return_type name_of_function(data_type parameter…){

// executable code in c

Example
Here, we have given a source code below for the function max(). The max() function takes
two of the parameters- namely val1 and val2. It then returns the maximum value that is
present between both of them. Let us take a look:

/* the function returns the max in between two values */

int max(int val1, int val2) {

/* declaration of local variable */

int result;

if (val1 > val2)

result = val1;

else

result = val2;

return result;

Types of Functions
The C programming language has functions that are of the following types:

• User-defined Functions – These are the types of functions that we can create using
the C programmer so that we can make use of it multiple times. This function reduces
the complexity of any big program- and thus, optimizes the given code.
• Library Functions – These are the functions whose declaration occurs in the header
files of C, such as floor(), ceil(), outs(), gets(), printf(), scanf(), etc.

Return Value
Any function in the C programming may not return its value from any function. In case we
don’t need to return the value that is available in any function, we can make use of the void in
the form of a return type. Let us look at an example of the C function that does not perform
the return of a value from the available function.

Let us look at an example of a C function that has no return value:

void chocolate(){

printf(“chocolate c”);

In case we want to return a value from an available function, we must make use of any of the
data types, such as char, long, int, etc. Thus, the return type depends totally on the value that
needs to return from the available function. We will look at an example of a C function that
will return an int value from the given function.

Example of a C function with int return value,

int get(){

return 20;

In the example mentioned above, we are trying to return the value 20, and the data type, in
this case, is int. If we are willing to return to the value of the floating-point (example, 64.5,
7,9, 27.61, etc.), then we must make use of the float in the form of the method’s return type.

float get(){

return 83.7;
}

Here, we have to call a function so that we get the function’s value.

Function Calling – Different Aspects


The functions available in a code may accept an argument or may not accept it at all. Thus, it
may return any of the values or may not do so. On the basis of these facts, the function calls
have the following four aspects:

• A function that has no arguments and has no return value.


• A function that has no arguments but has a return value.
• A function that has arguments but has no return value.
• A function that has arguments and also has a return value.
Example of a function that has no arguments and has no return value,

#include<stdio.h>

void printName();

void main ()

printf(“Bye “);

printName();

void printName()

printf(“Kiddo”);

The output generated from this program would be:

Bye Kiddo

Example of a function that has no arguments but has a return value,

#include<stdio.h>

int sum();
void main()

printf(“By calculating the area of the square given\n”);

float area = square();

printf(“The area of the given square will be: %f\n”,area);

int square()

float side;

printf(“The length of the side of square in meters will be: “);

scanf(“%f”,&side);

return side * side;

The output generated from this program would be:

By calculating the area of the square given

The length of the side of the square in meters will be: 10

The area of the given square will be: 100.000000

Example of a function that has arguments but has no return value,

#include<stdio.h>

void sum(int, int);

void main()

int x,y,result;

printf(“\nCalculation of the sum of two values:”);

printf(“\nEntering of two values:”);

scanf(“%d %d”,&x,&y);
sum(x,y);

void sum(int x, int y)

printf(“\nThe sum of the two values is %d”,x+y);

The output generated from this program would be:

Calculation of the sum of two values:

Entering of two values 58

62

The sum of the two values is 120

Example of a function that has arguments and also has a return value,

#include<stdio.h>

int sum(int, int);

void main()

int x,y,result;

printf(“\nThe calculation of the sum of two values:”);

printf(“\nThe entering of two values:”);

scanf(“%d %d”,&x,&y);

result = sum(x,y);

printf(“\nThe sum is : %d”,result);

int sum(int x, int y)

return x+y;
}

The output generated from this program would be:

The calculation of the sum of two values:

The entering of two values: 49

51

The sum is: 100

Declaration of Function
The function definition basically tells a compiler about the name of the function and how one
can call this function. Then we can define the actual body of this function separately.

There are these following parts that exist in a function declaration:

return_type name_of_function( parameter list );

For the defined function above that is max(), the function declaration will occur in the
following way:

int max(int val1, int val2);

The parameter names are not that important in the case of function declaration- we only
require their type. Thus, the declaration mentioned below is also going to be valid:

int max(int, int);

We only require the declaration of the function when we define that function in a source file
while we still call that function in a separate file. In any such case, we have to declare that
function at the very top of the file that calls that function.

Calling of a Function
When we create a function in C programming, we basically provide that function with a
definition of what it has to do. But to use this function, we need to call this function for
performing its defined task.

When a function gets called by a program, the called function will get the program control
transferred to it. The called function will be performing a defined task. Once we execute the
return statement or when it reaches the closing brace of the function-ending, it is bound to
return the program control to the main program.

For calling a function, we simply have to pass the parameters required for it, along with the
name of the function. Here, if the function happens to return a value, then we can easily keep
the returned value stored with us.

For example −

#include <stdio.h>

/* declaration of function */

int max(int val1, int val2);

int main () {

/* definition of local variable */

int x = 400;

int y = 500;

int ret;

/* function calling for getting a max value */

ret = max(x, y);

printf( “The max value is : %d\n”, ret );

return 0;

/* function returns the max in between two values */

int max(int val1, int val2) {

/* declaration of local variable */

int result;

if (val1 > val2)

result = val1;

else

result = val2;

return result;
}

Output

In this case, we have kept the function max() with the function main(). After that, we have
performed the compilation of the source code. Running of the final executable would be
producing the result that follows:

The max value is: 500

Function Arguments
When a function needs to use the arguments, it has to declare those variables that happen to
accept the arguments’ values. Such variables are known as the given function’s formal
parameters.

The formal parameters behave just like any other local variables inside our given function.
These parameters get created when they enter into a function. After that, it gets destroyed
when it exits.

During the calling of a function, there will be two ways in which we can perform the passing
of these arguments to a given function:

Type of Description of Call Type


Call

Call By The Call by Reference method creates a copy of the address of the given argument into the parameter
Reference that is formal in nature. Inside this function, the use of address helps in accessing the actual argument
that comes in use in this call. It means that the changes that appear on the parameter are bound to affect
the given argument.

Call By The Call by Value method creates a copy of the actual value of the given argument in the parameter of
Value the function that is formal in nature. Here, the changes that appear on the parameter (that exists inside
the function) create no effect whatsoever on the available argument.

The C programming, by default, is the Call by Value for passing the arguments. Generally, it
means that we cannot make use of the code that exists within a function for altering the
arguments that help in calling the function.

Library Functions In C
The library functions in the C programming language are the inbuilt functions that are
grouped together and then placed in a common place known as the library. We make use of
the library functions to perform a specific type of operation. For example, the library function
printf helps is printing on the console. The designers of a compiler create the library
functions. The defining of all the standard library functions in C occurs inside multiple
header files that are saved using the .h extension.

A user needs to include such header files in their program for utilizing the library functions
that exist in those header files. For instance, if we want to make use of scanf/ printf library
functions, then we have to include the stdio.h header file in our program. This header file
consists of all library functions that relate to standard output/ input.

Here is a list of some of the header files that is mostly utilized in the C programming:

Header Header file’s Description


file

conio.h It is a type of console output/ input header file.

stdio.h It is a standard type of output/ input header file. This header file consists of all the library functions that
are related to standard output/ input.

stdlib.h It is a type of header file that consists of various general library functions, such as exit(), calloc(),
malloc(), etc.

string.h This header file consists of all the library functions that are string-related, such as puts(), gets(), etc.

time.h It is a type of header file that consists of all the functions that are time-related.

math.h It is a type of header file that consists of all the functions that are related to the math operations, such as
pow(), etc.

stdarg.h We use this header file for defining the variable argument functions.

ctype.h It is a type of header file that consists of the characters that handle the functions.

setjmp.h It is a type of header file that consists of all the functions that are jump functions.
signal.h We use this header file for defining any given signal handling function.

errno.h This type of header file consists of all the error handling functions.

locale.h It is a type of header file that consists of alL the locale functions.

assert.h It is a type of header file that consists of all the diagnostic functions.

Practice Problems on Function in C


1. The declaration of which of these functions occurs in the header files of C?

A. Library functions

B. User-defined functions

C. Both

D. None

Answer – A. Library functions

2. Which of these is a part of a function?

1. Function Name

2. Function Body

3. Variable Space

4. Parameters

5. Return Type

A. 1, 2, 3, 4

B. 2, 4, 5

C. 1, 2, 4, 5

D. 2, 3, 5
E. None of the above

Answer – C. 1, 2, 4, 5

FAQs
Q1
What are the different aspects of function calls?
The functions available in a code may accept an argument or may not accept it at all. Thus, it
may return any of the values or may not do so. On the basis of these facts, the function calls
have the following four aspects:

• A function that has no arguments and has no return value.


• A function that has no arguments but has a return value.
• A function that has arguments but has no return value.
• A function that has arguments and also has a return value.
Q2
Why do we need to declare a function in a program?
The function definition basically tells a compiler about the name of the function and how one
can call this function. Then we can define the actual body of this function separately.
There are these following parts that exist in a function declaration:
return_type name_of_function( parameter list );
Q3
What is the importance of calling a function?
When we create a function in C programming, we basically provide that function with a
definition of what it has to do. But to use this function, we need to call this function for
performing its defined task.
When a function gets called by a program, the called function will get the program control
transferred to it. The called function will be performing a defined task. Once we execute the
return statement or when it reaches the closing brace of the function-ending, it is bound to
return back the program control to the main program.
For calling a function, we simply have to pass the parameters required for it, along with the
name of the function. Here, if the function happens to return a value, then we can easily keep
the returned value stored with us.

You might also like