Professional Documents
Culture Documents
Function in C
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
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.
• 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:
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.
Function Syntax
Here is the syntax that we use to create a function in the C programming language:
// 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:
int result;
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.
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.
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;
}
#include<stdio.h>
void printName();
void main ()
printf(“Bye “);
printName();
void printName()
printf(“Kiddo”);
Bye Kiddo
#include<stdio.h>
int sum();
void main()
int square()
float side;
scanf(“%f”,&side);
#include<stdio.h>
void main()
int x,y,result;
scanf(“%d %d”,&x,&y);
sum(x,y);
62
Example of a function that has arguments and also has a return value,
#include<stdio.h>
void main()
int x,y,result;
scanf(“%d %d”,&x,&y);
result = sum(x,y);
return x+y;
}
51
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.
For the defined function above that is max(), the function declaration will occur in the
following way:
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:
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 main () {
int x = 400;
int y = 500;
int ret;
return 0;
int result;
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:
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:
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:
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.
A. Library functions
B. User-defined functions
C. Both
D. None
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: