Sample


[br]11 [br]Finally, we end this guide as we've begun it. C is a functional language and that means it's made up of functions. You've learnt how to use functions that already exists - we've analysed printf and scanf - and how to create the <strong>main</strong> function. <strong>How do you create a function exactly?</strong> Functions are <strong>defined outside</strong> <em>main</em> and any other function, but they can only be <strong>called inside</strong> <em>main</em> or another function. <em>To define a function means to create it, while to call a function means to use it.</em> A function is made of these components: <ul> <li>A <strong>name</strong></li> <li>A <strong>return type</strong>: basically the data type of its result</li> <li>Some <strong>parameters</strong>: its inputs. The name of these can be different from the one of the variables you pass when you use the function, but their data types have to be the same.</li> <li>A body: all the functions, statements and variables that make the function itself.</li> </ul> [crayon-5daf731d67319886453378/] The result is returned using the <strong>return</strong> keyword: [crayon-5daf731d67321046714233/] You should read the article at the link below, because it contains interesting details about functions, how to define and declare them and how to pass variables in the right way - pay attention to the difference between passing a variable <strong>by copy</strong> and <strong>by reference</strong>. [br]https://itszephyro.com/blog/learn-c/functions/[br]#include <stdio.h> int factorial(int n) { int r = 1; for(int i=2; i<=n; i++) { r*=i; } return r; } int main() { printf("Number: "); int num; scanf("%d", &num); printf("%d! = %d", num, factorial(num)); } [br]

[br]11
[br]Finally, we end this guide as we’ve begun it.

C is a functional language and that means it’s made up of functions.

You’ve learnt how to use functions that already exists – we’ve analysed printf and scanf – and how to create the main function.

How do you create a function exactly?

Functions are defined outside main and any other function, but they can only be called inside main or another function.

To define a function means to create it, while to call a function means to use it.

A function is made of these components:

  • A name
  • A return type: basically the data type of its result
  • Some parameters: its inputs. The name of these can be different from the one of the variables you pass when you use the function, but their data types have to be the same.
  • A body: all the functions, statements and variables that make the function itself.

The result is returned using the return keyword:

You should read the article at the link below, because it contains interesting details about functions, how to define and declare them and how to pass variables in the right way – pay attention to the difference between passing a variable by copy and by reference.

[br]https://itszephyro.com/blog/learn-c/functions/[br]#include

int factorial(int n) {
int r = 1;
for(int i=2; i<=n; i++) { r*=i; } return r; } int main() { printf("Number: "); int num; scanf("%d", &num); printf("%d! = %d", num, factorial(num)); } [br]

To learn more, visit: