SHARE
TWEET

C Examples: Functions

gigabytemon Jun 22nd, 2020 (edited) 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // FUNCTIONS EXAMPLE - C
  2. // (c) gigabytemon
  3. // 22/06/2020
  4. // last edit: 06/07/2020
  5. // >> included function prototypes, because as a Java baby I never learned it T_T
  6. // 2862 abc5 66cb a008 dbd9 5bf7 0dda 04f8
  7. // e192 33e4 e689 4efb 24b9 d34f 6c2d 1ee1
  8.  
  9. /* introduction
  10.  * ------------
  11.  * functions are a group of statements that do a specific task.
  12.  * every c program has at least 1 function, which is main().
  13.  * functions have three parts that make it up:
  14.  * 1) (if it is user-defined/written by the programmer) the function's prototype, usually declared at the beginning of the program (above main)
  15.  * 2) the function's name, return type, and parameters, are its DECLARATION
  16.  * 3) the body of the function (all the statements inside it) is its DEFINITION
  17.  */
  18.  
  19. /* syntax
  20.  * ------
  21.  * the general syntax of a function is:
  22.  *
  23.  * RETURN_TYPE FUNCTION_NAME(PARAMETERS)
  24.  * {
  25.  *      FUNCTION_BODY
  26.  * }
  27.  *
  28.  * the RETURN_TYPE is what the function returns when it is called.
  29.  * the FUNCTION_NAME is what the function is named.
  30.  * PARAMETERS are what inputs the function is expecting. this can be zero inputs, or more.
  31.  * FUNCTION_BODY is where all the statements in the function are written.
  32.  */
  33.  
  34. /* personal notes
  35.  * --------------
  36.  * if a function is doing more than one thing, it can be broken down into more than one function.
  37.  * don't worry if your main() is just filled with function calls, or only has one function call in it. just worry that your function names make sense and do what they're named to do.
  38.  * !!!REMEMBER: FUNCTIONS CAN TAKE MANY PARAMETERS, BUT CAN ONLY RETURN 1 VALUE!!!
  39.  */
  40.  
  41. //prototyping functions
  42. void displayCoolStuff();
  43. int getNumber();
  44. int addNumbers(int num1, int num2);
  45. int minusNumbers(int n1, int n2);
  46.  
  47. //main()
  48. #include <stdio.h>
  49. int main()
  50. {
  51.     //you don't have to put the bulk of your program in main
  52.     //this line below immediately jumps to the function called displayCoolStuff();
  53.     displayCoolStuff();
  54. }
  55.  
  56. //displayCoolStuff() takes no parameters, and returns nothing (void)
  57. void displayCoolStuff()
  58. {
  59.     //functions can be used get inputs from the user
  60.     int number1, number2, sum;
  61.     number1 = getNumber();
  62.     printf("The number %d has been stored to number1!\n", number1);
  63.    
  64.     number2 = getNumber();
  65.     printf("The number %d has been stored to number2!\n", number2);
  66.    
  67.     //functions can be used to calculate things
  68.     sum = addNumbers(number1, number2);
  69.     printf("The sum of %d and %d is %d\n", number1, number2, sum);
  70.    
  71.     printf("The difference between %d and %d is %d\n", number1, number2, minusNumbers(number1, number2));
  72. }
  73.  
  74. //getNumber() takes no parameters, and returns 1 value (int)
  75. int getNumber()
  76. {
  77.     int input;
  78.     printf("Please enter a number: ");
  79.     scanf("%d", &input);
  80.    
  81.     return input;
  82. }
  83.  
  84. //both of the functions below take in 2 parameters and return 1 value (int)
  85. int addNumbers(int num1, int num2)
  86. {
  87.     return num1 + num2;
  88. }
  89.  
  90. int minusNumbers(int n1, int n2)
  91. {
  92.     return n1 - n2;
  93. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top