0% found this document useful (0 votes)
29 views44 pages

Unit 4 SPC

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views44 pages

Unit 4 SPC

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 44

Functions and Parameters

Prof. Manohar B. Patil


Assistant Professor

Department of Computer Engineering


R.C. Patel Institute of Technology,
(An Autonomous Institute)
Lecture Outline
• Introduction
1 Function Declaration
2 Function Definition
Function Call
3 Function Type
System Defined
User Defined
4 Function Classification
5 Recursive Function
6 Storage Classes
Introduction
• Divide a large program into the basic building blocks known as function.

• A function is a set of statements or a block of code that takes inputs, performs


operations, and outputs the results.
• Before calling a function, it's preferable to declare it first.

• 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.
•Function:
The function is also
Function known
is a subpart as procedure
of a program or subroutine
used to perform in and
a specific task other programming
is executed
individually.
languages.
Advantage of functions in C

• 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

any place in a program.

• Debugging done easily in a large C program easily when it is divided into multiple

functions.

• However, Function calling is always a overhead in a C program.


• Every function in C has the following,
1. Function Declaration (Function Prototype)
2. Function Definition
3. Function Call
• The function declaration tells the compiler about,

1. Function Name

2. The data type of the return value

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 :-

return_type function_name (parameter list)


{
Body_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.

• 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.
Calling a Function:-
• Function call is calling the function in the program by writing the function name and passing the
arguments if necessary.

• The function call is performed inside the main() function or any other function or inside the
function itself.

Syntax (Function Call)

functionName (parameters);
User-defined function :-

#include <stdio.h>
void functionName()
{
... .. ...
}
int main()
{
... .. ...
functionName();
... .. ...
}
Example 1:- Function declaration and calling a function

#include <stdio.h> /* function returning the max


/* function declaration */ between two numbers */
int max(int num1, int num2); int max(int num1, int num2) {
/* local variable declaration */
int main () {
int result;
/* local variable definition */
int a = 100; if (num1 > num2)
int b = 200;
int ret; result = num1;
else
/* calling a function to get max value */ result = num2;
ret = max(a, b);

printf( "Max value is : %d\n", ret ); return result;


return 0; }
}
Example 2:- 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.

• function without arguments and without return value

• function without arguments and with return value

• function with arguments and without return value

• function with arguments and with return value


Example:- Function without argument and return value

#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

formal parameter is the argument which is used in the function definition.


Example (Call by value)

#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

void swap (int a, int b)


{
int temp;
temp = a;
#include <stdio.h>
a=b;
void swap(int , int); //prototype of the function
b=temp;
int main()
printf("After swapping values in function a = %d, b =
{
%d\n",a,b); // Formal parameters, a = 20, b = 10
int a = 10;
}
int b = 20;
printf("Before swapping the values in main a = %d, b = %d\ Output :-
n",a,b); // printing the value of a and b in main
swap(a,b);
Before swapping the values in
printf("After swapping values in main a = %d, b = %d\n",a,b); // The main a = 10, b = 20
value of actual parameters do not change by changing the //formal
parameters in call by value, a = 10, b = 20
After swapping values in
} function a = 20, b = 10
After swapping values in main
a = 10, b = 20
Call by value and Call by reference in C

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

the address of the actual parameters is passed.

• 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

void swap (int *a, int *b)


#include <stdio.h>
void swap(int *, int *); //prototype of the function {
int main() int temp;
{
temp = *a;
int a = 10;
int b = 20; *a=*b;
printf("Before swapping the values in main a = %d, b = %d\ *b=temp;
n",a,b); // printing the value of a and b in main
printf("After swapping values in
swap(&a,&b);
printf("After swapping values in main a = %d, b = %d\ function a = %d, b = %d\n",*a,*b); //
n",a,b); Formal parameters, a = 20, b = 10
}
}
Difference between call by value and call by reference in c

Call by value Call by reference

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 the example program, the addresses of variables num1

• and num2 are copied to pointer variables a and b.

• The changes made on the pointer variables a and b in called

• function effects the values of actual parameters num1 and

• num2 in calling function


Recursive Function

• 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.

• A storage class in C is used to represent the information of a variable.


1) Write a program in C to find the sum of two numbers using function
• #include <stdio.h>

• int sum (int, int);//function declaration


• int main (void)
• {
• int total;
• printf("\n\n Function : a simple structure of function :\n");
printf("------------------------------------------------\n");
• total = sum (5, 6);//function call
• printf ("The total is : %d\n", total);
• return 0;
• }
• int sum (int a, int b) //function definition
• {
int s;
• s=a+b;
• return s; //function returning a value
• }
2) WAP in c to get the max value
using function
#include <stdio.h>
* function declaration */
int max(int num1, int num2);
int main () {
int a = 100;
int b = 200;
int ret;
/* calling a function to get max value */
ret = max(a, b);
printf( "Max value is : %d\n", ret );
return 0;
}
/* function returning the max between two numbers */
int max(int num1, int num2)
{
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
3) Write a program in C to find the square of any number using the function

• #include <stdio.h>

• double square(double num)


• {
• return (num * num);
• }
• int main()
• {
• int num;
• double n;
• printf("\n\n Function : find square of any number :\n");
• printf("------------------------------------------------\n");
• printf("Input any number for square : ");
• scanf("%d", &num);
• n = square(num);
• printf("The square of %d is : %.2f\n", num, n);
• return 0;
• }
4) Write a program in C to check whether a number is a prime
number or not using the function
#include<stdio.h>
int PrimeOrNot(int);
int main()
{
int n1,prime;
printf("\n\n Function : check whether a number is prime number or not :\n");
printf("---------------------------------------------------------------\n");

printf(" Input a positive number : ");


scanf("%d",&n1);
prime = PrimeOrNot(n1);
if(prime==1)
printf(" The number %d is a prime number.\n",n1);
else
printf(" The number %d is not a prime number.\n",n1);
return 0;
}
int PrimeOrNot(int n1)
{
int i=2;
while(i<=n1/2)
{
if(n1%i==0)
return 0;
else
i++;
}
return 1;
}
• #include<stdio.h>
• void swap(int ,int );
• int main()
• {
• int n1,n2;
• printf("\n\n Function : swap two numbers using function :\n");
• printf("------------------------------------------------\n");
• printf("Input 1st number : ");
• scanf("%d",&n1);
• printf("Input 2nd number : ");
• scanf("%d",&n2);
• printf("Before swapping: n1 = %d, n2 = %d ",n1,n2);
• swap(n1,n2);
• printf("\nAfter swapping: n1 = %d, n2 = %d \n\n",n2,n1);
• return 0;
• }
• void swap(int p,int q)
• {
• int tmp;
• tmp = p;
• p=q;
• q=tmp;

You might also like