C++ - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

C++ Functions

C++ Functions

shape Introduction

Basically, every C++ program consists of huge code. Compiler cannot handle all the tasks itself at a time.Hence, it divides the program into several parts. One such one is a function.

Function

shape Description

CPP Functions can be defined as a partitioned block of code in a program, which can be called any number of times during single execution of a program. The information is passed to the functions by using arguments.
  • Functions reduces the memory and complexity of the program.
  • CPP Functions mainly helps in re-usability.
  • Functions reduces the bugs and saves the time of programmer.
  • Information hiding is possible by functions.
  • Once the function execution is completed it returns to the position where it was called.

shape Example

[c]#include<iostream> int main() { message(); cout<< "splessons" << endl; } message() { cout <<" You are reading splessons "<< endl; } [/c]

Types of Functions in C++

shape Description

CPP Functions are divided into two types.
  • Library Functions
  • User-defined Functions

Library Functions

Library functions are built-in C++ compiler and can be retrieved by the programmer directly. Eg: [c] #include <iostream> #include <cmath> using namespace std; int main() { int num1,num2,power_value; cout<< "Enter two numbers: "; cin >> num1 >> num2; power_value = pow(num1,num2); cout<<"num1 to the power of num2 is "<< power_value << endl; return 0; }[/c] Output: [c]Enter two numbers: 5 2 num1 to the power of num2 is 25[/c]

User-defined functions

User-defined functions are defined by the user as the requirement at the time of programming. The user-defined functions can be accessed by using the following terms:

Function Prototype

shape Description

Code can be divided into separate functions. The division will be in such a way that each function performs a specific task logically.

Function Declaration

shape Description

Like variable declaration, every function in C need to be declared before it is used. The declaration should be done before the first call of the function. It gives the information about function name, return type and arguments of function. Function declaration is also called as “function prototype”. To be a prototype, function declaration need to have arguments. Although functions that return int values need not use prototype, but it is recommended.Function Body is not the part of function declaration. It is involved in function definition.

shape Syntax

return_type function_name ( argument_list );
In the above syntax, return_type : returns the data type of the function value. function_name : This is function's actual name. argument_list : The argument list refers to the order, type and number of the parameters of a function. Eg: int add(int a,int b)

Function Definition

shape Description

Function Definition consists of all the code which needs to be executed. It contains Declarator ( enclosed in () ) and Body part ( enclosed in {}).

shape Syntax

return_type function_name(argument list) { //body of function }
In the above syntax, the body of the function has a group of statements that defines the functionality of function does.

shape Example

[c] int add( int a, int b) { a++; b++; cout<< "sum=%d" << a+b << endl; cout<< "Welcome to splessons.com" << endl; }[/c]

Function Arguments & Return Value

shape Description

The variables that accept the arguments values can only be used in functions. These variables are also called as formal parameters of the function. Inside of the function formal parameters behaves like other local variables and are created when entered into the function and destroys when exits the function. CPP Functions normally accepts the argument and it gives back a return value to the calling Program. So, there establishes two-way Communication between calling function and called function.

Function Call

There are two ways to pass the arguments while calling a function from a program.They are :

Call by value

By default, all functions are passed by "call by value". The value of the variable is passed as a parameter. Actual value cannot be modified by using the derived parameter i.e formal parameter. Both are given different memories. [c] #include<iostream> using namespace std; int add(int n);// Declaration of function int main() { int num,res; num=3; // num is actual parameter cout << " The initial value of number : " << num << endl; res=add(num); cout << " The result after addition is : " << res << endl; return(0); } int add(int a)// a is formal parameter { a=a+1000; return(a); }[/c] Output: [c]The initial value of number : 3 The result after addition is : 1003[/c]

Call by reference:

By using call by reference, address of the variable is passed as a parameter. Actual parameter can be modified by using the derived parameter i.e formal parameter. Both are given same memories. [c] #include <iostream.h> void square(int *x) { *x = (x) * (x); } int main ( ) { int n=20; square(&n); cout<<" Value of number is "<< n ; return 0; }[/c]

Functions without parameters

shape Description

Functions in C without parameters is nothing but the functions without arguments. There occurs two conditions of functions without parameters.

Function with no arguments and no return values

shape Example

[c] # include <iostream> using namespace std; void splessons(); int main() { splessons(); return 0; } void splessons() // no arguments given hence void type { int n, i, x= 0; printf("Enter a positive integer to check: "); cin >> n; for(i = 2; i <= n/2; ++i) { if(n%i == 0) { x=1; break; } } if (x == 1) { cout << n << "is not a prime number" << endl;; } else { cout << n << "is prime number." << endl; } // no return value specified }[/c] Output: [c]Enter a positive integer to check: 5 5is prime number.[/c]

Function with no arguments and return values:

shape Example

[c] #include <iostream> using namespace std; int splessons(); int main() { int n, i, x= 0; n = splessons(); /* No argument is passed to splessons() */ for (i = 2; i <= n/2; ++i) { if (n%i == 0) { x= 1; break; } } if (x== 1) { cout << n <<" is not a prime number."; } else { cout << n <<" is a prime number."; } return 0; } int splessons() { int n; cout << "Enter a positive integer to check: " << endl; cin >> n; return n; //integer value n is returned to main }[/c] Output: [c] Enter a positive integer to check: 3 3 is a prime number.[/c]

Functions with parameters

shape Description

Functions in C with parameters is nothing but the functions with arguments. There occurs two conditions of functions with parameters.

Function with arguments and no return values

shape Examples

[c] #include <iostream> using namespace std; int splessons(int n); int main() { int n; cout << "Enter a positive integer to check: "; cin >> n; splessons(n); return 0; } int splessons(int n) // arguent n is given { int i, x= 0; for (i = 2; i <= n/2; ++i) { if (n%i == 0) { x= 1; break; } } if (x== 1) { cout << n << " is not a prime number."; } else { cout << n <<" is a prime number."; } //no value is returned }[/c] Output: [c] Enter a positive integer to check: 6 6 is not a prime number.[/c]

Function with arguments and return values:

shape Example

[c] #include <iostream> using namespace std; int splessons(int n);//argument is passed to splessons() int main() { int n, x= 0; cout << "Enter positive enter to check: "; cin >> n; x= splessons(n); if(x== 1) cout<< n << " is not a prime number."; else { cout<< n << " is a prime number."; return 0; } int splessons(int n) { int i; for(i = 2; i <= n/2; ++i) { if(n%i == 0) return 1; } return 0; }[/c] Output: [c] Enter positive enter to check: 4 4 is not a prime number.[/c]

Summary

shape Key Points

  • A re-usability block CPP Functions may or may-not possess arguments but can be called whenever required.
  • CPP Functions can be called by value or by reference.