🚀 C# ile Programlamaya Giriş: Kodlamaya İlk Adımını At, Kendi Projelerini Geliştirmeye Başla!
00HOME 01POSTS 02COURSES 03ELECTRİCAL_ENERGY 04AUTOMOTİVE_ENGİNEERİNG 05TOOLS 06LAB 07GLOSSARY 08TAGS ♡MY LIST ⌕SEARCH
TRENG

Functions

Split your code into pieces: definitions, parameters, return values, default arguments and function overloading.

As a program grows, you start writing the same calculation again in different places. A function is a named piece of code that can be called again and again. It reduces repetition and also lets you think of the program as "small, testable pieces".

The structure of a function

The structure is: returnType name(parameters) { body }. The values a function takes as input are called parameters, and the actual values you pass when calling it are called arguments. If it does not return a value, the return type is void.

#include <iomanip>
#include <iostream>

// Ohm's law: I = V / R
double current(double volt, double resistance) {
    return volt / resistance;
}

double seriesResistance(double r1, double r2) {
    return r1 + r2;
}

double parallelResistance(double r1, double r2) {
    return (r1 * r2) / (r1 + r2);
}

// Overloading: same name, different parameter types
int square(int x) { return x * x; }
double square(double x) { return x * x; }

// Default arguments: vref 3.3 V, 10 bits
double adcVoltage(int reading, double vref = 3.3, int bits = 10) {
    int maxValue = (1 << bits) - 1;
    return reading * vref / maxValue;
}

int main() {
    std::cout << "I = " << current(12, 4) << " A\n";
    std::cout << "Series: " << seriesResistance(100, 220) << " ohm\n";
    std::cout << "Parallel: " << parallelResistance(100, 300) << " ohm\n";
    std::cout << "square(3) = " << square(3) << '\n';
    std::cout << "square(1.5) = " << square(1.5) << '\n';

    std::cout << std::fixed << std::setprecision(3);
    std::cout << "ADC: " << adcVoltage(512) << " V\n";
    std::cout << "ADC 12 bit: " << adcVoltage(2048, 5.0, 12) << " V\n";
    return 0;
}
I = 3 A
Series: 320 ohm
Parallel: 75 ohm
square(3) = 9
square(1.5) = 2.25
ADC: 1.652 V
ADC 12 bit: 2.501 V

Why do these examples matter?

Passing by value

In C++, parameters are copied by default. In the call current(12, 4), the function works with its own copies of the values 12 and 4. Even if you change a parameter inside, the variable outside is not affected. We will learn how to change things without copying in the lesson on references.

Overloading

You can define more than one function with the same name; the compiler picks the right one by looking at the argument types. square(3) calls the int version and square(1.5) calls the double version. Two functions that differ only in their return type do not count as overloads.

Default arguments

If you give a parameter = value, the caller can leave it out. The call adcVoltage(512) uses 3.3 V and 10 bits; in the second call everything is given explicitly. Parameters with default values must always come at the end of the list.

Declaration and definition

The C++ compiler reads the file from top to bottom. If you call a function before it is defined, you get an error. The fix is to declare the function's signature (prototype) in advance:

double current(double volt, double resistance);   // declaration (prototype)

int main() {
    // current can be used here
}

double current(double volt, double resistance) {  // definition
    return volt / resistance;
}

In large projects, declarations go into a header file (.h) and definitions go into a .cpp file.

Scope

A variable defined inside a function lives only there. Another function can use the name volt too; the two are independent of each other. This lets you think about each function on its own.

A good function does one job and its name says what that job is: current, parallelResistance, adcVoltage. If you need the word "and" to describe a function, you should probably split it in two.

In a call like parallelResistance(0, 0) the denominator becomes zero and the result is an undefined value (NaN). It is a good habit to validate or document a function's inputs: "r1 + r2 must not be zero".

Try it yourself

  1. Write a function that takes two voltages and a resistance and calculates the current that flows through the resistor because of the voltage difference between them.
  2. Write a power(double volt, double amps) function and use it together with the current function.
  3. Overload the parallelResistance function for three resistors in parallel.
  4. Write a voltage divider function: Vout = Vin * R2 / (R1 + R2).

// Mini quiz

Click an option; you will see right away, with the reason, whether it is correct or not.

1. If a function does not return a value, what should its return type be?
2. What is it called when two functions have the same name and differ only in their parameter types?
3. Where in the parameter list must parameters with default values appear?

Mark it when you finish: your progress is saved and you earn +20 XP.

Tip: ← → keys let you move between lessons.