Working with Functions in C++

What is a function ?

  • its the encapsulation of some calculation

  • We invoke a function and provide informatino in the form of arguments

  • The function receives the arguments as “parameters”, and uses the parameters to make its calculation!

  • A value is returned by the function to the Caller!

Functions are very useful to break down programs into smaller and more understable pieces

  • Functions should have one purpose and only one purpose ( a function abstracts one idea)
  • Should be generic, resuable, and readable

Built-In Functions (Standard Library Functions)

Here’s an example of using the built-in math function to calculate the ‘pow’ x raised to power (x^p)

#include <cmath>
#include <iostream>
using std::cout;
using std::endl;

int main()
{
    cout << pow(10,2)<< endl;
}

Output:

100

User-Defined Functions

Calling a function is similar to Python with the ‘;’

Making our own functions! Void doesnt return anything, it used when were calling a function to print something! Here is an example:

#include <iostream>

using std::cout;
using std::endl;

void greet(){

    cout << "Hi my name is Devin!" << endl;
}
int main()

{
    greet();
}

Output:

Hi my name is Devin!

Function Parameters

A function can be declared with parameters (arguments), a parameter is a value that is passed when declaring a function.

Example:

#include <iostream>
#include <string>

using namespace std;

void print_name(string name)
{
    cout << name << endl;
}

int main()
{
    string name = "Devin";

    string name2 = "Paul";

    string name3 = "Jordan";

    print_name(name);

    print_name(name2);

    print_name(name3);

}

Output:

Devin
Paul
Jordan

Void Functions and Return Statements

Function that doesnt return any value is a void function, which is used to print!

example: Multiply two numbers



#include <iostream>
#include <string>

#include<cmath>

using std::cout;
using std::endl;

int multiply(int a, int b)
{
    return (a * b);
}

int main()
{
  int sum;

  sum = multiply(100, 5);

  cout << "100 * 5 = " << sum << endl;

  return 0;

}

Output:

100 * 5 = 500

Scope

What is Scope?

  • WHen we create a variable, we make an association between a name and a value. The value exists at some memory location, the name is associated with both

The part of the program where the name and that associated is valid is called the variables scope. Blocks of code constitute a scope, a variable declared within a clock is only valid within that block.

  • think local and global scopes

  • Parameters of a function are also considered local, part of the scope of the function

  • Unless we say otherwise, C++ copies things that are passed both in and out of a function

Functions with Reference and Pointers

  • By defualt, you copy the values from argument to parameter

  • We can change that if we declare the type of the parameter to be a reference (&) then both the arg and param refer to the same value, which means a change to the function parameter changes the invoker’s argument!

Example of using a reference and not using a reference:

Example of not using a reference (&):

#include <iostream>
using std::cout;
using std::endl;


void print_(int number ){
    number = number * 10;

    cout << "Number after being multipled " << number << endl;
}

int main(){

    int number = 20;

    cout << "Number before we pass to function " << number << endl;
    print_(number);

    cout << "Number after we pass to function " << number << endl;

}

Ouput:

Number before we pass to function 20
Number after being multipled 200
Number after we pass to function 20

Example after we add the reference (&):

#include <iostream>
using std::cout;
using std::endl;


void print_(int &number ){
    number = number * 10;

    cout << "Number after being multipled " << number << endl;
}

int main(){

    int number = 20;

    cout << "Number before we pass to function " << number << endl;
    print_(number);

    cout << "Number after we pass to function " << number << endl;

}

Output:

Number before we pass to function 20
Number after being multipled 200
Number after we pass to function 200

As you can see, the number after it was changed in the function has remained changed!! All because of the reference!

Const

Overloaded Functions

Function Templates

What is a Template?

Its a really good way to get around the problem of making a function for each type (int, long)

  • its a pattern that can be used to create a function with whatever types we want!!

  • The template itself is not a function, it creates a function with some type information set

  • For return type you can use auto, since auto will automatically know what type to return based on the parameter passeds in.

Example of a Template, we can pass types int, long, double, string to the Template and the function works!!!

#include<iostream>
using std::cout; using std::endl;

#include <string>
using std::string;


template <typename template_type>
void function (template_type &first, template_type &second) {
    // basic function just prints the values passed in !
    cout << "Value for first: " << first << endl;
    cout << "Value for second: " << second << endl;
}

int main() {

    int a = 10;
    int b = 34;
    double c = 899.42;
    double d = 9002.20202;
    long e = 184834848484844488;
    long f = 120380398923429042;

    string g = "Hello";
    string h = "World";

    // call to fuction

    function(a,b);
    function(c,d);
    function(e,f);
    function(g,h);
    
}