Class 10 Exam  >  Class 10 Notes  >  C++ Programming for Beginners  >  C++ Functions

C++ Functions | C++ Programming for Beginners - Class 10 PDF Download

C++ Functions

  • A function is a block of code which only runs when it is called.
  • You can pass data, known as parameters, into a function.
  • Functions are used to perform certain actions, and they are important for reusing code: Define the code once, and use it many times.

Create a Function

  • C++ provides some pre-defined functions, such as main(), which is used to execute code. But you can also create your own functions to perform certain actions.
  • To create (often referred to as declare) a function, specify the name of the function, followed by parentheses ():

Syntax

void myFunction() {

  // code to be executed

}

Example Explained

  • myFunction() is the name of the function
  • void means that the function does not have a return value. You will learn more about return values later in the next chapter
  • inside the function (the body), add code that defines what the function should do

Call a Function

  • Declared functions are not executed immediately. They are "saved for later use", and will be executed later, when they are called.
  • To call a function, write the function's name followed by two parentheses () and a semicolon ;
  • In the following example, myFunction() is used to print a text (the action), when it is called:

Example

Inside main, call myFunction():

// Create a function

void myFunction() {

  cout << "I just got executed!";

}


int main() {

  myFunction(); // call the function

  return 0;

}


// Outputs "I just got executed!"

A function can be called multiple times:
Example

void myFunction() {

  cout << "I just got executed!\n";

}


int main() {

  myFunction();

  myFunction();

  myFunction();

  return 0;

}


// I just got executed!

// I just got executed!

// I just got executed!

Function Declaration and Definition

A C++ function consist of two parts:

  • Declaration: the return type, the name of the function, and parameters (if any)
  • Definition: the body of the function (code to be executed)

void myFunction() { // declaration

  // the body of the function (definition)

}

Note: If a user-defined function, such as myFunction() is declared after the main() function, an error will occur:

Example

int main() {

  myFunction();

  return 0;

}


void myFunction() {

  cout << "I just got executed!";

}


// Error

However, it is possible to separate the declaration and the definition of the function - for code optimization.
You will often see C++ programs that have function declaration above main(), and function definition below main(). This will make the code better organized and easier to read:

Example

// Function declaration

void myFunction();


// The main method

int main() {

  myFunction();  // call the function

  return 0;

}


// Function definition

void myFunction() {

  cout << "I just got executed!";

}

The document C++ Functions | C++ Programming for Beginners - Class 10 is a part of the Class 10 Course C++ Programming for Beginners.
All you need of Class 10 at this link: Class 10
15 videos|20 docs|13 tests

Top Courses for Class 10

15 videos|20 docs|13 tests
Download as PDF
Explore Courses for Class 10 exam

Top Courses for Class 10

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

video lectures

,

pdf

,

past year papers

,

Important questions

,

Sample Paper

,

Extra Questions

,

C++ Functions | C++ Programming for Beginners - Class 10

,

C++ Functions | C++ Programming for Beginners - Class 10

,

MCQs

,

Semester Notes

,

Exam

,

study material

,

shortcuts and tricks

,

Free

,

Objective type Questions

,

Viva Questions

,

ppt

,

C++ Functions | C++ Programming for Beginners - Class 10

,

Previous Year Questions with Solutions

,

Summary

,

practice quizzes

,

mock tests for examination

;