Unit 4 SPC
Unit 4 SPC
• By using functions, we can avoid rewriting same logic/code again and again in a
program.
• Reusability :- We can call C functions any number of times in a program and from
• Debugging done easily in a large C program easily when it is divided into multiple
functions.
1. Function Name
3. Parameters
• The function declaration is performed before the main function or inside the
main function or any other function.
• If the function declares above the main() function called global function,
they can be accessed thought the program.
• Syntax of Function :-
Example:-
// Create a function
void myFunction()
{
printf("I just got executed!");
}
• Syntax of Function :-
Function aspects Discription Syntax
Return type indicates
the type of output
Return type return_type
returned by the
function
Function name
indicates the name of
Function name the function which we function_name
use to call the
function
Set of inputs indicates
Set of inputs or
Set of inputs inputs provided to the
parameters
function
Body contain set of
Types of Functions
• Library Functions: are the functions which are declared in the C header files
such as scanf(), printf(), gets(), puts(), ceil(), floor() etc.
• The function call is performed inside the main() function or any other function or inside the
function itself.
functionName (parameters);
User-defined function :-
#include <stdio.h>
void functionName()
{
... .. ...
}
int main()
{
... .. ...
functionName();
... .. ...
}
Example 1:- Function declaration and calling a function
#include <stdio.h>
int addNumbers(int a, int b); // function prototype
int addNumbers(int a, int b) //
int main()
{
function definition
int n1,n2,sum; {
int result;
printf("Enters two numbers: ");
scanf("%d %d",&n1,&n2);
result = a+b;
return result; // return
sum = addNumbers(n1, n2); // function call statement
printf("sum = %d",sum);
}
return 0;
}
Example 3:- Calling function multiple times :-
#include <stdio.h>
// Create a function
void myFunction() {
printf("I just got executed!\n");
} Output :-
I just got executed!
int main() { I just got executed!
myFunction(); // call the function I just got executed!
myFunction(); // call the function
myFunction(); // call the function
return 0;
}
Passing arguments to a function
Different aspects of function calling
There are four different aspects of function calls.
#include<stdio.h>
void printName();
void main ()
{
printf("Hello ");
printName(); Output :-
} Hello Javatpoint
void printName()
{
printf("Javatpoint");
}
Example:- Function without argument and return value
#include<stdio.h>
void sum();
void main()
{
printf("\nGoing to calculate the sum of Output :-
two numbers:"); Going to calculate the sum of
sum(); two numbers:
}
void sum() Enter two numbers 10
{ 24
int a,b;
printf("\nEnter two numbers"); The sum is 34
scanf("%d %d",&a,&b);
printf("The sum is %d",a+b);
}
Example:- Function without argument and with return value
#include<stdio.h>
int sum();
void main()
{
int result;
printf("\nGoing to calculate the sum of two Output :-
numbers:"); Going to calculate the sum of
result = sum(); two numbers:
printf("%d",result);
}
int sum() Enter two numbers 10
{ 24
int a,b;
printf("\nEnter two numbers"); The sum is 34
scanf("%d %d",&a,&b);
return a+b;
}
Example:- Function without argument and with return value
#include<stdio.h>
int square();
void main()
{
printf("Going to calculate the area of the
square\n");
float area = square();
printf("The area of the square: %.2f\
Output :-
n",area); Going to calculate the area of
} the square
int square() Enter the length of the side in
{ meters: 10
float side;
printf("Enter the length of the side in
The area of the square: 100.00
meters: ");
scanf("%f",&side);
return side * side;
}
Example:- Function with argument and without return value
#include<stdio.h>
void sum(int, int);
void main()
{
int a,b,result; Output :-
printf("\nGoing to calculate the sum of two Going to calculate the sum of
numbers:"); two numbers:
printf("\nEnter two numbers:");
scanf("%d %d",&a,&b);
sum(a,b); Enter two numbers 10
} 24
void sum(int a, int b)
{ The sum is 34
printf("\nThe sum is %d",a+b);
}
Example:- Function with argument and without return value
#include<stdio.h>
void average(int, int, int, int, int);
void main()
{
int a,b,c,d,e;
printf("\nGoing to calculate the Output :-
average of five numbers:"); Going to calculate the average
printf("\nEnter five numbers:");
scanf("%d %d %d %d
of five numbers:
%d",&a,&b,&c,&d,&e); Enter five numbers:10
average(a,b,c,d,e); 20
} 30
void average(int a, int b, int c, int d, 40
int e)
{
50
float avg; The average of given five
avg = (a+b+c+d+e)/5; numbers : 30.000000
printf("The average of given five
numbers : %f",avg);
}
Example:- Function with argument and with return value
#include<stdio.h>
int sum(int, int);
void main()
{
int a,b,result;
printf("\nGoing to calculate the sum of two
numbers:");
Output :-
printf("\nEnter two numbers:"); Going to calculate the sum of two
scanf("%d %d",&a,&b); numbers:
result = sum(a,b); Enter two numbers:10
printf("\nThe sum is : %d",result); 20
}
int sum(int a, int b)
The sum is : 30
{
return a+b;
}
Call by value and Call by reference in C
Call by value in C :-
• In call by value method, the value of the actual parameters is copied into the formal
parameters.
• 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
#include<stdio.h>
void change(int num) {
printf("Before adding value inside function
num=%d \n",num);
num=num+100; Output :-
printf("After adding value inside function
num=%d \n", num); Before function call x=100
} Before adding value inside function
int main() { num=100
int x=100; After adding value inside function
printf("Before function call x=%d \n", x); num=200
change(x);//passing value in function
printf("After function call x=%d \n", x); After function call x=100
return 0;
}
Call by Value Example: Swapping the values of the two variables
Call by reference in C :-
• In call by reference, the address of the variable is passed into the function call as the actual
parameter.
• The value of the actual parameters can be modified by changing the formal parameters since
• In call by reference, the memory allocation is similar for both formal parameters and actual
parameters. All the operations in the function are performed on the value stored at the address
of the actual parameters, and the modified value gets stored at the same address.
Example :- (Call by Reference)
#include<stdio.h>
void change(int *num) {
printf("Before adding value inside function num=%d \n",*num);
Output
(*num) += 100; Before function call x=100
printf("After adding value inside function num=%d \n", *num);
}
Before adding value inside
int main() { function num=100
int x=100;
printf("Before function call x=%d \n", x);
After adding value inside
change(&x);//passing reference in function function num=200
printf("After function call x=%d \n", x);
return 0;
After function call x=200
}
Call by reference Example: Swapping the values of the two variables
A copy of the value is passed into the function An address of value is passed into the function
Changes made inside the function is limited to Changes made inside the function validate
the function only. The values of the actual outside of the function also. The values of the
parameters do not change by changing the actual parameters do change by changing the
formal parameters. formal parameters.
Actual and formal arguments are created at Actual and formal arguments are created at
the different memory location the same memory location
Swapping of two numbers
# include < stdio .h> void swap (int a, int b) // called function
# include < conio .h> {
void main () int temp ;
{ temp = a;
int num1 , num2 ; a = b;
void swap (int , int); // function declaration b = temp ;
num1 = 10; }
num2 = 20;
printf ("\ nBefore swap : num1 = %d, num2 = %d", num1 , num2 );
swap (num1 , num2 ); // calling function
printf ("\ nAfter swap : num1 = %d, num2 = %d", num1 , num2 );
}
Example Program: Call by Reference
# include < stdio .h> swap (& num1 , & num2 ); // calling function
# include < conio .h> printf ("\ nAfter swap : num1 = %d, num2 = %d", num1
, num2 );
void main ()
}
{
void swap (int *a, int *b) // called function
int num1 , num2 ;
{
void swap ( int *, int*); // function declaration
int temp ;
num1 = 10;
temp = *a;
num2 = 20;
*a = *b;
printf ("\ nBefore
*b = temp ;
swap : num1 = %d,
}
num2 = %d", num1 , num2 );
Output
Before Swap: num1 = 10, num2 = 20
After Swap: num1 = 20, num2 = 10
• In C programming language, function calls can be made from the main() function,
other functions or from the same function itself.
Recursive Fuction: When function called itself again and again is called recursive
function.
# include < stdio .h> {
# include < conio .h> int temp ;
int factorial (int ); if(n ==0)
int main () {
{ return 1;
int fact , n ; }
printf (" Enter any else
positive integer :"); {
scanf ("%d", &n); // recursive
fact = factorial (n); function call
printf (" Factorial of %d is %d\n", n,fact ); temp = n * factorial (n -1);
return 0; }
} return temp ;
int factorial (int n) }
Storage Classes
• Every variable in C has two properties; type and storage classes.
• Among them, the type refers to the data type of the variable, and storage classes in C
determine the scope, lifetime, and visibility of the variable.
• Storage classes in C are used to find the lifetime, visibility,memory location, and initial
value of a variable.
• #include <stdio.h>