19 Feb 2023
Function
Function

Keypoints
- Compiling and running a simple C++ program
- Basic C/C++ programming workflow
- Inside the compiler
- How to call a function
- How to declare a function
- Head file and source file
Hello world
// hello_world.c
#include <stdio.h>
// Print "Hello, world!" followed by newline and exit
int main(void){
printf("Helllo, world\n");
return 0;
}
// Compilation command line
$ gcc hello_world.c -std=c99 -pedantic -Wall -Wextra // or g++ -o generated_file_name target_file_name, ex: g++ res hello_world.c
$ ./a.out
// Output
Hello, world!
- #include is a preprocessor directive, similar to import in Python
- **** means it's defined in stdio.h(standard input output package); **.h** is "header file"
- main is a function, every program has exactly one main
- int is the return type. Because return 0, so its return type is int. If without return, using void as return type.
- (void) says the main function takes no inputs/parameters. If with parameter inputs, ex: void main(int num1, int num2).
- printf prints a string to “standard out” (terminal)
- \n denotes the newline character
- return 0 means “program completed without error”
- gcc is the compiler; hello_world.c is the cpp file name;
- Switches in compilation command: -std=c99 is to use the c99 standard from 1999 year; -pedantic -Wall -Wextra is to get full feedback, i.e. error and warning (potential error)
- ./ is the execution sign; . is current directory
- a.out is the compilation default file name
Basic C/C++ programming workflow
- Edit file (using EMACS or VIM)
- Compile using GNUplot C compiler (gcc) to compile, link, and create excut
- If compile-time errors reported, edit .c file and re-compile
- Run the excutable file
- If run-time errors reported/detected, go back to edit step
Inside the compiler
- Step 1:preprocessor
- Bring together all the code that belongs together
- Process the directives that start with #, such as #include
- also #define (to ensure library exists)
- Step 2:compiler
- Turn human-readable source code into object code
- Might yield warnings and errors if mistakes are “visible” to compiler
- Step 3:linker
- Bring together all the relevant object code into a single excutable file
- Might yield warnings and errors if relevant code is missing, naming conflicts, etc
many .c files --compiler--> many .o files --linker--> one excutable file
How to call a function
int add(int num1, int num2){
int a = num1 + num2;
return a;
}
int main(){
int a = 10;
int b = 20;
int c = add(a+b);
}
NOTE: We initialize and assign value to another variable a in function add. But the a value in function add doesn’t make any difference to a value in main function due to different thread, i.e the value change of parameters doesn’t change the value of argument.
How to declare a function
It’s to notify compiler there is a function before defining it. With so, we can define the function seperately/later.
int max(int a, int b);
int main(){
int a = 10;
int b = 20;
return max(a, b)
}
int max(int a, int b){
return a > b ? a:b;
}
- We initialize and assign value to another variable a in function add. But the a value in function add doesn’t make any difference to a value in main function due to different thread, i.e the value change of parameters doesn’t change the value of argument.
- a > b ? a:b: If the argument before ? is True, then return the value before :, otherwise return the latter one.
Head file and source file
- Set up head file ending with .h
- Set up source file ending with .cpp
- Declare function in head file
- Define function in source file
// head file swap.h
# include <iostream>
using namespace std;
void swap(int a, int b); // declare the function
- using namespace std means that we can use names for objects and variables from the standard library without writing “std::cout”
// source file
# include "swap.h"
void swap(int a, int b){
int temp = a;
a = b;
b = temp;
cout << "a = "<< a << endl;
cout << "b = "<< b << endl;
}
- include the head file using “” not <>. <> is for package.
// main file
# include "swap.h"
# include <iostream>
using namespace std;
int main() {
int a = 10;
int b = 20;
swap(a, b);
system("pause");
return 0;
}