Functional Programming - Function Types


Advertisements

Functions are of two types −

  • Predefined functions
  • User-defined functions

In this chapter, we will discuss in detail about functions.

Predefined Functions

These are the functions that are built into Language to perform operations & are stored in the Standard Function Library.

For Example − ‘Strcat’ in C++ & ‘concat’ in Haskell are used to append the two strings, ‘strlen’ in C++ & ‘len’ in Python are used to calculate the string length.

Program to print string length in C++

The following program shows how you can print the length of a string using C++ −

#include <iostream> 
#include <string.h> 
#include <stdio.h> 
using namespace std;  

int main() {     
   char str[20] = "Hello World"; 
   int len; 
   len = strlen(str); 
   cout<<"String length is: "<<len; 
   return 0; 
} 

It will produce the following output −

String length is: 11

Program to print string length in Python

The following program shows how to print the length of a string using Python, which is a functional programming language −

str = "Hello World"; 
print("String length is: ", len(str)) 

It will produce the following output −

('String length is: ', 11)

User-defined Functions

User-defined functions are defined by the user to perform specific tasks. There are four different patterns to define a function −

  • Functions with no argument and no return value
  • Functions with no argument but a return value
  • Functions with argument but no return value
  • Functions with argument and a return value

Functions with no argument and no return value

The following program shows how to define a function with no argument and no return value in C++

#include <iostream> 
using namespace std; 

void function1() { 
   cout <<"Hello World"; 
}  
int main() { 
   function1(); 
   return 0; 
} 

It will produce the following output −

Hello World 

The following program shows how you can define a similar function (no argument and no return value) in Python

def function1():    
   print ("Hello World") 
    
function1() 

It will produce the following output −

Hello World 

Functions with no argument but a return value

The following program shows how to define a function with no argument but a return value in C++

#include <iostream> 
using namespace std; 
string function1() { 
   return("Hello World"); 
}  

int main() { 
   cout<<function1(); 
   return 0; 
}

It will produce the following output −

Hello World 

The following program shows how you can define a similar function (with no argument but a return value) in Python

def function1(): 
   return "Hello World" 
res = function1() 
print(res) 

It will produce the following output −

Hello World 

Functions with argument but no return value

The following program shows how to define a function with argument but no return value in C++

#include <iostream> 
using namespace std; 
void function1(int x, int y) {    
   int c; 
   c = x+y;  
   cout<<"Sum is: "<<c; 
}  

int main() { 
   function1(4,5); 
   return 0; 
}

It will produce the following output −

Sum is: 9 

The following program shows how you can define a similar function in Python

def function1(x,y): 
   c = x + y 
   print("Sum is:",c) 
function1(4,5)

It will produce the following output −

('Sum is:', 9)

Functions with argument and a return value

The following program shows how to define a function in C++ with no argument but a return value −

#include <iostream> 
using namespace std; 
int function1(int x, int y) {    
   int c; 
   c = x + y;  
   return c;    
} 

int main() {  
   int res; 
   res = function1(4,5); 
   cout<<"Sum is: "<<res; 
   return 0; 
}

It will produce the following output −

Sum is: 9 

The following program shows how to define a similar function (with argument and a return value) in Python

def function1(x,y): 
   c = x + y 
   return c  

res = function1(4,5) 
print("Sum is ",res) 

It will produce the following output −

('Sum is ', 9)
Advertisements