You are on page 1of 20

Basics of Function in C Language

1
What we are going to learn today?

A technique of managing a large program more


efficiently.

2
Introduction

Cooking foods !
Menu
How to manage the cooking in the best possible way?
Item 1
Item 2
Item 3
Item 4
Item 5
Item 6 3
Introduction (cond…)

Solution: Six people participate in cooking


the same item in the same kitchen

Shortcomings
➢ Possible interference among the cooks
➢ If an item tastes bad, who will be responsible and how to rectify?
➢ If any item is required more in future, we need all people to cook that.

4
Introduction (cond…)

Item 1

Item 2

Item 3

Item 4
Item 6 Item 5
5
Introduction (cond…)
A structure of a simple calculator program

• No error propagation
ADD • Easy to find an error
• Reduce program size
SUB
Main Definition: A program
segment that performs a
specific well-defined task.

MUL
DIV

6
Introduction (cond…)
❖What should a function consist of?

Juice maker

Return_type func_name(data_type1, data_type2,….)


• Inputs, known as parameters
{
• Output, known as return type
Function body
• Function body
}
• Function name

7
A Sample Function:
int add(int x1, int y1)
{
int z;
z=x1+y1; Formal parameters
return (z);
}
Function call
ar=add(num1,num2);

Actual parameters 8
A Simple Example
#include <iostream> int add(int x1, int y1)
using namespace std; {
int z;
int add(int x1, int y1); z = x1 + y1;
void sub(int x2, int y2); return (z);
}
void main()
{ void sub(int x2, int y2)
int num1 = 5, num2 = 10, sr, ar; {
int z;
ar = add(num1, num2); z = x2 - y2;
sub(num1, num2); cout << "\nResult of subtraction: "
<< z;
cout << "\nResult of addition: " << ar
<< "\n"; }
system("pause"); 9
}
Program Output

10
Local & Global Variables
#include <iostream> int add(int a)
using namespace std; {
int add(int); int d;
int x; d=x+a;
void main() x=4;
{ a=10;
int a=2,sum1,sum2; return d;
x=2; }
sum1=add(a);
sum2=add(a);
cout<<“sum1=“<<sum1;
cout<<“sum2=“<<sum2; x a sum1 sum2 a
}

Global variable Local variables 11


12
Call by value and Call by Reference
#include <iostream> int add(int &x1, int y1)
using namespace std; {
int add(int &x1, int y1); int z;
int sub(int &x2, int y2); z = x1 + y1;
x1 = 3; y1 = -5;
void main() return (z);
{ }
int num1=5, num2 = 10, sr = 4, ar;
int sub(int &x2, int y2)
ar = add(num1, num2); {
sr = sub(num1, num2); int z;
cout <<"\nResult of addition : " << ar; z = x2 - y2;
cout <<"\nResult of subtraction : " << sr; return (z);
}
system("pause");
}
13
Default Argument
❖Definition:
A value provided in function declaration that is
automatically a ssigned by the compiler if caller
of the function doesn’t provide a value for the
argument with default value.

14
#include<iostream> #include<iostream>
using namespace std; using namespace std;
int sum(int x, int y, int z = 0, int w = 0) int sum(int x, int y, int z = 0, int w )
{ {
return (x + y + z + w); return (x + y + z + w);
} }

int main() int main()


{ {
cout << sum(10, 15)<<“\n”; cout << sum(10, 15)<<“\n”;
cout << sum(10, 15, 25)<<“\n”; cout << sum(10, 15, 25)<<“\n”;
cout << sum(10, 15, 25, 30) << “\n”; cout << sum(10, 15, 25, 30) << “\n”;
system("pause"); system("pause");
return 0; return 0;
} }

15
Function Overloading
❖Multiple functions having the same name with
Different number of parameters and/or data
types of the arguments
Example:
void show_value(int a, float b)
void show_value(float a, float b)

void func(int x);


Void func(int x, int y);
16
#include<iostream>
using namespace std; int abs2(int n)
{
int abs2(int n); cout << "In integer abs()\n";
long abs2(long n); return n < 0 ? -n : n;
double abs2(double n); }

void main() long abs2(long n)


{ {
cout << "Absolute value of -10: " << abs2(- cout << "In long abs()\n";
10) << "\n\n"; return n < 0 ? -n : n;
cout << "Absolute value of -10L: " << abs2(- }
10L) << "\n\n";
cout << "Absolute value of -10L: " << abs2(- double abs2(double n)
10.01) << "\n\n"; {
system("pause"); cout << "In double abs()\n";
} return n < 0 ? -n : n;
}

17
#include<iostream> void f1(int a)
using namespace std; {
cout << "In f1(int a)\n";
void f1(int a); }
void f1(int a, int b);
void f1(int a, int b)
void main() {
{ cout << "In f1(int a, int b)\n\n";
f1(10, 20); }
f1(10);

system("pause");
}

18
19
Reference
1. H. Schildt, “The Complete Reference C++”, McGraw-Hill Publishing
Company, USA, Fourth Edition.
2. M. Lee, “C++ Programming for the Absolute Beginner”, Course
Technology, USA.
3. H. Schildt, “Teach Yourself C++”, McGraw-Hill Publishing Company
Limited, USA, Third Edition.

20