C Programming UNIT 3.3 Functions
C Programming UNIT 3.3 Functions
In c, we can divide a large program into the basic building blocks known as function. The
function contains the set of programming statements enclosed by {}. A function can be called
multiple times to provide reusability and modularity to the C program. In other words, we can
say that the collection of functions creates a program. The function is also known as procedure
or subroutine in other programming languages.
Advantage of functions in C
There are the following advantages of C functions.
1. By using functions, we can avoid rewriting same logic/code again and again in a
program.
2. We can call C functions any number of times in a program and from any place in a
program.
3. We can track a large C program easily when it is divided into multiple functions.
4. Reusability is the main achievement of C functions.
5. However, Function calling is always a overhead in a C program.
Function Aspects
There are three aspects of a C function.
1. Function declaration A function must be declared globally in a c program to tell
the compiler about the function name, function parameters, and return type.
2. Function call Function can be called from anywhere in the program. The parameter
list must not differ in function calling and function declaration. We must pass the
same number of functions as it is declared in the function declaration.
3. Function definition It contains the actual statements which are to be executed. It
is the most important aspect to which the control comes when the function is called.
Here, we must notice that only one value can be returned from the function.
AD
SN C function aspects Syntax
Types of Functions
There are two types of functions in C programming:
1. Library Functions: are the functions which are declared in the C header files such as
scanf(), printf(), gets(), puts(), ceil(), floor() etc.
2. User-defined functions: are the functions which are created by the C programmer, so
that he/she can use it many times. It reduces the complexity of a big program and
optimizes the code.
AD
Return Value
A C function may or may not return a value from the function. If you don't have to return any
value from the function, use void for the return type. Let's see a simple example of C function
that doesn't return any value from the function.
Example without return value:
1. void hello(){
2. printf("hello c");
3. }
If you want to return any value from the function, you need to use any data type such as int,
long, char, etc. The return type depends on the value to be returned from the function.
Let's see a simple example of C function that returns int value from the function.
Example with return value:
1. int get(){
2. return 10;
3. }
In the above example, we have to return 10 as a value, so the return type is int. If you want to
return floating-point value (e.g., 10.2, 3.1, 54.5, etc), you need to use float as the return type of
the method.
1. float get(){
2. return 10.2;
3. }
Now, you need to call the function, to get the value of the function.
Different aspects of function calling
A function may or may not accept any argument. It may or may not return any value. Based on
these facts, there are four different aspects of function calls.
o function without arguments and without return value
o function without arguments and with return value
o function with arguments and without return value
o function with arguments and with return value
Example 2
AD
1. #include<stdio.h> 8. void sum()
2. void sum(); 9. {
3. void main() 10. int a,b;
4. { 11. printf("\nEnter two numbers");
5. printf("\nGoing to calculate the s 12. scanf("%d %d",&a,&b);
um of two numbers:"); 13. printf("The sum is %d",a+b);
6. sum(); 14. }
7. }
Output
Going to calculate the sum of two numbers:
The sum is 34
Example for Function without argument and with return value
Example 1
1. #include<stdio.h> 9. }
2. int sum(); 10. int sum()
3. void main() 11. {
4. { 12. int a,b;
5. int result; 13. printf("\nEnter two numbers");
6. printf("\nGoing to calculate the s 14. scanf("%d %d",&a,&b);
um of two numbers:"); 15. return a+b;
7. result = sum(); 16. }
8. printf("%d",result);
17.
Output
Going to calculate the sum of two numbers:
The sum is 34
The sum is 34
C Library Functions
Library functions are the inbuilt function in C that are grouped and placed at a common place
called the library. Such functions are used to perform some specific operations. For example,
printf is a library function used to print on the console. The library functions are created by the
designers of compilers. All C standard library functions are defined inside the different header
files saved with the extension .h. We need to include these header files in our program to make
use of the library functions defined in such header files. For example, To use the library
functions such as printf/scanf we need to include stdio.h in our program which is a header file
that contains all the library functions regarding standard input/output.
The list of mostly used header files is given in the following table.
SN Header file Description
4 stdlib.h This header file contains all the general library functions like
malloc(), calloc(), exit(), etc.
5 math.h This header file contains all the math operations related functions
like sqrt(), pow(), etc.
The values that are declared within a function when the function is called are known as an
argument or actual parameters. Whereas, the variables that are defined when the function is
declared are known as a parameter or formal parameters.
1 The values that are declared within a The variables that are defined when the
function when the function is called function is declared are known as
are known as an argument. parameters.
2 These are used in function call These are used in the function header of
statements to send value from the the called function to receive the value
calling function to the receiving from the arguments.
function.
3 During the time of call each Parameters are local variables which
argument is always assigned to the are assigned values of the arguments
parameter in the function definition. when the function is called.
4 They are also known as Actual They are also known as Formal
Parameters. Parameters.
Function Arguments
If a function is to use arguments, it must declare variables that accept the values of the
arguments. These variables are called the formal parameters of the function.
Formal parameters behave like other local variables inside the function and are created upon
entry into the function and destroyed upon exit.
While calling a function, there are two methods to pass the data into the function in C language,
i.e., call by value and call by reference.
1 Call by value
This method copies the actual value of an argument into the formal parameter of
the function. In this case, changes made to the parameter inside the function have
no effect on the argument.
2 Call by reference
This 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.
• The call by value method basically copies the actual value of an argument into the
formal parameter of the function. In this case, changes made to the parameter inside the
function have no effect on the argument.
• The value of the actual parameters is copied into the formal parameters. In other words,
we can say that the value of the variable is used in the function call in the call by value
method.
• In call by value method, we cannot modify the value of the actual parameter by the
formal parameter.
• In call by value, different memory is allocated for actual and formal parameters since
the value of the actual parameter is copied into the formal parameter.
• The actual parameter is the argument which is used in the function call whereas formal
parameter is the argument which is used in the function definition.
Let's try to understand the concept of call by value in c language using another example given
below:
1. #include<stdio.h>
2. void change(int num) {
3. printf("Before adding value inside function num=%d \n",num);
4. num=num+100;
5. printf("After adding value inside function num=%d \n", num);
6. }
7. int main() {
8. int x=100;
9. printf("Before function call x=%d \n", x);
10. change(x);//passing value in function
11. printf("After function call x=%d \n", x);
12. return 0;
13. }
Before function call x=100
Before adding value inside function num=100
After adding value inside function num=200
After function call x=100
1. #include <stdio.h>
2. void swap(int *, int *); //prototype of the function
3. int main()
4. {
5. int a = 10;
6. int b = 20;
7. printf("Before swapping the values in main a = %d, b = %d\n",a,b); // printing the v
alue of a and b in main
8. swap(&a,&b);
9. printf("After swapping values in main a = %d, b = %d\n",a,b); // The values of actu
al parameters do change in call by reference, a = 10, b = 20
10. }
11. void swap (int *a, int *b)
12. {
13. int temp;
14. temp = *a;
15. *a=*b;
16. *b=temp;
17. printf("After swapping values in function a = %d, b = %d\n",*a,*b); // Formal para
meters, a = 20, b = 10
18. }
Output
Before swapping the values in main a = 10, b = 20
After swapping values in function a = 20, b = 10
After swapping values in main a = 20, b = 10
Difference between call by value and call by reference in c
1 A copy of the value is passed into An address of value is passed into the function
the function
2 Changes made inside the function is Changes made inside the function validate
limited to the function only. The outside of the function also. The values of the
values of the actual parameters do actual parameters do change by changing the
not change by changing the formal formal parameters.
parameters.
3 Actual and formal arguments are Actual and formal arguments are created at the
created at the different memory same memory location
location
Example
void myFunction(int myNumbers[5]) {
for (int i = 0; i < 5; i++) {
printf("%d\n", myNumbers[i]);
}
}
int main() {
int myNumbers[5] = {10, 20, 30, 40, 50};
myFunction(myNumbers);
return 0;
}
The function (myFunction) takes an array as its parameter (int myNumbers[5]), and loops
through the array elements with the for loop.
When the function is called inside main(), we pass along the myNumbers array, which
outputs the array elements.
Note that when you call the function, you only need to use the name of the array when
passing it as an argument myFunction(myNumbers). However, the full declaration of the
array is needed in the function parameter (int myNumbers[5]).
Return Values
The void keyword, used in the previous examples, indicates that the function should not
return a value. If you want the function to return a value, you can use a data type (such
as int or float, etc.) instead of void, and use the return keyword inside the function:
Example
int myFunction(int x) {
return 5 + x;
}
int main() {
printf("Result is: %d", myFunction(3));
return 0;
}
// Outputs 8 (5 + 3)
Example
int myFunction(int x, int y) {
return x + y;
}
int main() {
printf("Result is: %d", myFunction(5, 3));
return 0;
}
// Outputs 8 (5 + 3)
Example
int myFunction(int x, int y) {
return x + y;
}
int main() {
int result = myFunction(5, 3);
printf("Result is = %d", result);
return 0;
}
// Outputs 8 (5 + 3)