Bu öğreticide, örnekler yardımıyla C ++ işlevi ve işlev ifadeleri hakkında bilgi edineceğiz.
Bir işlev, belirli bir görevi yerine getiren bir kod bloğudur.
Bir daire oluşturup renklendirmek için bir program oluşturmamız gerektiğini varsayalım. Bu sorunu çözmek için iki işlev oluşturabiliriz:
- çemberi çizmek için bir fonksiyon
- çemberi renklendirme işlevi
Karmaşık bir problemi daha küçük parçalara bölmek, programımızın anlaşılmasını kolaylaştırır ve yeniden kullanılabilir hale getirir.
İki tür işlev vardır:
- Standart Kitaplık İşlevleri: C ++ 'da önceden tanımlanmıştır
- Kullanıcı tanımlı İşlev: Kullanıcılar tarafından oluşturulmuştur
Bu eğitimde, çoğunlukla kullanıcı tanımlı işlevlere odaklanacağız.
C ++ Kullanıcı Tanımlı İşlev
C ++, programcının kendi işlevini tanımlamasına izin verir.
Belirli bir görevi gerçekleştirmek için kullanıcı tanımlı bir işlev grubu kodu ve bu kod grubuna bir ad (tanımlayıcı) verilir.
İşlev, programın herhangi bir bölümünden çağrıldığında, tümü işlevin gövdesinde tanımlanan kodları yürütür.
C ++ İşlev Bildirimi
Bir işlevi bildirmek için sözdizimi şöyledir:
returnType functionName (parameter1, parameter2,… ) ( // function body )
İşte bir işlev bildirimi örneği.
// function declaration void greet() ( cout << "Hello World"; )
Buraya,
- işlevin adı
greet()
- işlevin dönüş türü
void
- boş parantezler herhangi bir parametresi olmadığı anlamına gelir
- işlev gövdesi içeriye yazılmıştır
()
Not: Bu öğreticide returnType
ve parameters
daha sonra öğreneceğiz .
Bir Fonksiyon Çağırma
Yukarıdaki programda isimli bir fonksiyon tanımladık greet()
. Fonksiyonu kullanmak için greet()
onu aramamız gerekiyor.
Yukarıdaki greet()
işlevi şöyle adlandırabiliriz .
int main() ( // calling a function greet(); )

Örnek 1: Bir Metin Görüntüleme
#include using namespace std; // declaring a function void greet() ( cout << "Hello there!"; ) int main() ( // calling the function greet(); return 0; )
Çıktı
Selam!
Fonksiyon Parametreleri
Yukarıda bahsedildiği gibi, bir işlev parametrelerle (argümanlar) bildirilebilir. Parametre, bir işlev bildirilirken iletilen bir değerdir.
Örneğin, aşağıdaki işlevi ele alalım:
void printNum(int num) ( cout << num; )
Burada int
değişken num, işlev parametresidir.
Fonksiyonu çağırırken fonksiyon parametresine bir değer iletiyoruz.
int main() ( int n = 7; // calling the function // n is passed to the function as argument printNum(n); return 0; )
Örnek 2: Parametreli İşlev
// program to print a text #include using namespace std; // display a number void displayNum(int n1, float n2) ( cout << "The int number is " << n1; cout << "The double number is " << n2; ) int main() ( int num1 = 5; double num2 = 5.5; // calling the function displayNum(num1, num2); return 0; )
Çıktı
İnt sayısı 5, çift sayı 5.5
Yukarıdaki programda, bir int
parametresi ve bir double
parametresi olan bir fonksiyon kullandık .
Daha sonra argüman olarak num1 ve num2'yi geçiririz. Bu değerler sırasıyla n1 ve n2 fonksiyon parametreleri tarafından saklanır.

Not: İşlev çağrılırken iletilen argümanların türü, işlev bildiriminde tanımlanan karşılık gelen parametrelerle eşleşmelidir.
İade Beyanı
Yukarıdaki programlarda, fonksiyon bildiriminde void kullandık. Örneğin,
void displayNumber() ( // code )
Bu, işlevin herhangi bir değer döndürmediği anlamına gelir.
Bir işlevden bir değer döndürmek de mümkündür. Bunun için returnType
fonksiyon bildirimi sırasında fonksiyonun ne olduğunu belirtmemiz gerekiyor .
Daha sonra return
ifade, bir işlevden bir değer döndürmek için kullanılabilir.
Örneğin,
int add (int a, int b) ( return (a + b); )
Burada int
yerine veri türüne sahibiz void
. Bu, işlevin bir int
değer döndürdüğü anlamına gelir .
Kod return (a + b);
, işlev değeri olarak iki parametrenin toplamını döndürür.
return
İfadesi işlevi sona erdiğini belirtir. return
İşlevin içinden sonraki herhangi bir kod çalıştırılmaz.
Örnek 3: İki Sayı Ekleme
// program to add two numbers using a function #include using namespace std; // declaring a function int add(int a, int b) ( return (a + b); ) int main() ( int sum; // calling the function and storing // the returned value in sum sum = add(100, 78); cout << "100 + 78 = " << sum << endl; return 0; )
Çıktı
100 + 78 = 178
Yukarıdaki programda, add()
fonksiyon iki sayının toplamını bulmak için kullanılır.
Biz iki geçmesi int
değişmezleri 100
ve 78
işlevi arama sırasında.
Fonksiyonun döndürülen değerini değişken toplamda saklarız ve sonra yazdırırız.

Toplamın bir tür değişkeni olduğuna dikkat edin int
. Dönüş değeri Bunun nedeni add()
taşımaktadır int
türü.
İşlev Prototipi
In C++, the code of function declaration should be before the function call. However, if we want to define a function after the function call, we need to use the function prototype. For example,
// function prototype void add(int, int); int main() ( // calling the function before declaration. add(5, 3); return 0; ) // function definition void add(int a, int b) ( cout << (a + b); )
In the above code, the function prototype is:
void add(int, int);
This provides the compiler with information about the function name and its parameters. That's why we can use the code to call a function before the function has been defined.
The syntax of a function prototype is:
returnType functionName(dataType1, dataType2,… );
Example 4: C++ Function Prototype
// using function definition after main() function // function prototype is declared before main() #include using namespace std; // function prototype int add(int, int); int main() ( int sum; // calling the function and storing // the returned value in sum sum = add(100, 78); cout << "100 + 78 = " << sum << endl; return 0; ) // function definition int add(int a, int b) ( return (a + b); )
Output
100 + 78 = 178
The above program is nearly identical to Example 3. The only difference is that here, the function is defined after the function call.
That's why we have used a function prototype in this example.
Benefits of Using User-Defined Functions
- Functions make the code reusable. We can declare them once and use them multiple times.
- Functions make the program easier as each small task is divided into a function.
- Functions increase readability.
C++ Library Functions
Library functions are the built-in functions in C++ programming.
Programmers can use library functions by invoking the functions directly; they don't need to write the functions themselves.
Some common library functions in C++ are sqrt()
, abs()
, isdigit()
, etc.
In order to use library functions, we usually need to include the header file in which these library functions are defined.
For instance, in order to use mathematical functions such as sqrt()
and abs()
, we need to include the header file cmath
.
Example 5: C++ Program to Find the Square Root of a Number
#include #include using namespace std; int main() ( double number, squareRoot; number = 25.0; // sqrt() is a library function to calculate the square root squareRoot = sqrt(number); cout << "Square root of " << number << " = " << squareRoot; return 0; )
Output
25 = 5'in karekökü
Bu programda, sqrt()
bir sayının karekökünü hesaplamak için kütüphane işlevi kullanılır.
İşlev bildirimi başlık dosyasında sqrt()
tanımlanır cmath
. Biz kodun kullanılması gerekmektedir yüzden #include
kullanmak sqrt()
işlevi.
Daha fazla bilgi edinmek için C ++ Standart Kitaplık işlevlerini ziyaret edin.