CS-323 Programming Fundamentals 4 (3-2) CS-323 Programming Fundamentals 4 (3-2)
CS-323 Programming Fundamentals 4 (3-2) CS-323 Programming Fundamentals 4 (3-2)
CS-323 Programming
Programming Fundamentals
Fundamentals
4(3-2)
4(3-2)
Lecture#
Lecture#99&&10
10
Today’s
Today’s Lecture
Lecture
• What are functions?
Programming Fundamentals 2
Functions
Functions
• A function is, in effect, a subprogram that can act on data
and return a value.
• Every C++ program has at least one function, main().
– When program starts, main() is called automatically.
– main() might call other functions, some of which might call still
others.
• Each function has its own name, and when that name is
encountered, the execution of the program branches to
the body of that function.
– When the function returns, execution resumes on the next line of
the calling function.
• When a program calls a function, execution switches to
the function and then resumes at the line after the function
call.
Programming Fundamentals 3
Functions…
Functions…
Programming Fundamentals 4
Functions…
Functions…
• Well-designed functions perform a specific and easily
understood task.
• Complicated tasks should be broken down into multiple
functions, and then each can be called in turn.
• Functions come in two varieties:
– user-defined and built-in.
Programming Fundamentals 5
Functions…Example
Functions…Example
Programming Fundamentals 6
Declaring
Declaring and
and Defining
Defining Functions
Functions
• Using functions in your program requires that you first
declare the function and that you then define the function.
• The declaration tells the compiler the name, return type,
and parameters of the function.
• The definition tells the compiler how the function works.
Programming Fundamentals 7
Declaring
Declaring the
the Functions
Functions
There are three ways to declare a function:
1.Write your prototype into a file, and then use the #include
directive to include it in your program.
Programming Fundamentals 8
Declaring
Declaring and
and Defining
Defining Functions
Functions
Two types of functions:
return-value-type function-name(argument-list)
{
declarations and statements
}
Programming Fundamentals 9
Functions…
Functions…
• Prototype
Programming Fundamentals 10
Functions…
Functions…
• Function Declaration
Programming Fundamentals 11
Functions…
Functions…
• Example:
void main ( )
{
… … … … … … …
… … … … … … …
… … … … … … …
… … … … … … …
… … … … … … …
}
Programming Fundamentals 12
Functions…
Functions…
• Function Definition
Programming Fundamentals 13
Functions…
Functions…
• Examples:
• Declaration:
int square (int n) ;
• Definition:
Programming Fundamentals 14
Functions…
Functions…
• Function Call
int x;
int i = 5;
x = square (i) ; // Function Call
cout<< “Square of ” << i << “ = “ << x;
OR
int x ;
int i = 5;
cout<< “Square of ” << i << “=“<< square(i);
Programming Fundamentals 15
Functions…
Functions…
• Example: Function to calculate integer power (Xn)
Programming Fundamentals 16
Functions…
Functions…
• Code to Call the raisetopow Function
include <iostream>
void main ( )
{
double num ;
int p;
cout << “Please enter the number”;
cin >> num;
cout << “Please enter the integer power that you want this number raised to”;
cin >> p;
cout << num << “raise to power” << p << “is equal to” << raiseToPow (num, p);
}
Output
• Please enter the number
• Please enter the integer power that you want this number raised to
• 5 raise to power 3 is equal to 125
Programming Fundamentals 17
Functions…
Functions…
• Calling a function
– Call By Value
– Call By Reference
Calling function
Called function
Programming Fundamentals 18
Programming
Programming Assignment#
Assignment# 11
• Write functions for Addition, Multiplication, Subtraction,
Division, Mod (%) (remainder) operation of any 2
numbers. Your output should display each operation
result.
Programming Fundamentals 19
THANK
THANK YOU
YOU
Programming Fundamentals 20