Function
Pass by value
#include <stdio.h>
void doubleNum(int n)
{
printf("In fuction: %d, %p\n", n, &n);
n *= 2;
printf("In fuction: %d, %p\n", n, &n);
}
int main()
{
int a = 10;
printf("Value: %d, Address: %p\n", a, &a);
doubleNum(a);
printf("After calling function: %d, %p\n", a, &a);
}
Pass by reference
#include <stdio.h>
void doubleNum(int *n)
{
printf("In fuction: %d, %p\n", *n, n);
*n *= 2;
printf("In fuction: %d, %p\n", *n, n);
}
int main()
{
int a = 10;
printf("Value: %d, Address: %p\n", a, &a);
doubleNum(&a);
printf("After calling function: %d, %p\n", a, &a);
}
Layout in single file
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int doubleNum(int n)
{
int num;
num = 2*n;
return num;
}
int main()
{
srand(time(NULL)); // take a random seed
int r = rand()%100; // generate a random number within [0, 100)
printf("Value: %d\n", doubleNum(r));
}
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int doubleNum(int);
int main()
{
srand(time(NULL)); // take a random seed
int r = rand()%100; // generate a random number within [0, 100)
printf("Value: %d\n", doubleNum(r));
}
int doubleNum(int n)
{
int num;
num = 2*n;
return num;
}
Layout in multiple files
// f3.c
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include "util.h"
int main()
{
srand(time(NULL)); // take a random seed
int r = rand()%100; // generate a random number within [0, 100)
printf("Value: %d\n", doubleNum(r));
}
// util.c
#include "util.h"
int doubleNum(int n)
{
int num;
num = 2*n;
return num;
}
// util.h
#ifndef UTIL_H
#define UTIL_H
int doubleNum(int);
#endif
Recursion
#include <stdio.h>
int factorial(int n)
{
if(n <= 1) // basis step
return 1;
else // recursive step
return n*factorial(n-1);
}
int main()
{
printf("%d\n", factorial(10));
return 0;
}
#include <stdio.h>
#include <stdlib.h>
void factorial(int *p, int n)
{
if(n <= 1) // basis step
{
*p *= 1;
}
else // recursive step
{
*p *= n;
factorial(p, n-1);
}
}
int main()
{
int *ptr;
ptr = (int *) malloc(sizeof(int));
*ptr = 1;
factorial(ptr, 10);
printf("%d\n", *ptr);
return 0;
}
Static Variable
#include <stdio.h>
void count()
{
static int num = 0;
num++;
printf("%d\n", num);
}
int main()
{
count(); // 1
count(); // 2
return 0;
}
Global Variable
// main.c
#include <stdio.h>
#include "util.h"
extern int count;
int main()
{
increase();
increase();
printf("%d\n", count);
return 0;
}
// util.h
#ifndef UTIL_H
#define UTIL_H
void increase();
#endif
// util.c
#include "util.h"
int count = 0; // global variable
void increase()
{
count++;
}
Static Global Variable
A static global variable is a global variable that can only be accessed by functions in the same C program file as the variable
// main.c
#include <stdio.h>
#include "util.h"
int main()
{
increase();
printf("%d\n", increase()); // 2
return 0;
}
// util.h
#ifndef UTIL_H
#define UTIL_H
int increase();
#endif
// util.c
#include "util.h"
static int count = 0; // global static variable
int increase()
{
count++;
return count;
}
Static Function
A static function is a function that can only be accessed by functions in the same C program file
// main.c
#include <stdio.h>
#include "util.h"
int main()
{
display();
return 0;
}
// util.h
#ifndef UTIL_H
#define UTIL_H
static int getNum();
void display();
#endif
// util.c
#include <stdio.h>
#include <stdlib.h>
#include "util.h"
static int getNum()
{
return rand()%10;
}
void display()
{
printf("%d\n", getNum());
}