From faeb8454e007df2346061f4599fc5ad1b2b79ef6 Mon Sep 17 00:00:00 2001 From: Saboor Elahi Date: Fri, 14 Dec 2018 09:07:57 +0500 Subject: [PATCH] Functions are elaborated & more examples are added (#24798) * Functions are elaborated & more examples are added * Cleaned up code and added formatting --- guide/english/cplusplus/functions/index.md | 47 +++++++++++++++++++--- 1 file changed, 42 insertions(+), 5 deletions(-) diff --git a/guide/english/cplusplus/functions/index.md b/guide/english/cplusplus/functions/index.md index 9b4ac0ec35..d623a9322b 100644 --- a/guide/english/cplusplus/functions/index.md +++ b/guide/english/cplusplus/functions/index.md @@ -68,7 +68,7 @@ void increment_by_reference (int & a) { ### Function body: The function body contains a collection of statements that define what the function does. -##Example: +#### Example: ```cpp int max(int num1, int num2) @@ -83,12 +83,49 @@ int max(int num1, int num2) return result; } -## calling a function: - +``` +#### Calling the function: +```cpp int res; -res=max(5,10); +res = max(5,10); +``` + +## Functions with void return type + +If a function returns a value, assign it to a variable, else just invoke the function like so: +```cpp +void printMax(int num1, int num2) +{ + // local variable declaration + int result; + + if (num1 > num2) + result = num1; + else + result = num2; + + cout << result; +} +``` +#### calling the function: +```cpp +printMax(5,10); +``` + +## Functions without parameters + +Also note that it is not necessary to create a function with parameters, we can also make functions without parameters like so: + +```cpp +// Function without parameter +int test() +{ + // code goes here +} + +// In main you can call it like: +int val = test(); -// if the function returns a value, assign it to that variable, else just call it like function(arg1,arg2); ``` ## Why are functions important?