C Functions lecture
C Functions lecture
Syntax of Functions in C
1. Function Declaration
2. Function Definition
3. Function Calls
Function Declarations
In a function declaration, we must provide the function name, its return type,
and the number and type of its parameters. A function declaration tells the
compiler that there is a function with the given name defined somewhere
else in the program.
Syntax
Example
Function Definition
Function Call
In the below example, the first sum function is called and 10, 30 are passed
to the sum function. After the function call sum of a and b is returned and
control is also returned back to the main function of the program.
Working of function in C
Example of C Function
// their sum
{ return a + b;
// Driver code
int main( )
return 0;
Output
Sum is: 40
Function return type tells what type of value is returned after all function is
executed. When we don’t want to return a value, we can use the void data
type.
Example:
int func(parameter_1,parameter_2);
The above function will return an integer value after running statements
inside the function.
Note: Only one value can be returned from a C function. To return multiple
values, we have to use pointers or structures.
Function Arguments
Function Arguments (also known as Function Parameters) are the data that is
passed to a function.
Example:
3. Calling the function: Calling the function is a step where we call the
function by passing the arguments in the function.
Types of Functions
1. Library Functions
1. Library Function
For Example:
Example:
C
// C program to implement
#include <math.h>
#include <stdio.h>
// Driver code
int main()
double Number;
Number = 49;
// library function
Number, squareRoot);
return 0;
Output
Example:
// C program to show
// user-defined functions
#include <stdio.h>
return a + b;
// Driver code
int main( )
// function call
return 0;
Output
Sum is: 70
The data passed when the function is being invoked is known as the Actual
parameters. In the below program, 10 and 30 are known as actual
parameters. Formal Parameters are the variable and the data type as
mentioned in the function declaration. In the below program, a and b are
known as formal parameters.
Passing Parameters to Functions
1. Pass by Value
2. Pass by Reference
1. Pass by Value
Parameter passing in this method copies values from actual parameters into
formal function parameters. As a result, any changes made inside the
functions do not reflect in the caller’s parameters.
Example:
// of call by value
#include <stdio.h>
var1 = var2;2
// Driver code
int main()
{
int var1 = 3, var2 = 2;
var1, var2);
swap(var1, var2);
var1, var2);
return 0;
Output
Example
Following is the C program to increment value by 5 for every call by using
call by value or pass by value −
#include <stdio.h>
int inc(int num){
num = num+5;
return num;
}
int main(){
int a=10,b,c,d;
b =inc(a); //call by value
c=inc(b); //call by value
d=inc(c); //call by value
printf("a value is: %d
", a);
printf("b value is: %d
", b);
printf("c value is: %d
", c);
printf("d value is: %d
", d);
return 0;
}
Output
When the above program is executed, it produces the following result −
a value is: 10
b value is: 15
c value is: 20
d value is: 25
2. Pass by Reference
The caller’s actual parameters and the function’s actual parameters refer to
the same locations, so any changes made inside the function are reflected in
the caller’s actual parameters.
Example:
// call by Reference
#include <stdio.h>
*var1 = *var2;
*var2 = temp;
}
// Driver code
int main()
var1, var2);
swap(&var1, &var2);
var1, var2);
return 0;
Output
// C Program to implement
// Call by value
#include <stdio.h>
// Call by value
int c;
c = x + y;
return c;
// Driver Code
int main()
// Integer Declared
int a = 3, b = 2;
// Function Called
return 0;}
Advantages of Functions in C
1. The function can reduce the repetition of the same statements in the
program.
5. Once the function is declared you can just use it without thinking
about the internal working of the function.
Disadvantages of Functions in C
2. Memory and time overhead due to stack frame allocation and transfer
of program control.