Professional Documents
Culture Documents
Creating functions
With the exception of arrays and functions , C always passes arguments `by value': a copy of
the value of each argument is passed to the function; the function cannot modify the actual
argument passed to it:
void foo(int j) {
j = 0; /* modifies the copy of the argument received by the function
}
int main(void) {
int k=10;
foo(k);
/* k still equals 10
}
*/
If you do want a function to modify its argument you can obtain the desired effect using
pointer arguments instead:
void foo(int *j) {
*j = 0;
}
int main(void) {
int k=10;
foo(&k);
/* k now equals 0
}
*/
*/
Creating functions
pass by value
int func(int x) {
int i = 0 ;
x = 100 ;
printf("this is in the function\n");
i += x ;
return i ;
}
Result :
this is in the function
this is in the function
returned value of func(): 100
value of y : 50
int y = 50;
int x = func(y);
printf("returned value of func(): %d\n",func(y));
printf("value of y : %d\n",y);
return 0;
}
func(y) is not actually using the y directly, it's actually copying its value into the function.
This called call by value and all functions in C are called by value.
It's not actually passing a reference to the variable. So the function has no way of affecting this y.
The value of y will still being 50.
If the return type is void , void func(int x) , then it's no longer legal to return anything.
Creating functions
- pass by reference
Result :
this is in the function
returned value of func(): 100
value of y : 100
Now it's common to include the name, because it gives a hint of how it's used
int func(int x) ;
We have told the compiler exactly what that function looks like, and that it will be defined later.
int func(int x) ;
func.h file
#ifndef FUNC_H_
#define FUNC_H_
int func(int) ;
#endif /* FUNC_H_ */
Call by reference
when a is incremented in
the function only its local
copy is affected
= 1;
#include <stdio.h>
Result :
f(a) is 2
a is 1
int main() {
int a
= 1;
f(&a) ;
printf ("
Result :
a is 2
a is %d\n" , a );
return 0 ;
}
is different from
int main() {
int a = 1;
f(a) ;
printf ("
return 0 ;
}
a is %d\n" , a );
Call by value
Call by reference
#include <iostream>
using namespace std;
#include <iostream>
using namespace std;
void func(int );
void func(int * );
void func(int i) {
i = 132 ;
printf("in func(), i is %d\n", i);
}
Result
in func(), i is 132
i is 7
Result
in func(), i is 132
i is 132
in func(), i is 132
i is 132
Defining Functions
#include <iostream>
using namespace std;
}
#include <iostream>
using namespace std;
void func(string &s) {
s = "hello func()" ;
cout << s << endl ;
}
Result :
hello func()
hello func()
void func();
int main( int argc, char ** argv ) {
func();
func();
func();
return 0;
}
void func() {
int i = 5;
printf("this is func() , i is : %d\n", i++);
}
static int i = 5;
The variable i will be incremented every time. Static storage is not
stored on the stack. It's not temporary. It's persistent for life of the
process. The value is carried from one invocation to another. Static
storage is typically used for keeping state and for other purposes where
you just need to keep the storage around.
Pointer to function
void func();
}
void func() {
printf("this is func()\n");
}
If we use
void (*fptr)() = &func ;
Instead of
is
is
is
is
is
a()");
b()");
c()");
d()");
e()");
}
}
}
}
}
i--;
int prompt() {
puts("Choose an option:");
puts("");;
puts("1. Function a()");
puts("2. Function b()");
puts("3. Function c()");
puts("4. Function d()");
puts("5. Function e()");
puts("Q. Quit.");
printf(">> ");
// list is zero-based
}
}
fflush(stdout);
enum { bufsz = 16 };
static char response[bufsz];
fgets(response, bufsz, stdin);
return jump(response);
}
#include <stdio.h>
int main( int argc, char ** argv ) {
char code = '3';
int i = (int) code - '0';
printf(" %d\n", i) ;
return 0;
}
Dec
48
49
50
51
52
53
54
55
56
57
Hex
30
31
32
33
34
35
36
37
38
39
Char
0
1
2
3
4
5
6
7
8
9
ASCII table
#include <iostream>
#include <vector>
a()
b()
c()
d()
e()
{
{
{
{
{
puts("this
puts("this
puts("this
puts("this
puts("this
is
is
is
is
is
a()");
b()");
c()");
d()");
e()");
}
}
}
}
}
int func() ;
main() {
int (*ptr) () = func ;
printf(" this is i %d\n", (*ptr) ());
}
int func() {
int a =3, b = 4, c =7 ;
int i[] = { a , b , c } ;
return i[0] ;
// volume of a cylinder
double volume( double r, int h ) {
cout << "cylinder of " << r << " x " << h << endl;
return 3.14159 * r * r * h;
}
// volume of a cuboid
long volume( long a, long b, long c ) {
cout << "cuboid of " << a << " x " << b << " x " << c << endl;
return a * b * c;
}
int main( int argc, char ** argv ) {
cout << "volume of a 2 x 2 x 2 cube: " << volume(2) << endl;
cout << "volume of a cylinder, radius 2, height 2: " << volume( 2.0, 2 ) << endl;
cout << "volume of a 2 x 3 x 4 cuboid: " << volume(2, 3, 4) << endl;
return 0;
}
public:
A ( int a ) : a(a) {};
int value() { return a; }
};
int operator + (A & lhs, A & rhs ) {
cout << "operator + for class A" << endl;
return lhs.value() + rhs.value();
}
int main( int argc, char ** argv ) {
A a(5);
A b(42);
cout << "add em up: " << a + b << endl;
return 0;
}
#include <iostream>
using namespace std;
unsigned long int factorial( unsigned long int n ) {
if( n < 2 ) return 1;
return factorial( n - 1 ) * n;
}
Result :
factorial of 10 is 3628800
This actually called itself ten times. Now there is a limit to how many times you can do this, obviously there is
a limit of the size of unsigned long integer, but there is also the limit of the amount of resources that are
available on the computer. Every time you call a function, memory is allocated for the parameters, for any
local variables, and for the return value and all the other function called overhead. If you do this recursively, it
could happen many, many times, and all of those resources keep getting allocated and not deallocated until
the last one is returned, and then all of those resources get deallocated, so this can be very inefficient.
#include <iostream>
using namespace std;