0% found this document useful (0 votes)
3 views

Function

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Function

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Functions

What is Function?
🞂 A function is a group of statements that perform a specific task.
🞂 It divides a large program into smaller parts.
🞂 A function is something like hiring a person to do a specific job for you.
🞂 Every C program can be thought of as a collection of these functions.
🞂 Program execution in C language starts from the main function.
Syntax
void main()
{
// body part
}

🞂 Why function ?
⮩ Avoids rewriting the same code over and over.
⮩ Using functions it becomes easier to write programs and keep track of what they doing.
Types of Function
Function

Library Function User Defined Function (UDF)

Predefined or inbuilt Created by User


Declarations inside header files Programmer need to declare it
Eg. printf() – stdio.h Eg. findSimpleInterest()
pow() – math.h areaOfCircle()
strcmp() – string.h
Program Structure for Function
🞂 When we use a user-defined function program structure is divided into three parts.
Function Structure
void func1(); Function Prototype

void main()
{
....
func1(); Function call
}

void func1()
{
.... Function definition
//function body
....
}
Function Prototype
🞂 A function Prototype also know as function declaration.
🞂 A function declaration tells the compiler about a function name and how to call the
function.
🞂 It defines the function before it is being used or called.
🞂 A function prototype needs to be written at the beginning of the program.

Syntax Example
return-type function-name (arg-1, arg 2, …); void addition(int, int);
Function Definition
🞂 A function definition defines the functions header and body.
🞂 A function header part should be identical to the function prototype.
⮩ Function return type
⮩ Function name
⮩ List of parameters
🞂 A function body part defines function logic.
⮩ Function statements

Syntax Example
return-type function-name (arg-1, arg 2, …) void addition(int x, int y)
{ {
//... Function body printf("Addition is=%d“,(x+y));
} }
WAP to add two number using add(int, int) Function
Program Output
1 #include <stdio.h> Addition is = 11
2 void add(int, int); // function declaration
3
4 void main()
5 {
6 int a = 5, b = 6;
7 add(a, b); // function call
8 }
9
10 void add(int x, int y) // function definition
11 {
12 printf("Addition is = %d", x + y);
13 }
Prof. Nilesh Gambhava #3110003 (PPS) – Functions 9
Actual parameters and Formal parameters
🞂 Values that are passed to the called function from the main function are known
as Actual parameters.
🞂 The variables declared in the function prototype or definition are known as Formal
parameters.
🞂 When a method is called, the formal parameter is temporarily "bound" to the actual
parameter.

Actual parameters Formal parameters


void main() void add(int x, int y) // x and y are
{ formal parameters.
int a = 5, b = 6; {
add(a, b); // a and b are the printf("Addition is = %d", x + y);
actual parameters in this call.
} }
Return Statement
🞂 If function is returning a value to calling function, it needs to use the keyword
return.
🞂 The called function can only return one value per call.

Syntax
return;
Or

return (expression);
WAP to find maximum number from two number
Program Output
1 #include <stdio.h> Max value is : 200
2 int max(int a, int b);
3 void main()
4 {
5 int a = 100;
6 int b = 200;
7 int maxvalue;
8 maxvalue = max(a, b);
9 printf("Max value is : %d\n",
10 maxvalue);
11 }
12 int max(int a, int b)
13 {
14 if (a > b)
15 return a; // return a
16 else
17 return b; // return b
18 }
WAP to calculate the Power of a Number
Program Output
1 #include <stdio.h> Enter any number : 5
2 int power(int, int); Enter power of number : 3
3 void main() 5's power 3 = 125
4 {
5 int num, pow, res;
6 printf("Enter any number : ");
7 scanf("%d", &num);
8 printf("Enter power of number : ");
9 scanf("%d", &pow);
10 res = power(num, pow);
11 printf("%d's power %d = %d", num, pow, res);
12 }
13 int power(int n, int p)
14 { int r = 1;
15 while (p >= 1)
16 {
17 r = r * n;
18 p--;
19 }
20 return r;}
Category of Function
(1) Function with no argument and but no return value
No
void main() Input void fun1()
{ {
..... .....
No return
fun1(); .....
value
..... .....
} }

(2) Function with no argument and returns value


No
void main() Input int fun1(void)
{ {
..... .....
a = fun1() Function .....
..... result return b;
} }
(1) Function with no argument and but no return value
(2) Function with no argument and returns value
Category of Function cont.
(3) Function with argument and but no return value
Value of
void main() Argumen void fun1(int f)
{ t {
..... .....
fun1(a); No Return .....
..... value .....
} }

(4) Function with argument and returns value


Value of
void main() Argumen int fun1(int f)
{ t {
..... .....
b = fun1(a); Function .....
..... Result return e;
} }
3) Function with argument and but no return value
(4) Function with argument and returns value
Nested functions in C

In some applications, we have seen that some functions are declared inside another function.
This is sometimes known as nested function, but actually this is not the nested function. This is called
the lexical scoping.

Lexical scoping is not valid in C because the compiler is unable to reach correct memory location of
inner function.

Nested function definitions cannot access local variables of surrounding blocks. They can access only
global variables

In C there are two nested scopes the local and the global. So nested function has some limited use.
An extension of the GNU C Compiler allows the
Nested functions in C declarations of nested functions. The declarations of
nested functions under GCC’s extension need to be
// C program to illustrate the
prefix/start with the auto keyword.
// concept of Nested function.
#include <stdio.h>

void main()
{
printf("Main");
int fun()
{
printf("fun");

// defining view() function


inside fun() function.
int view()
{
printf("view");
}
return 1;
}
view();
} Compile time error: undefined
reference to `view'
C Recursion

A function that calls itself is known as a recursive


function. And, this technique is known as recursion.
C Recursion

Enter a number: 5
Factorial of 5 is: 120

Homework:-Fibonacci Series
Pass arrays to a function in C

Passing array elements to a function is similar to passing variables to a function.

O/P:
8
4
Pass arrays to a function in C

O/P:
Result = 162.50
Storage Classes
🞂 Storage class decides the scope, lifetime and memory allocation of variable.
🞂 Scope of a variable is the boundary within which a variable can be used.

Storage Initial
Storage Scope Life Example
Specifier Value

Automatic int a;
Stack Garbage Within block End of block
{auto} auto int a;

Register CPU Garbage Within block End of block


register int var;
{register} register

External Data Global Till end of


Zero extern int var;
{extern} segment Multiple file program

Till end of
Static Data Zero Within block static extern int var;
program
{static} segment static int var;
Static Example
Program Output
1 #include <stdio.h> Counter = 1
2 int incrementCounter(); Counter = 2
3
4 void main()
5 {
6 printf("Counter = %d \n", incrementCounter());
7 printf("Counter = %d \n", incrementCounter());
8 }
9
10 int incrementCounter()
11 {
12 static int count = 0; // static variable
13 count++;
14 return count;
15 }
Advantages of Function
🞂 Using function we can avoid rewriting the same logic or code again and again in a
program.
🞂 We can track or understand large program easily when it is divide into functions.
🞂 It provides reusability.
🞂 It help in testing and debugging because it can be tested for errors individually in the
easiest way.
🞂 Reduction in size of program due to code of a function can be used again and again,
by calling it.

Thank you

You might also like