Function Prototype
Large programs are best developed and maintained by dividing them into smaller modules through the technique called "Divide and Conquer." This approach enhances code organization, reusability, and maintainability. Learn about function prototypes, definitions, calling, and examples to effectively implement modular programming in your projects.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
Function Prototype Experience has shown that the best way to develop and maintain large programs is to construct it from smaller pieces(Modules) This technique is Called Divide and Conquer . Wise Development Approach Bad Development Approach main() { ----- } main() { ----- ----- ----- . . ---- ----- ----- Return 0; } function f1() { --- } function f2() { --- }
Function Prototype Tells compiler argument type and return type of function int square( int ); Function takes an int and returns an int Function Call : functionName (argument); or functionName(argument1, argument2, ); Example cout << sqrt( 900.0 ); sqrt (square root) function The preceding statement would print 30 All functions in math library return a double
Function Calling Calling/invoking a function sqrt(x); Parentheses an operator used to call function Pass argument x Function gets its own copy of arguments After finished, passes back result Output argument Function Name 3 cout<< sqrt(9); Parentheses used to enclose argument(s)
Function Definition Syntax format for function definition returned-value-type function-name (parameter-list) { Declarations of local variables and Statements } Parameter list Comma separated list of arguments Data type needed for each argument If no arguments, use void or leave blank Return-value-type Data type of result returned (use void if nothing returned)
Function Definition Example function int square( int y ) { return y * y; } return keyword Returns data, and control goes to function s caller If no data to return, use return; Function ends when reaches right brace Control goes to caller Functions cannot be defined inside other functions
// Creating and using a programmer-defined function. #include <iostream.h> int square( int ); // function prototype int main() { // loop 10 times and calculate and output // square of x each time for ( int x = 1; x <= 10; x++ ) cout << square( x ) << " "; // function call cout << endl; return 0; // indicates successful termination } // end main Function prototype: specifies data types of arguments and return values. square expects an int, and returns an int. Parentheses () cause function to be called. When done, it returns the result. Function Example // square function definition returns square of an integer int square( int y ) // y is a copy of argument to function { return y * y; // returns square of y as an int } // end function square Definition of square. y is a copy of the argument passed. Returns y * y, or y squared. 1 4 9 16 25 36 49 64 81 100
Classes And Objects Defining A Class Data Members Access specifier Constructors Method Implementation in C++ Accessing Class Member Destructors
Classes And Objects Static Data Members and Functions Array Of Objects Class as ADTs
Classes And Objects Classes are user-defined (programmer-defined) types. Data (data members) Functions (member functions or methods) In other words, they are structures + functions
Classes And Objects A class definition begins with the keyword class. The body of the class is contained within a set of braces, { } ; (notice the semi-colon). class class_name { . . . }; Any valid identifier Class body (data member + methods)
Class Specification Syntax: class class_name { Data members Members functions };
Class Specification class Student { int st_id; char st_name[10]; void read_data(); void print_data(); }; Data Members or Properties of Student Class Members Functions or Behaviours of Student Class
Class Specification Visibility of Data members & Member functions public - accessed by member functions and all other non-member functions in the program. private - accessed by only member functions of the class. protected - similar to private, but accessed by all the member functions of immediate derived class default - all items defined in the class are private.
Classes And Objects class class_name { private: public: };
Class Access Example class Student { private : int st_id; public : void read_data(); void print_data(); }; char st_name[10];
Classes Within the body, the keywords private: and public: specify the access level of the members of the class. the default is private. Usually, the data members of a class are declared in the private: section of the class and the member functions are in public: section.
Classes This class example shows how we can encapsulate (gather) a circle information into one package (unit or class) No need for others classes to access and retrieve its value directly. The class methods are responsible for that only. class Circle { private: public: }; double radius; They are accessible from outside the class, and they can access the member (radius) void setRadius(double r); double getDiameter(); double getArea(); double getCircumference();
Method Implementation Class implementation: writing the code of class methods. There are two ways: 1. Member functions defined outside class Using Binary scope resolution operator (::) Ties member name to class name Uniquely identify functions of particular class Different classes can have member functions with same name Format for defining member functions ReturnType ClassName::MemberFunctionName( ){ }
Method Implementation 2. Member functions defined inside class Do not need scope resolution operator, class name; class Circle { private: public: }; Defined inside class double radius; void setRadius(double r){radius = r;} double getDiameter(){ return radius *2;} double getArea(); double getCircumference();
Method Implementation class Circle { private: public: }; double Circle::getArea() { return radius * radius * (22.0/7); } double Circle:: getCircumference() { return 2 * radius * (22.0/7); } double radius; void setRadius(double r){radius = r;} double getDiameter(){ return radius *2;} double getArea(); double getCircumference(); Defined outside class
Method Implementation The second constructor is called Since radius is a private class data member void main() { Circle c1,c2(7); cout<< The area of c1: <<c1.getArea()<< \n ; //c1.raduis = 5;//syntax error c1.setRadius(5); cout<< The circumference of c1: << c1.getCircumference()<< \n ; cout<< The Diameter of c2: <<c2.getDiameter()<< \n ; }
class Circle { private: public: }; Circle::Circle(int r) { radius = r; } double Circle::getArea() { return radius * radius * (22.0/7); } double Circle:: getCircumference() { return 2 * radius * (22.0/7); } double radius; Circle() { radius = 0.0;} Circle(int r); void setRadius(double r){radius = r;} double getDiameter(){ return radius *2;} double getArea(); double getCircumference(); void main() { Circle c(7); Circle *cp1 = &c; Circle *cp2 = new Circle(7); cout<< The are of cp2: <<cp2->getArea(); } Method Implementation
Accessing Class Member Operators to access class members Identical to those for structs Dot member selection operator (.) Object Reference to object Arrow member selection operator (->) Pointers Accessing a Class Member through these operators requires an object of the class
Objects Declaring a variable of a class type creates an object. You can have many variables of the same type (class). Instantiation Once an object of a certain class is instantiated, a new memory location is created for it to store its data members and code You can instantiate many objects from a class type. Ex) Circle c; Circle *c;
ASSIGNMENT Design a class Student With Its RollNo, Fees : Data member setRollNo , displayStudentdata : Member functions Create two different Student objects and set their RollNo and Fees and display Their Data()
class Student { private: public: }; void main( ) { Student s1,s2; s1.setRollNo( ); s1.displayStudentData( ); s2.setRollNo( ); s2.displayStudentData( ); } int nRollno; float fFees; void setRollNo( ){ cin>>nRollNo>>fFees; } void displayStudentData() { cout<<nRollNo<<fFees; }
ASSIGNMENT Design a class Circle With Its radius: Data member setRadius , getArea, getCircumference, getDiameter : Member functions Create two different Circle objects and set their Radius display Area, Diameter and Circumference of Circle
class Circle { private: public: }; double Circle::getArea() { return radius * radius * (22.0/7); } double Circle:: getCircumference() { return 2 * radius * (22.0/7); } double Circle:: getDiameter() { return 2 * radius; } double radius; void setRadius(double r){radius = r;} double getDiameter(){ return radius *2;} double getArea(); double getCircumference();
void main() { Circle c1,c2; c1.setRadius(5); c2.setRadius(7); cout<< The area of c1: <<c1.getArea()<< \n ; cout<< The area of c2: <<c2.getArea()<< \n ; //c1.raduis = 5;//syntax error cout<< The circumference of c1: << c1.getCircumference()<< \n ; cout<< The Diameter of c2: <<c2.getDiameter() }