Skip to main content

Function in C++


Function

Function

Functions are set of Instructions.
Functions are also known as block of code.
Functions are used to reduce the repetition of the code.
In a Single Program we can define more than one Function.
Functions can be used as many time the Program needs to execute the task.

Advantages

  • Used to reduce the Line of Code.
  • It increases the Program readability.
  • It reduce the chance of error of the Program.
  • Functions are used to divide a large Problem into many parts to reduce the complexity.

Types of Functions

  • Predefined Functions
  • User Define Functions

Predefined Functions:

Predefined functions are also Library functions which are stored in the the Header file.
Predefined functions can only be called.

User Define Function

User define functions can be defined as the user wants to define the Functions.
It can be of Two Types:
  • Non-Parameterized
  • Parameterized

Syntax of Non-Parameterized Function

Function Definition:
Function can be define either before the main() or after the main() functions.

Function Calling:
Function can be called in the main() function.
       functionName();


Syntax of Parameterized Function

Function Definition:
Function can be define either before the main() or after the main() functions.

return type functionName(Parameters seprated by Comma)
{
Statements;
}

Function Calling:
Function can be called in the main() function.
         functionName(Parameters);

Program

Write a program to print the Message 10 times using function.




Parameterized Function
Write a Program to Print the Square of a number using function.




2. Write a Program to print Square of numbers between two numbers.

Function With return type

Write a program to print the factorial of a number.

  

Call By Value 

  • In this the copy of value is passed to function.
  • Actual and formal Arguments created in different memory locations so that the changes inside a functions does not reflect another function.


Call By Reference

  • In the the reference of the value is passed at calling time to the called function.
  • Actual and formal Arguments created in same memory locations so that the changes inside a functions  reflect another function.
1. Call By reference Basic Example


2. If we Change the value inside the function definition It will reflect the values of calling function.
Here is an Example of It:
 

Comments

Popular posts from this blog

Programming Basics

Logic Building Data_Types Operators Algorithms Pseudo_Code Hello Guys, Let's Play with Logic.........but wait, before that you must know that how to build Logic in Programming as you all know that programming is all about Logic..... So Let's start the game... If you are new Here that means you are absolutely right Place, Or If you already know, than you will learn new things here! If we Talk about the Programming we should be familiar with  these terms..... 1. Variable 2. Data Types 3.Keywords Variable: Variable is something which holds the value.                  Like:      a=10;                 Here,                 a---------------------->  Variable                  10 ---------------------> Value * The Value of Variable can be changed , *  " Variable is a named memory Location which holds a value." Data Types:   Data Type is what which tells that which kind of data we are using. In programming we have different Dat

FAQs

Basic Questions on Programming Languages 1. What is the meaning of Programming? Ans: Programming is the process of Scheduling the task to perform step by step to Execute a specific Task. 2. Types of Programming Languages? Ans: Types:             Procedural Programming Language            Object Oriented Programming Language            Object Based Language            Scripting Language Procedural Programming Language   C Programming  Object Oriented Programming Language     C++     Java     C#     VB.Net Object Based Language     JavaScript Scripting Language     Python     VBScript     PHP     Perl     Ruby     R 3. Types Of Operators in Programming Languages. Ans:      Operators Symbols Type Example Assignment = Unary a=10 Arithmetic +,-,/,*,% Binary a+b  a-b  a*b  a/b  a%b Relational >, <, >=, <=, ==, !=(Not Equal) Binary It gives Result in Boolen value True/False a>b  a<b  a>=b  a<=b  a==b  a!=b Logical && (AND), ||(OR), !(Not) Binary It gives Resul