|
CS256 - Principles of Structured Design
Fall 2021
|
Displaying ./code/cs256su21code/jun10/byRefren.c
/*
this uses call by reference
In call by reference, the location (address) of actual arguments is passed
to formal arguments of the called function. This means by accessing the
addresses of actual arguments we can alter them within from the called function.
In call by reference, alteration to actual arguments is possible within from
called function; therefore the code must handle arguments carefully else you get
unexpected results.
*/
#include <stdio.h>
//the *x means pointer to x, or where the integer x is located in memory
void doubleNum(int *x){ //we need the star in front of the variable so that we can
// access it's location in memory
*x = *x * 2;
}
int main(){
int num;
printf("Enter a number: ");
scanf("%d", &num);
doubleNum(&num); //similar to scanf, we need the ampersand to be able to access where
//num is located in memory
printf("Double that number is %d\n", num);
}
|