Department of Information Technology Functions Prepared by...

Info icon This preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
Department of Information Technology Functions Prepared by: Ms Seetha BIT205 - Programming in C++
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Department of Information Technology Functions Modules are defined in C++ as functions. 2 general kinds of functions : 1. User defined functions void functions functions that return a value 2.Standard library functions User-defined functions : Creating function consists of 3 parts : 1. Function prototype 2. Function call 3. Function definition Functions must be defined before they are used. They are called function prototypes .
Image of page 2
Department of Information Technology Functions Function prototypes are typically placed near the beginning of the program file after the #include statements. Function prototypes permits type checking at compilation and linking time. A function prototype consists of : 1. the return type 2. the identifier/name of the function and 3. the types of all parameters that the function expects.
Image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Department of Information Technology Functions Format of function prototyping: returntype functionname (parameterType parameterName, .... ); parametertype defines the type of the data. E.g. char, int, float, void etc. parametername is the name by which this data is known within this function, and may be (in fact, usually is) different to its name in the calling module. functionname is a meaningful name to address the function Some examples of function prototypes : int PromptAndRead(); float CircleArea(float radius=3); bool IsVowel(char CurrentCharacter); void Menu();
Image of page 4
Department of Information Technology Functions In addition default arguments may be specified when declaring C++ prototypes. Example : float mission (int destroyers = 20); to provide destroyers with the default value of 20. The function call: ….…… cout<<mission(); ………. Results in destroyers being set to a value of 20. While the call: ……… cout<<mission(5); ……… results in destroyers being set to a value of 5 function call function call
Image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Department of Information Technology Functions Example 1 : # include <iostream.h> int square(const int num = 10 ); int main(){ int value; cout<<“Please enter a number:”<<flush; cin<<value; cout<<“Square is :”<<square(value)<<endl; return 0; } int square(const int num) { return num *num; } The above function accepts an integer as parameter. The const modifier is used to guarantee that the function will not modify the num value i.e it will remain constant within the function. Function prototype Function call will call the function and the value returned will be displayed function definition
Image of page 6
Department of Information Technology Functions Example 2 : Program to compute area of a user-specified circle # include <iostream.h> float CircleArea(float r){ const float Pi=3.1415; return Pi*r*r; } int main(){ float radius; cout<<“Circle radius (real number)” <<flush; cin<<radius; float area=CircleArea(radius); cout<<“Area of a circle with radius”<<
Image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern