#include<iostream>
usingnamespace std;
// function prototype BEFORE the main,
// NOT INSIDE!
void print(char a[]);
int main()
{
char a[20];
cout << "Enter a word: ";
cin>>a;
cout << "This is the word: ";
// call to the function
print(a);
return 0;
}
// definition of the function
void print(char a[20])
{
for(int i=0; a[i]!='\0'; i++)
{
cout<<a[i];
}
}
For your two previous questions... you should probably read a book about programming.
For the typer effect: what do you mean? A certain delay between the moment you print a letter and the moment you print the following one? To do this you can use one of the sleep functions, but I would not do it with a "useless" for-loop.
In C++, you give the type of the return value and the arguments when you define the function - when you tell the compiler what the function does. When you call the function, you give the actual arguments that you want to pass to the function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// Define a function called myFunc.
// It takes two arguments: an int and a string
int myFunc(int num, string str)
{
cout << num << " " << str << endl;
return num+1
};
int main()
{
int i = 0;
// call MyFunc, passing 1 and "hello" as the arguments
i = myFunc(1, "hello");
i = myFunc(2, "world");
cout << i << endl;
}
So what was going on with your original code? In addition to defining and calling a function, you can also declare a function. This tells the compiler that the function exists, what it takes as arguments, and what it reutrns as a value. You do this just like a definition, but leaving out the code:
1 2
// Declare myFunc, but don't define it.
int myFunc(int num, string str);
There are several reasons to do this, mostly when the function is defined in another file. The problem is that you can put this inside your code:
1 2 3 4 5 6 7
int main()
{
int i;
string str;
int someFunc(int i, string str); // this is a declaration it does NOT call someFunc()
myFunc(i, str); /// this is a call to myFunc
}