Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // FUNCTIONS EXAMPLE - C
- // (c) gigabytemon
- // 22/06/2020
- // last edit: 06/07/2020
- // >> included function prototypes, because as a Java baby I never learned it T_T
- // 2862 abc5 66cb a008 dbd9 5bf7 0dda 04f8
- // e192 33e4 e689 4efb 24b9 d34f 6c2d 1ee1
- /* introduction
- * ------------
- * functions are a group of statements that do a specific task.
- * every c program has at least 1 function, which is main().
- * functions have three parts that make it up:
- * 1) (if it is user-defined/written by the programmer) the function's prototype, usually declared at the beginning of the program (above main)
- * 2) the function's name, return type, and parameters, are its DECLARATION
- * 3) the body of the function (all the statements inside it) is its DEFINITION
- */
- /* syntax
- * ------
- * the general syntax of a function is:
- *
- * RETURN_TYPE FUNCTION_NAME(PARAMETERS)
- * {
- * FUNCTION_BODY
- * }
- *
- * the RETURN_TYPE is what the function returns when it is called.
- * the FUNCTION_NAME is what the function is named.
- * PARAMETERS are what inputs the function is expecting. this can be zero inputs, or more.
- * FUNCTION_BODY is where all the statements in the function are written.
- */
- /* personal notes
- * --------------
- * if a function is doing more than one thing, it can be broken down into more than one function.
- * 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.
- * !!!REMEMBER: FUNCTIONS CAN TAKE MANY PARAMETERS, BUT CAN ONLY RETURN 1 VALUE!!!
- */
- //prototyping functions
- void displayCoolStuff();
- int getNumber();
- int addNumbers(int num1, int num2);
- int minusNumbers(int n1, int n2);
- //main()
- #include <stdio.h>
- int main()
- {
- //you don't have to put the bulk of your program in main
- //this line below immediately jumps to the function called displayCoolStuff();
- displayCoolStuff();
- }
- //displayCoolStuff() takes no parameters, and returns nothing (void)
- void displayCoolStuff()
- {
- //functions can be used get inputs from the user
- int number1, number2, sum;
- number1 = getNumber();
- printf("The number %d has been stored to number1!\n", number1);
- number2 = getNumber();
- printf("The number %d has been stored to number2!\n", number2);
- //functions can be used to calculate things
- sum = addNumbers(number1, number2);
- printf("The sum of %d and %d is %d\n", number1, number2, sum);
- printf("The difference between %d and %d is %d\n", number1, number2, minusNumbers(number1, number2));
- }
- //getNumber() takes no parameters, and returns 1 value (int)
- int getNumber()
- {
- int input;
- printf("Please enter a number: ");
- scanf("%d", &input);
- return input;
- }
- //both of the functions below take in 2 parameters and return 1 value (int)
- int addNumbers(int num1, int num2)
- {
- return num1 + num2;
- }
- int minusNumbers(int n1, int n2)
- {
- return n1 - n2;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement