Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Simpel Funktion Praksis | Introduktion til Funktioner
C++ Introduction

book
Simpel Funktion Praksis

Eksempel på funktionen med argumenter fra det forrige kapitel

h

function

copy
double convert_usd_to_eur(double usd_amount)
{
const double exchange_rate = 0.91;
double euros = usd_amount * exchange_rate;
return euros;
}
123456
double convert_usd_to_eur(double usd_amount) { const double exchange_rate = 0.91; double euros = usd_amount * exchange_rate; return euros; }
Opgave

Swipe to start coding

Opret en funktion withdraw, der simulerer en bankkonto hævning. Den accepterer den nuværende saldo og hævningsbeløbet som argumenter. Hvis saldoen er tilstrækkelig, trækkes beløbet fra og den nye saldo returneres. Ellers returneres den oprindelige saldo.

Løsning

cpp

solution

#include <iostream>

int withdraw(int balance, int amount)
{
if (balance >= amount)
return balance - amount;
return balance;
}

int main()
{
int balance = 500, amount = 200;
balance = withdraw(balance, amount); // Call the withdraw function
std::cout << "Balance after withdraw is: $" << balance << std::endl;
}

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 5. Kapitel 4
#include <iostream>

___ withdraw(___ balance, ___ amount)
{
if (___)
return ___;
return ___;
}

int main()
{
int balance = 500, amount = 200;
balance = withdraw(balance, amount);
std::cout << "Balance after withdraw is: $" << balance << std::endl;
}
toggle bottom row
We use cookies to make your experience better!
some-alt