Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save s3f/0a797c283ba2d65d9e9028fddbf8743f to your computer and use it in GitHub Desktop.
Save s3f/0a797c283ba2d65d9e9028fddbf8743f to your computer and use it in GitHub Desktop.
Chapter 31 - Passing Variables to Your Functions created by s3f - https://repl.it/FH1x/11
/* Passing Arguments
When you pass a variable from one function to another, you are passing an argument from the first function to the next. The recieving function recieevs the parameters from the function that sent the variables.
Methods of Passing Arguments
The variables you want to pass go inside the parenthesis of the function call () and also in the recieving function.
You pass arguments from function to function in 2 ways, by value and by address,
Passing by Value
Passing by value means that that the value of the variable is passed to the recieving function, not the variable itself.
The following program below shows how to pass by value
#include <stdio.h>
main()
{
int i;
printf("Please enter an integer of your choosing... ");
scanf(" %d", &i);
// Now call the half function, passing the value
half(i);
// Shows that the function did not alter i's value
printf("In main(), is is still %d.\n", i);
return (0);
}
****************
half (int i) // Recieves the value of i
{
i = i /2;
printf("Your value halved is %d.\n", i);
return; // Returns to main
}
*/
/* Passing by Address
When pass an array to another function, the array is passed by address. The program below demostrates passing an array to a function.
*/
#include <stdio.h>
#include <string.h>
main ()
{
char name[10] = "Sef Sheek";
change(name);
printf("Back in main(), the name is now %s.\n", name);
return (0); // Ends the program
}
/* *************************** */
change(char name[10])
{
// Change the string stored at the address pointed to by name
strcpy(name, "XXXXXXXX");
return; // Returns to main
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment